Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2534 Accepted Submission(s): 494
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.
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.
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
Author
XJZX
Source
2015 Multi-University Training Contest 4
Recommend
wange2014 | We have carefully selected several similar problems for you: 5338 5337 5334 5333 5332
#include <bits/stdc++.h>
#define maxn 1000 + 10
using namespace std;
typedef pair<int , int> P;
int go[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1} };
int T, n, m;
int vis[maxn][maxn];
char s[maxn][maxn];
int sx, sy;
int judge(int x, int y)
{
if(x > 0 && y > 0 && x <= n && y <= m)
return 1;
return 0;
}
void bfs()
{
queue<P> Q;
Q.push(P(1, 1));
while(!Q.empty())
{
P p = Q.front();
Q.pop();
for(int i = 0; i < 4; i++)
{
int x, y;
x = p.first + go[i][0];
y = p.second + go[i][1];
// printf("%d--%d\n", x, y);
if(!judge(x, y) || vis[x][y]) continue;
vis[x][y] = 1;
if(s[x][y] == '0')
Q.push(P(x, y));
if(sx + sy < x + y)
{
sx = x;
sy = y;
}
}
}
//cout<<sx<<" * "<<sy<<endl;
}
int main()
{
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i= 1; i <= n; i++)
scanf("%s" , s[i] + 1 );
memset(vis, 0, sizeof vis);
sx = sy = 1;
vis[1][1] = 1;
if(s[1][1] == '0') bfs();
if(s[sx][sy] == '0') putchar('0');
else
{
int f1 , f2 ;
f1 = f2 = 0;
putchar('1');
//cout<<sx<<" "<<sy<<endl;
for(int step = sx + sy; step < n + m; step++)
{
int x, y;
f1 = 0;
for(x = 1; x <= n; x++)
{
y = step - x;
if(!judge(x, y) || !vis[x][y]) continue;
if(f2 && s[x][y] == '1') continue;
int xx, yy;
for(int i = 0; i < 2; i++)
{
xx = x + go[i][0];
yy = y + go[i][1];
if(!judge(xx, yy)) continue;
vis[xx][yy] = 1;
if(s[xx][yy] == '0') f1 = 1;
}
}
f2 = f1;
putchar(f1 ? '0' : '1');
}
}
putchar('\n');
}
return 0;
}