#include <bits/stdc++.h>
#define MAXN 510
using namespace std;
typedef pair < int, int > ii;
typedef pair < int, ii > iii;
int n, m, val[MAXN][MAXN], c, f, vist[MAXN*MAXN];
char mat[MAXN][MAXN];
int l[MAXN][MAXN], r[MAXN][MAXN], u[MAXN][MAXN], d[MAXN][MAXN];
vector < int > grafo[MAXN*MAXN];
int bfs()
{
queue < ii > fila;
fila.push({c, 0});
while(fila.size())
{
ii u = fila.front();
fila.pop();
int v = u.first;
int d = u.second;
if(vist[v])continue;
vist[v] = d;
if(v == f) return d;
for(int i = 0 ; i < grafo[v].size() ; i++)
{
int at = grafo[v][i];
fila.push({at, d+1});
}
}
return -1;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
int cont = 1;
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= m ; j++)
{
cin >> mat[i][j];
val[i][j] = cont++;
if(mat[i][j] == 'C') c = cont-1;
else if(mat[i][j] == 'F') f = cont-1;
}
}
// vizinhos esquerda
int last;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(mat[i][j]=='#'){
last=j;
}
else{
if(mat[i+1][j]=='#')l[i][j]=val[i][last+1];
if(mat[i-1][j]=='#')l[i][j]=val[i][last+1];
if(mat[i][j+1]=='#')l[i][j]=val[i][last+1];
if(mat[i][j-1]=='#')l[i][j]=val[i][last+1];
}
}
}
// vizinhos direita
for(int i=1; i<=n; i++){
for(int j=m; j>=1; j--){
if(mat[i][j]=='#'){
last=j;
}
else{
if(mat[i+1][j]=='#')r[i][j]=val[i][last-1];
if(mat[i-1][j]=='#')r[i][j]=val[i][last-1];
if(mat[i][j+1]=='#')r[i][j]=val[i][last-1];
if(mat[i][j-1]=='#')r[i][j]=val[i][last-1];
}
}
}
// vizinhos cima
for(int j=1; j<=m; j++){
for(int i=1; i<=n; i++){
if(mat[i][j]=='#'){
last=i;
}
else{
if(mat[i+1][j]=='#')u[i][j]=val[last+1][j];
if(mat[i-1][j]=='#')u[i][j]=val[last+1][j];
if(mat[i][j+1]=='#')u[i][j]=val[last+1][j];
if(mat[i][j-1]=='#')u[i][j]=val[last+1][j];
}
}
}
// vizinhos baixo
for(int j=1; j<=m; j++){
for(int i=n; i>=1; i--){
if(mat[i][j]=='#'){
last=i;
}
else{
if(mat[i+1][j]=='#')d[i][j]=val[last-1][j];
if(mat[i-1][j]=='#')d[i][j]=val[last-1][j];
if(mat[i][j+1]=='#')d[i][j]=val[last-1][j];
if(mat[i][j-1]=='#')d[i][j]=val[last-1][j];
}
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(mat[i][j]=='#')continue;
//if(mat[i+1][j]!='#')grafo[val[i][j]].push_back(val[i+1][j]);
////if(mat[i-1][j]!='#')grafo[val[i][j]].push_back(val[i-1][j]);
//if(mat[i][j+1]!='#')grafo[val[i][j]].push_back(val[i][j+1]);
//if(mat[i][j-1]!='#')grafo[val[i][j]].push_back(val[i][j-1]);
if(mat[i+1][j]=='#')
{
grafo[val[i][j]].push_back(r[i][j]);
grafo[val[i][j]].push_back(d[i][j]);
grafo[val[i][j]].push_back(l[i][j]);
grafo[val[i][j]].push_back(u[i][j]);
}
else if(mat[i-1][j]=='#'){
grafo[val[i][j]].push_back(r[i][j]);
grafo[val[i][j]].push_back(d[i][j]);
grafo[val[i][j]].push_back(l[i][j]);
grafo[val[i][j]].push_back(u[i][j]);
}
else if(mat[i][j+1]=='#'){
grafo[val[i][j]].push_back(r[i][j]);
grafo[val[i][j]].push_back(d[i][j]);
grafo[val[i][j]].push_back(l[i][j]);
grafo[val[i][j]].push_back(u[i][j]);
}
else if(mat[i][j-1]=='#'){
grafo[val[i][j]].push_back(r[i][j]);
grafo[val[i][j]].push_back(d[i][j]);
grafo[val[i][j]].push_back(l[i][j]);
grafo[val[i][j]].push_back(u[i][j]);
}
}
}
int resp = bfs();
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= m ; j++)
{
//cout << vist[val[i][j]] << " ";
}
//cout << endl;
}
if(resp == -1) cout << "nemoguce\n";
else cout << resp << endl;
}
Compilation message
portal.cpp: In function 'int bfs()':
portal.cpp:32:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < grafo[v].size() ; i++)
~~^~~~~~~~~~~~~~~~~
portal.cpp: In function 'int main()':
portal.cpp:101:41: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(mat[i-1][j]=='#')u[i][j]=val[last+1][j];
~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
6528 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
6528 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
6528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
6656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
6656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
8832 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
9132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
12940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
48 ms |
15596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
15608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |