Very simple BFS.
WA reason: or because rqnoj data input with GetChar () seems to be wrong, changed to scanf ("%s"), and then because did not notice starting from 1 ...
scanf ("%s", Map[i]);
Change into
scanf ("%s", &map[i][1]);
It's AC.
This simple search question can not be once AC is the reflection of the following ...
#define _crt_secure_no_warnings #include <iostream> #include <cstdio> #include <cstring> #include
<queue> using namespace std;
#define INF 0x3f3f3f3f int N, dist[1001][1001];
Char map[1001][1001];
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0,-1, 1};
typedef pair<int, int> P;
Queue<p> que;
int BFS (P s, p g) {memset (dist, 0x3f, sizeof (Dist));
Dist[s.first][s.second] = 0;
Map[s.first][s.second] = ' 1 ';
Que.push (s);
while (Que.size ()) {p p = que.front (); Que.pop ();
if (P.first = = G.first && P.second = = G.second) return Dist[g.first][g.second];
for (int i = 0; i < 4; i++) {int NX = P.first+dx[i], NY = p.second+dy[i];
if (0<nx&&nx<=n && 0<ny&&ny<=n && map[nx][ny]== ' 0 ') {map[nx][ny] = ' 1 ';
Dist[nx][ny] = Dist[p.first][p.second] + 1;
Que.push (P (NX, NY));
}} return-1;
int main () {scanf ("%d", &n); for (int i = 1; I <= N; i++) scanf ("%s", &map[i][1]);
int SX, SY, GX, GY;
scanf ("%d%d%d%d", &sx, &sy, &GX, &gy);
printf ("%d\n", BFS (P (sx,sy), P (gx,gy));
return 0; }