Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2001 Accepted Submission(s): 376
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
从矩阵左上角走到右下角,而且经过的数字会被以二进制的方式记录下来,求最小
思路:如果第一个位置是1,只能一直走下 或者 右,找出最小值
如果第一个位置是0,找到离右下角最近的1,然后进行上面步骤
但是当时并没有实现,超时了,感觉有点麻烦 - -,用搜索不知道怎么记录值,而且容易超
标码中的用for循环求解,感觉好机智(⊙o⊙)…,找到最近的位置 x + y,找出这些中最小的值输出,再讲它们进行标记。
P:每次离右下角i + j的点是那些固定的,在这些固定的周围继续找。
必须扩充栈用C++,否则爆掉。 自己测试时就说怎么一直有问题 TAT
#include
#include
#include
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
int T, n, m,tot;
char S[1100][1100];
bool used[1100][1100];
char ch = '0';
char ch1 = '1';
void bfs(int x,int y)
{
if(used[x][y])
return ;
used[x][y] = true;
if(S[x][y] == ch1)
return;
if (x>1) bfs(x-1,y);
if (x1) bfs(x,y-1);
if (y