Description
Recently DZB is playing a game called Dot Dot Dot. It's a simple game but very interesting. The rule is like this:
The game is played on a N*M grid map, each grid is adjacent with 4 grids(up, down, left, right). There's only one exit on the map, the game finished when the player reach the exit. When the game starts, the player go to the exit step by step. In each step he can only move to adjacent grid. But there's some barriers on the map, each barrier is yellow or green. The player can not go to the grid when there is a barrier or there is outside wall.
The only way to destroy the barriers is to stand on some grid which has a trigger. Each trigger is also yellow or green and can be used only once. When the player stand on it, it will sent shock waves to destroy all the barriers with the same color it could reach directly. Shock waves spread as wide as possible. It can spread on all the connected grids which no barrier is on it, that means if it reach a barrier with different color, it will be blocked too. Notice if the wave can not reach a barrier directly, it can not be destroied, even if it's the same color with the trigger.
Now DZB not only wants to finish the game, but also wants to finish the game with the minimal step, so he ask you for help.
Input
There are multiple test cases. For each test case:
The first line contains two integers N(1≤N≤40), M(1≤M≤40), N is the number of rows of the map, and M is the number of the columns.
The after N lines describe the map, each line has M characters. A character could be:
- '.' means there's nothing, you can stand there.
- '*' means the outside wall, we guarantee that the outside wall form a close area and the player is in it.
- 'Y' means a yellow barrier, you can not stand there unless you have destroied the barrier.
- 'G' means a green barrier, similar with 'Y'.
- 'y' means a yellow trigger.
- 'g' means a green trigger. Notice the number of yellow trigger plus the number of green trigger is no more than 5.
- 's' means the initial position of the player.
- 'e' means the exit, the player must go to there finally to finish the game.
There is a blank line between every two cases.
Process to the end of input.
Output
One line for each test case. The minimal step. If can not reach the exit, print -1 instead.
Sample Input
5 17 ***************** *.......Y.......* *.y.s...Y....e..* *.......Y.......* *****************
Sample Output
13
说实话这个题对于我而言有点难,参考了WZK学长的代码,之后自己做了一些变动最终AC了 具体思路就是每走到一个点去储存这个点的状态(距离,坐标,当前地图),之后进行遍历就可以了
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define MAX_SIZE 40 + 1 #define LEN 4000 + 10 struct States{ int x; /*当前x坐标*/ int y; /*当前y坐标*/ int dist; /*当前距离*/ char G[MAX_SIZE][MAX_SIZE]; /*当前地图*/ }state[LEN]; States start;/*初始的状态*/ int mins; int visa[MAX_SIZE][MAX_SIZE];/*记录点是否被访问过*/ int m,n;/*地图的长度和宽度*/ int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; /*4个方向,上、下、左、右*/ void Get_start(){ /*获得初始地图的起点*/ for(int i(0);i<n;i++) scanf("%s",start.G[i]); for(int i(0);i<n;i++) for(int j(0);j<strlen(start.G[i]);j++){ if(start.G[i][j] == 's' ){ start.x=i; start.y=j; start.G[i][j]='.'; } } start.dist=0; } void strpy(States &st1,States &st2){ /*复制地图的函数*/ st1.x=st2.x; st1.y=st2.y; st1.dist=st2.dist; memcpy(st1.G,st2.G,sizeof(st1.G)); } bool Judge_char(char x,char y){ if(x=='Y'&&y=='y') return true; else if(x=='G'&&y=='g') return true; else return false; } void Do_map(char G[][MAX_SIZE],int x,int y,char t){ if(x<0||y<0||x>=n||y>=m||visa[x][y]) return ; visa[x][y]=1; char w = G[x][y]; if(Judge_char(w,t)){ G[x][y]='.'; return ; } if(w!='.') return ; else{ Do_map(G,x-1,y,t); Do_map(G,x,y-1,t); Do_map(G,x,y+1,t); Do_map(G,x+1,y,t); } return ; } void display(char G[][MAX_SIZE]){ for(int i=0;i<n;i++){ for(int j=0;j<m;j++) printf("%c",G[i][j]); printf("\n"); } } bool Judge_State(States &st,int q){ for(int i(0);i<q;i++){ if(st.x==state[i].x&&st.y==state[i].y&&memcmp(st.G,state[i].G,sizeof(st.G))==0) return false; } return true; } int DFS(){ int front = 0; int back = 0; int ok = 0; strpy(state[back++],start); visa[start.x][start.y]=1; while(front<=back){ States st; strpy(st,state[front++]); for(int i=0;i<4;i++){ int nx=st.x+dir[i][0]; int ny=st.y+dir[i][1]; if(nx>=0&&ny>=0&&nx<n&&ny<m){ char new_G[MAX_SIZE][MAX_SIZE]; char t = st.G[nx][ny]; memcpy(new_G,st.G,sizeof(new_G)); if(t=='*'||t=='Y'||t=='G'){ continue; } /*如果是死路就停止遍历*/ else if(t=='e'){ ok=1; mins=mins<(st.dist+1)?mins:(st.dist+1); } else if(t=='y'||t=='g'){ // display(new_G); new_G[nx][ny]='.'; memset(visa,0,sizeof(visa)); Do_map(new_G,nx,ny,t); // display(new_G); /*修改地图*/ } state[back].x=nx; state[back].y=ny; state[back].dist=st.dist+1; memcpy(state[back].G,new_G,sizeof(new_G)); if(Judge_State(state[back],back)) back++; } } } if(ok) return mins; else return -1; } int main() { int cases=1; while(scanf("%d%d",&n,&m)!= EOF){ mins=99999; Get_start(); printf("%d\n",DFS()); cases++; } return 0; }