标签:
http://poj.org/problem?id=3984
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
Input
Output
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
#include <iostream> #include <cstdio> #include <cstring> #include <stdlib.h> #include <math.h> #include <queue> #include <algorithm> using namespace std; #define maxn 521 #define INF 0xfffffff int maps[maxn][maxn]; int dir[4][2]= {{0,1},{1,0},{-1,0},{0,-1}}; struct node { int x, y; }a[maxn][maxn]; void DFS(int x, int y) { if(x == 0 && y == 0) { printf("(0, 0)\n"); return ; } DFS(a[x][y].x, a[x][y].y); printf("(%d, %d)\n",x, y); } void BFS() { int i; queue<node>Q; node begins, now, next; begins.x = 0; begins.y = 0; Q.push(begins); while(Q.size()) { now = Q.front(); Q.pop(); if(now.x == 4 && now.y == 4) return ; for(i=0; i<4; i++) { next.x = now.x + dir[i][0]; next.y = now.y + dir[i][1]; if(next.x>=0 && next.x<5 && next.y>=0 && next.y<5 && !maps[next.x][next.y]) { maps[next.x][next.y]=1; a[next.x][next.y] = now; Q.push(next); } } } } int main() { int i,j; for(i=0; i<5 ; i++) for(j=0; j<5; j++) scanf("%d", &maps[i][j]); BFS(); DFS(4, 4); return 0; }
标签:
原文地址:http://www.cnblogs.com/daydayupacm/p/5683406.html