注意这道题的输入,每行有时候会包含多余的空格信息,只能采用cin cout存储字符,用getchar或者scanf的时候会ole。
#include <iostream> #include <stdio.h> #include <string.h> #include <queue> using namespace std; int M, N; int maze[25][25]; int people; class T { public: int loc; int len; T(int a, int b) { loc = a; len = b; } }; int bfs() { queue<T> way; way.push(T(people, 0)); while(!way.empty()) { T now = way.front(); int nowLoc = now.loc; int nowLen = now.len; way.pop(); if(nowLoc - N >= 0) { switch(maze[nowLoc / N - 1][nowLoc % N]) { case (0): maze[nowLoc / N - 1][nowLoc % N] = 1; way.push(T(nowLoc - N, nowLen + 1)); break; case (2): return nowLen + 1; } } if(nowLoc % N - 1 >= 0) { switch(maze[nowLoc / N][nowLoc % N - 1]) { case (0): maze[nowLoc / N][nowLoc % N - 1] = 1; way.push(T(nowLoc - 1, nowLen + 1)); break; case (2): return nowLen + 1; } } if(nowLoc % N + 1 < N) { switch(maze[nowLoc / N][nowLoc % N + 1]) { case (0): maze[nowLoc / N][nowLoc % N + 1] = 1; way.push(T(nowLoc + 1, nowLen + 1)); break; case (2): return nowLen + 1; } } if(nowLoc / N + 1 < M) { switch(maze[nowLoc / N + 1][nowLoc % N]) { case (0): maze[nowLoc / N + 1][nowLoc % N] = 1; way.push(T(nowLoc + N, nowLen + 1)); break; case (2): return nowLen + 1; } } } return -1; } int main() { char ch; cin >> M >> N; while(M != 0 && N != 0) { for(int i = 0; i < M; i++) { for(int j = 0; j < N; j++) { cin >> ch; switch(ch) { case ('@'): people = i * N + j; maze[i][j] = 1; break; case ('.'): maze[i][j] = 0; break; case ('#'): maze[i][j] = -1; break; case ('*'): maze[i][j] = 2; } } } cout << bfs() << endl; cin >> M >> N; } return 0; } #include <iostream> #include <cstdio> #include <cstdlib> #include <string.h> #include <queue> using namespace std; int m, n; int maze[25][25]; int x, y; typedef struct position { int row; int col; int pace; }pos; int bfs() { queue<pos> q; int p = -1; pos start; start.row = x; start.col = y; start.pace = 0; q.push(start); while(!q.empty()) { pos now = q.front(); q.pop(); int x1 = now.row; int y1 = now.col; if(x1 > 1) { switch(maze[x1 - 1][y1]) { case 0: pos next; next.row = x1 - 1; next.col = y1; next.pace = now.pace + 1; q.push(next); maze[x1 - 1][y1] = 1; break; case 2: return now.pace + 1; } } if(x1 < m) { switch(maze[x1 + 1][y1]) { case 0: pos next; next.row = x1 + 1; next.col = y1; next.pace = now.pace + 1; q.push(next); maze[x1 + 1][y1] = 1; break; case 2: return now.pace + 1; } } if(y1 > 1) { switch(maze[x1][y1 - 1]) { case 0: pos next; next.row = x1; next.col = y1 - 1; next.pace = now.pace + 1; q.push(next); maze[x1][y1 - 1] = 1; break; case 2: return now.pace + 1; } } if(y1 < n) { switch(maze[x1][y1 + 1]) { case 0: pos next; next.row = x1; next.col = y1 + 1; next.pace = now.pace + 1; q.push(next); maze[x1][y1 + 1] = 1; break; case 2: return now.pace + 1; } } } return p; } int main() { //scanf("%d%d", &m, &n); cin >> m >> n; while(m != 0 || n != 0) { //getchar(); for(int i = 1; i <= m; i++) { for(int j = 1; j <= n; j++) { char ch; //ch = getchar(); //scanf("%c", &ch); cin >> ch; switch(ch) { case '@': x = i; y = j; maze[i][j] = 1; break; case '.': maze[i][j] = 0; break; case '#': maze[i][j] = -1; break; case '*': maze[i][j] = 2; break; } } //getchar(); } //printf("%d\n", bfs()); cout << bfs() << endl; //scanf("%d%d", &m, &n); cin >> m >> n; } return 0; }