经典的BFS。
1 #include2 #include 3 4 #define MAXNUM 105 5 #define MAXROW 105 6 #define MAXQUE 10005 7 8 char buf[MAXROW][MAXNUM]; 9 char visit[MAXROW][MAXNUM];10 11 typedef struct {12 int x, y;13 } pos_st;14 15 pos_st queue[MAXQUE];16 int direct[8][2] = { { 0,-1},{-1,-1},{-1,0},{-1,1},{ 0,1},{ 1,1},{ 1,0},{ 1,-1}};17 int m, n;18 int total;19 20 void bfs();21 void search();22 23 int main() {24 int i;25 26 while (scanf("%d%d", &m, &n)!=EOF && (m||n)) {27 getchar();28 for (i=0; i 0) {60 queue[rear].x = row;61 queue[rear].y = col;62 rear++;63 while (front != rear) {64 x = queue[front].x;65 y = queue[front].y;66 front++;67 for (i=0; i<8; ++i) {68 newx = x+direct[i][0];69 newy = y+direct[i][1];70 if (newx>=0 && newx =0 && newy