he always puts his left hand on the wall no matter he walks or turns around. Because he thinks it is too dangerous, Edison does not walk backward.
NxN square cells which is surrounded by walls from the outside. In the maze, some of the cells are also walls. Edison can only move between two empty cells in four directions, north, south, west and east. In order to get out of the maze, he drafts a plan. He uses his left hand to lean on the wall and goes by following the wall.
T. T test cases follow. Each test case starts with an integer N. N is the size of the maze. The following N lines, each line contains N characters which may be '.' or '#'. '.' is an empty cell, '#' is a wall. Followed by a line which contains four integers: sx, sy, ex, ey. (sx, sy) means that Edison is standing on row sx and column sy as his starting cell, (ex, ey) is the exit of the maze. (sx, sy) is guaranteed to be at one of the 4 corners of the maze, and Edison can only touch the wall on 4 adjacent cells(not 8) initially. (ex, ey) can be anywhere in the maze. Note that the top-left corner is at position (1,1).
≤ 30.
≤
N ≤ 10.
N ≤ 100.
左邊沒有牆,一定會左轉再直走;
前面是牆,則一定會順時針旋轉,依次摸索每個方向是否能走;
一條路一種走法,最後會回到起點,且回到起點時的方向會是初始方向。
所以,只需要按照規則依次走完並記錄每次的方向即可,遇到終點即返回;若走完依次未遇到終點則返回失敗。
遞迴求解,fun(sx,sy,ex,ey,head),參數為起點,終點,當前方向
細節:
1、我採用遞迴求解,邊界問題很重要,若起點終點相同,直接返回0,若起點本身是牆,直接返回-1。
2、考慮上述分析第1條,每次都先將當前方向head向左轉,再一次遍曆四個方向
3、用flag[x][y][4]記錄每個點的4個方向是否遍曆過,若之前遍曆過則跳過這個方向
4、遍曆四個方向時,若發現當前方向不是遍曆的第一個方向,且左手方向不是牆,證明已經旋轉的脫離牆了,那麼這個點已經遍曆結束,直接跳出迴圈。(因為預設遍曆的第一個方向是先向左轉,此時左手方向是上一個走過的點,一定不是牆,但是繼續順時針轉的話,必須保證左手是牆才能前進)
5、每次記錄當前走的方向,並在遞迴失敗後去掉。這裡用string記錄的時候,記錄到4500個就崩潰了。。不知為何。。後來用char數組就沒問題了
代碼:
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<cstring>#include<string>#include<iostream>#define MIN(x,y) (x)<(y)?(x):(y)using namespace std;const int N=110;char m[N][N];bool mz[N][N];int dir[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};char dirn[4] = {'W','N','E','S'};bool flag[N][N][4];char step[20010];int stepnum;int fun(int sx,int sy,int ex,int ey,int head){//printf("sx %d sy %d step %d\n",sx,sy,step);if(sx==ex && sy==ey) return 0;if(mz[sx][sy] == 0) return -1;head = (head+3)%4;for(int i=0; i<4; i++){int headx = sx+dir[head][0];int heady = sy+dir[head][1];int left = (head+3)%4;int leftx = sx+dir[left][0];int lefty = sy+dir[left][1];if(flag[sx][sy][head]) continue;if(i>0 && mz[leftx][lefty]) break;step[stepnum++] = dirn[head];flag[sx][sy][head] = 1;int tmp = fun(headx,heady,ex,ey,head);if(tmp >= 0) return tmp + 1;step[--stepnum] = 0;head = (head+1)%4;}return -1;}int main(){freopen("D-large-practice.in","r",stdin);freopen("D-large-practice.out","w",stdout);int t;scanf("%d",&t);for(int cnt=1;cnt<=t;cnt++){int n;scanf("%d",&n);memset(m,0,sizeof(m));memset(mz,0,sizeof(mz));memset(flag,0,sizeof(flag));for(int i=0; i<n; i++)scanf("%s",m[i]);for(int i=0; i<n; i++)for(int j=0; j<n; j++)if(m[i][j] == '.')mz[i+1][j+1] = 1;int sx,sy,ex,ey;scanf("%d%d%d%d",&sx,&sy,&ex,&ey);memset(step,0,sizeof(step));stepnum = 0;int head;if(sx==1 && sy==1) head = 3;else if(sx==1 && sy>1) head = 0;else if(sx>1 && sy==1) head = 2;else head = 1;int ans = fun(sx,sy,ex,ey,head);printf("Case #%d: ",cnt);if(ans < 0) printf("Edison ran out of energy.\n"); else if(ans == 0) printf("0\n");else printf("%d\n%s\n",ans,step);} }