4 4 XXXX .Z.. .XS. XXXX 4 4 XXXX .Z.. .X.S XXXX 4 4 XXXX .ZX. .XS. XXXX
1 1 Bad Luck!
题意:S和Z同在一迷宫,其中一个人走一步,另外一个人就朝相反的方向走一步,问多少步能够到达相同位置或者两人相邻
思路:开一个四维数组记录,要注意的是两人的初始位置要置为“.”空地
本来我一开始是开了两个三维数组的,虽然过了样例还是A不了,求教哪位高手用三维做的指点下
#include <stdio.h> #include <string.h> #include <queue> using namespace std; int m,n; int z1,z2,s1,s2; char map[25][25]; int vis[25][25][25][25]; int to[4][2] = {1,0,-1,0,0,-1,0,1};//Z的走法 int to2[4][2] = {-1,0,1,0,0,1,0,-1};//S走相反路线 struct node { int x1,x2,y1,y2,step; }; int check(int x,int y) { if(x<0 || y<0 || x>=n || y>=m || map[x][y] == 'X') return 1; return 0; } int judge(int x,int x1,int y,int y1)//符合条件的状况 { if(x == x1 && y == y1) return 1; if(x == x1+1 && y == y1) return 1; if(x == x1-1 && y == y1) return 1; if(x == x1 && y == y1-1) return 1; if(x == x1 && y == y1+1) return 1; return 0; } int bfs() { int i; queue<node> Q; node a,next; memset(vis,0,sizeof(vis)); a.x1 = z1; a.x2 = s1; a.y1 = z2; a.y2 = s2; a.step = 0; vis[z1][z2][s1][s2] = 1;//四维数组记录行走状况 Q.push(a); while(!Q.empty()) { a = Q.front(); Q.pop(); if(judge(a.x1,a.x2,a.y1,a.y2)) return a.step; for(i = 0; i<4; i++) { next = a; next.x1 = a.x1+to[i][0]; next.y1 = a.y1+to[i][1]; next.x2 = a.x2+to2[i][0]; next.y2 = a.y2+to2[i][1]; if(check(next.x1,next.y1)) continue; if(check(next.x2,next.y2)) { next.x2 = a.x2; next.y2 = a.y2; } if(vis[next.x1][next.y1][next.x2][next.y2]) continue; vis[next.x1][next.y1][next.x2][next.y2] = 1; next.step = a.step+1; Q.push(next); } } return 0; } int main() { while(~scanf("%d%d",&n,&m)) { int i,j; for(i = 0; i<n; i++) { scanf("%s",map[i]); for(j = 0; map[i][j]; j++) { if(map[i][j] == 'Z') { map[i][j] == '.';//初始位置置为空地 z1 = i; z2 = j; } else if(map[i][j] == 'S') { map[i][j] == '.'; s1 = i; s2 = j; } } } int ans; ans = bfs(); if(ans) printf("%d\n",ans); else printf("Bad Luck!\n"); } return 0; }