Escape from Tetristime limit:12000/4000ms (java/other) Memory limit:32768/32768k (Java/other) total submission (s): 1 Accepted Submission (s): 1Problem description because all day and all night to this board, Lele finally went mad. Every day he sleeps, he will dream that he will be thrown into a chessboard, can not find a way out, and then awakened from the dream. Over time, Lele was mentally weakened. Whether the dream will become a reality, who can not say, but not afraid of 10,000 only fear in the event. Now Lele every time he sees a chessboard, he will imagine how to escape after being locked in.
Lele encountered the chessboard are square, some of the lattice is bad, can not go, the rest are can go. Just walk to the edge of the chessboard (the outermost lap), even if you have escaped. Lele dreamed that he would be thrown in a walking lattice, but unsure of which one it was, so he was prepared to be thrown in any lattice.
Now Lele ask you to help, for any of the chessboard, find a shortest sequence, the sequence can include "North" (map Up), "East" (map to the right), "South" (Map Down), "West" (map left), these four directions command. No matter which Lele is thrown in a good grid in the chessboard, he can walk out of the chessboard by this sequence.
The specific way to escape is: no matter where the Lele is thrown, Lele follow the direction of the sequence to walk one after the other, each command to walk a box, if you go to encounter a bad lattice, then ignore this command. Of course, if you have escaped, you can leave out the commands left in the sequence.
Input This topic contains multiple sets of tests, please process to the end of the file. The first row of each group of tests contains a positive integer n (0<n<9), which means that the size of the checkerboard is N*n= "" followed by n lines, each with n characters representing the chessboard. = "" Where 0 means the position is good, can go, 1 means the position is bad and cannot go. = "" Title data guarantee, for any chessboard, there is the sequence required in the topic = "" <= "" div= "" >
Output for each set of data, outputs the sequence required by the title, one row for each element in the sequence. If there are two qualifying sequences, output the sequence with the smallest dictionary order. Please separate the two Tests with a blank line.
Sample Input
41101000111001001
Sample Output
Eastnorth
Authorlinle
Sourcehdoj Summer Exercise (2)
Title ida*
Wide search for each possible landing point of the pre-valuation, by the side wide search
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <string >using namespace Std;int dir[4][2]= {0,1,-1,0,1,0,0,-1};char Map[10][10];int n,len,ll;char ch[4][10]= {{"East"},{" North "},{" South "},{" West "}};int vis[10][10],flag[1111];struct node{int x, y; BOOL Cheak () {if (x>=0&&x<n&&y>=0&&y<n) return true; return false; }} a[111];void bfs (int x,int y)//preprocessing estimate {node E; E.x=x,e.y=y; queue<node>q; Q.push (e); vis[e.x][e.y]=0; while (Q.size ()) {E=q.front (); Q.pop (); for (int i=0; i<4; i++) {node ee=e; EE.X+=DIR[I][0]; EE.Y+=DIR[I][1]; if (Ee.cheak ()) {if (map[ee.x][ee.y]== ' 0 ') if (vis[ee.x][ee.y]==-1| | vis[ee.x][ee.y]>vis[e.x][e.y]+1) {vis[ee.x][ee.y]=vis[e.x][e.y]+1; Q.push (EE); }}}} return;} int Get_h (node * e) {int ans=0; for (int i=0; i<len; i++) {Ans=max (Vis[e[i].x][e[i].y],ans); } return ans; BOOL DFS (int length,node * e) {if (Length<get_h (e)) return false; if (length==0) return true; for (int i=0; i<4; i++) {node ee[111]; for (int j=0; j<len; J + +) {Ee[j]=e[j]; EE[J].X+=DIR[I][0]; EE[J].Y+=DIR[I][1]; if (!ee[j].cheak () | | | e[j].x==0| | e[j].y==0| | e[j].x==n-1| | e[j].y==n-1| | map[ee[j].x][ee[j].y]== ' 1 ')//not reaching exit {EE[J]=E[J]; }} flag[length]=i; if (Dfs (LENGTH-1,EE)) return true; } return false;} int main () {bool ant=false; while (~SCANF ("%d", &n)) {if (ant) printf ("\ n"); Ant=true; for (int i=0; i<n; i++) {GetChar (); for (int j=0; j<n; j + +) scanf ("%c", &map[i][j]); } memset (Vis,-1,sizeof (VIS)); len=0; for (int i=0, i<n; i++) for (int j=0; j<n; J + +) {if (map[i][j]== ' 0 ' && (i== 0| | i==n-1| | j==0| | j==n-1)) BFS (I,J); else if (map[i][j]== ' 0 ') a[len].x=i,a[len++].y=j; } int Length=get_h (a); while (!dfs (length,a)) {length++; } while (length--) {printf ("%s\n", Ch[flag[length+1]]); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1813 Escape from Tetris