Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 639 Accepted Submission(s): 114
Problem Description
In an
n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit). In every position of this maze, there is either a 0 or a 1 written on it.
An explorer gets lost in this grid. His position now is (1,1), and he wants to go to the exit. Since to arrive at the exit is easy for him, he wants to do something more difficult. At first, he'll write down the number on position (1,1). Every time, he could make a move to one adjacent position (two positions are adjacent if and only if they share an edge). While walking, he will write down the number on the position he's on to the end of his number. When finished, he will get a binary number. Please determine the minimum value of this number in binary system.
An explorer gets lost in this grid. His position now is (1,1), and he wants to go to the exit. Since to arrive at the exit is easy for him, he wants to do something more difficult. At first, he'll write down the number on position (1,1). Every time, he could make a move to one adjacent position (two positions are adjacent if and only if they share an edge). While walking, he will write down the number on the position he's on to the end of his number. When finished, he will get a binary number. Please determine the minimum value of this number in binary system.
Input
The first line of the input is a single integer
T (T=10), indicating the number of testcases.
For each testcase, the first line contains two integers n and m (1≤n,m≤1000). The i-th line of the next n lines contains one 01 string of length m, which represents i-th row of the maze.
For each testcase, the first line contains two integers n and m (1≤n,m≤1000). The i-th line of the next n lines contains one 01 string of length m, which represents i-th row of the maze.
Output
For each testcase, print the answer in binary system. Please eliminate all the preceding
0 unless the answer itself is 0 (in this case, print 0 instead).
Sample Input
2
2 2
11
11
3 3
001
111
101
Sample Output
111
101
Author
XJZX
Source
解题:搜索,先看一直走0,走到最靠近终点的1,然后再从这个1所在半径,斜对角线似的搜索,
1 #include <bits/stdc++.h> 2 #define pii pair<int,int> 3 using namespace std; 4 const int maxn = 1005; 5 const int dir[4][2] = {1,0,0,1,-1,0,0,-1}; 6 char mp[maxn][maxn]; 7 int n,m,sx,sy; 8 queue< pii >q; 9 bool vis[maxn][maxn]; 10 bool isIn(int x,int y) { 11 return x >= 0 && x < n && y >= 0 && y < m; 12 } 13 void bfs(int x,int y) { 14 while(!q.empty()) q.pop(); 15 q.push(pii(x,y)); 16 vis[x][y] = true; 17 while(!q.empty()) { 18 pii now = q.front(); 19 q.pop(); 20 for(int i = 0; i < 4; ++i) { 21 int nx = now.first + dir[i][0]; 22 int ny = now.second + dir[i][1]; 23 if(!isIn(nx,ny) || vis[nx][ny]) continue; 24 vis[nx][ny] = true; 25 if(mp[nx][ny] == '0') q.push(pii(nx,ny)); 26 if(sx + sy < nx + ny){ 27 sx = nx; 28 sy = ny; 29 } 30 } 31 } 32 } 33 int main() { 34 int kase; 35 scanf("%d",&kase); 36 while(kase--) { 37 scanf("%d%d",&n,&m); 38 for(int i = 0; i < n; ++i) scanf("%s",mp[i]); 39 sx = sy = 0; 40 memset(vis,false,sizeof vis); 41 vis[0][0] = true; 42 if(mp[0][0] == '0') bfs(0,0); 43 if(mp[sx][sy] == '0') puts("0"); 44 else { 45 bool nowflag = false; 46 putchar('1'); 47 for(int i = sx + sy; i < n + m - 2; ++i){ 48 bool flag = false; 49 for(int k = 0; k <= i; ++k){ 50 int x = k; 51 int y = i - k; 52 if(!isIn(x,y) || !vis[x][y]) continue; 53 if(nowflag && mp[x][y] == '1') continue; 54 for(int j = 0; j < 2; ++j){ 55 int nx = x + dir[j][0]; 56 int ny = y + dir[j][1]; 57 if(!isIn(nx,ny)) continue; 58 vis[nx][ny] = true; 59 if(mp[nx][ny] == '0') flag = true; 60 } 61 } 62 nowflag = flag; 63 putchar(flag?'0':'1'); 64 } 65 putchar('\n'); 66 } 67 } 68 return 0; 69 }