problem Description
In an n∗m maze, the Right-bottom corner isThe exit (position (N,M) isThe exit). In every position of ThisMaze, there isEither a0or a1written on it. An explorer gets lostinch ThisGrid. His position now is(1,1), and he wants to go to the exit. Since to arrive at the exit isEasy forHim, he wants to DoSomething more difficult. At first, he'll write down the number on position. Every time, he could make a move to one adjacent position (both positions is adjacent if and only if they share an edge). While walking, he would write down the number on the position he's on to the end of his number. When finished, he'llGetA binary number. Please determine the minimum value of ThisNumberinchBinary system.
Input
is a single integer T (t=), indicating the number of testcases. For each testcase, the first line contains the integers n and m (1≤n,m≤string
of length m, which represents i-th row of the maze.
Output
inch 0 is 0 (in this case0 instead).
Sample Input
2 2 2 One One 3 3 001 111 101
Sample Output
111 101
Author
XJZX
Source
multi-university Training Contest 4
1, if mp[0][0]==0, first BFS once, the leading 0 is removed. BFs got Wx,wy.
2, then is to find the smallest path. A walking count for (int i=wx+wy;i<n+m-2;i++), which enumerates all possible (x, y) x+y==step based on this number of steps, and if 0 is encountered, fetch 0, always found (n-1,m-1)
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5#include <stdlib.h>6 using namespacestd;7 #defineN 10068 intn,m;9 intMp[n][n];Ten intVis[n][n]; One structnode{ A intx, y; - }st; - intdirx[]={1,0,-1,0}; the intdiry[]={0,1,0,-1}; - intWx,wy; - voidBFs () { -Queue<node>Q; + Q.push (ST); - Node t1,t2; + while(!Q.empty ()) { At1=Q.front (); at Q.pop (); - for(intI=0;i<4; i++){ -t2.x=t1.x+Dirx[i]; -t2.y=t1.y+Diry[i]; - if(Vis[t2.x][t2.y])Continue; - if(t2.x<0|| T2.x>=n | | t2.y<0|| T2.Y>=M)Continue; invis[t2.x][t2.y]=1; - if(mp[t2.x][t2.y]==0){ to Q.push (T2); + } - if(wx+wy<t2.x+t2.y) { thewx=t2.x; *wy=t2.y; $ }Panax Notoginseng } - } the } + intMain () A { the intT; +scanf"%d",&t); - while(t--){ $scanf"%d%d",&n,&m); $ Chars[1006]; - for(intI=0; i<n;i++){ -scanf"%s", s); the for(intj=0; j<m;j++){ -mp[i][j]=s[j]-'0';Wuyi } the } -st.x=0; Wust.y=0; -memset (Vis,0,sizeof(Vis)); Aboutvis[0][0]=1; $wx=0; -wy=0; - if(mp[0][0]==0){ - BFS (); A } + if(mp[wx][wy]==0){ theprintf"0\n"); - Continue; $ } theprintf"1"); the intnowflag=0; the for(inti=wx+wy;i<n+m-2; i++){ the intflag=0; - intstep=i; in for(intj=0; j<=step;j++){ the intx=J; the inty=i-J; About if(x<0|| X>=n | | y<0|| Y>=M)Continue; the if(Nowflag && Mp[x][y])Continue; the if(!vis[x][y])Continue; the + for(intk=0;k<2; k++){ - intsx=x+Dirx[k]; the intsy=y+Diry[k];Bayi if(sx<0|| Sx>=n | | sy<0|| SY>=M)Continue; thevis[sx][sy]=1; the if(mp[sx][sy]==0){ -flag=1; - } the } the the } thenowflag=Flag; - if(flag) { theprintf"0"); the } the Else{94printf"1"); the } the } theprintf"\ n");98 } About return 0; -}
View Code
Hdu 5335 Walk out (bfs+ looking for path)