1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include <cstdio> #include <queue> using namespace std;
int n,m; int vis[5][5]; int des[5][2] = {{0,0},{1,0},{-1,0},{0,1},{0,-1}};
struct p{ int x; int y; int pre[50][2]; int w; }; int maze[5][5]; bool check(int x,int y){ if(x >= 0 && x <= 4 && y >= 0 && y <= 4 && vis[x][y] == 0 && maze[x][y] == 0) return 1; return 0; }
queue<p>q; void dfs(){ p p1; p1.x = 0; p1.y = 0; p1.w = 0; p1.pre[p1.w][0] = p1.x; p1.pre[p1.w][1] = p1.y; q.push(p1); while(!q.empty()){ p pp ; pp = q.front(); q.pop();
if(pp.x == 4 && pp.y == 4){ for(int i = 0 ; i <= pp.w ; i++){ printf("(%d, %d)\n",pp.pre[i][0],pp.pre[i][1]); } } p pz; for(int i = 1; i <= 4 ; i++){ pz = pp; pz.w = pp.w + 1; pz.x = pp.x + des[i][0]; pz.y = pp.y + des[i][1]; pz.pre[pz.w][0] = pz.x; pz.pre[pz.w][1] = pz.y; if(check(pz.x,pz.y)){ vis[pz.x][pz.y] = 1; q.push(pz); } } }
}
int main() { for(int i = 0 ; i < 5 ; i++){ for(int j= 0 ; j < 5 ; j++){ scanf("%d",&maze[i][j]); } } dfs(); }
|