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 or a written on it. An explorer gets lost in this grid. His position now is (,), 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=), indicating the number of testcases. For each testcase, the first line contains two integers n and m (≤n,m≤). The i-th line of the next n lines contains one 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 unless the answer itself is (in this case, print instead).
Sample Input
Sample Output
Author
XJZX
Source
1、如果mp[0][0]==0的话,先bfs一次,将前导0去掉。bfs得到了wx,wy
2、然后就是找最小路径了。枚举步数for(int i=wx+wy;i<n+m-2;i++),在这个步数的基础上枚举所有可能的(x,y)x+y==step,如果遇到0则取0,一直找到(n-1,m-1)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stdlib.h>
using namespace std;
#define N 1006
int n,m;
int mp[N][N];
int vis[N][N];
struct Node{
int x,y;
}st;
int dirx[]={,,-,};
int diry[]={,,,-};
int wx,wy;
void bfs(){
queue<Node>q;
q.push(st);
Node t1,t2;
while(!q.empty()){
t1=q.front();
q.pop();
for(int i=;i<;i++){
t2.x=t1.x+dirx[i];
t2.y=t1.y+diry[i];
if(vis[t2.x][t2.y]) continue;
if(t2.x< || t2.x>=n || t2.y< || t2.y>=m) continue;
vis[t2.x][t2.y]=;
if(mp[t2.x][t2.y]==){
q.push(t2);
}
if(wx+wy<t2.x+t2.y){
wx=t2.x;
wy=t2.y;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
char s[];
for(int i=;i<n;i++){
scanf("%s",s);
for(int j=;j<m;j++){
mp[i][j]=s[j]-'';
}
}
st.x=;
st.y=;
memset(vis,,sizeof(vis));
vis[][]=;
wx=;
wy=;
if(mp[][]==){
bfs();
}
if(mp[wx][wy]==){
printf("0\n");
continue;
}
printf("");
int nowflag=;
for(int i=wx+wy;i<n+m-;i++){
int flag=;
int step=i;
for(int j=;j<=step;j++){
int x=j;
int y=i-j;
if(x< || x>=n || y< || y>=m) continue;
if(nowflag && mp[x][y]) continue;
if(!vis[x][y]) continue; for(int k=;k<;k++){
int sx=x+dirx[k];
int sy=y+diry[k];
if(sx< || sx>=n || sy< || sy>=m) continue;
vis[sx][sy]=;
if(mp[sx][sy]==){
flag=;
}
} }
nowflag=flag;
if(flag){
printf("");
}
else{
printf("");
}
}
printf("\n");
}
return ;
}