BFS沿着0走,记录下最靠近终点的1
然后斜着扫描
Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2573 Accepted Submission(s): 506
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
111 101
Author
XJZX
Source
2015 Multi-University Training Contest 4
/* *********************************************** Author :CKboss Created Time :2015年08月01日 星期六 20时06分43秒 File Name :HDOJ5335_2.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; typedef pair<int,int> pII; const int maxn=1100; int n,m; char mp[maxn][maxn]; bool vis[maxn][maxn]; const int dir_x[4]={1,-1,0,0}; const int dir_y[4]={0,0,1,-1}; bool OK(int x,int y) { if(vis[x][y]==true) return false; if((x>=0&&x<n)&&(y>=0&&y<m)) return true; return false; } bool inside(int x,int y) { if((x>=0&&x<n)&&(y>=0&&y<m)) return true; return false; } void BFS() { int mx=0; queue<pII> q; if(mp[0][0]=='0') q.push(make_pair(0,0)); vis[0][0]=true; while(!q.empty()) { pII u=q.front(); q.pop(); for(int i=0;i<4;i++) { int x=u.first+dir_x[i]; int y=u.second+dir_y[i]; if(OK(x,y)==false) continue; vis[x][y]=true; if(mp[x][y]=='0') { q.push(make_pair(x,y)); } else if(mp[x][y]=='1') { mx=max(mx,x+y); } } } if(vis[n-1][m-1]==true) { putchar(mp[n-1][m-1]); putchar(10); return ; } for(int i=mx;i<n+m-1;i++) { char ch='1'; for(int x=0;x<n;x++) { int y=i-x; if(inside(x,y)==false||vis[x][y]==false) continue; ch=min(ch,mp[x][y]); } putchar(ch); for(int x=0;x<n;x++) { int y=i-x; if(inside(x,y)==false||vis[x][y]==false) continue; if(mp[x][y]!=ch) continue; if(inside(x+1,y)==true) vis[x+1][y]=true; if(inside(x,y+1)==true) vis[x][y+1]=true; } } putchar(10); } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T_T; scanf("%d",&T_T); while(T_T--) { scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%s",mp[i]); memset(vis,false,sizeof(vis)); BFS(); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。