HDU 5335 Walk Out
来源:互联网 发布:知乎36经典回复 编辑:程序博客网 时间:2024/06/07 16:47
Problem Description
In ann∗m maze, the right-bottom corner is the exit (position(n,m) is the exit). In every position of this maze, there is either a0 or a1 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 integerT (T=10) , indicating the number of testcases. For each testcase, the first line contains two integersn andm (1≤n,m≤1000) . Thei -th line of the nextn lines contains one 01 string of lengthm , which representsi -th row of the maze.
Output
For each testcase, print the answer in binary system. Please eliminate all the preceding0 unless the answer itself is0 (in this case, print0 instead).
Sample Input
22 211113 3001111101
Sample Output
111101
先用dfs判断最远的0可以到达的位置,然后按照斜行递推
#include<cstdio>#include<cstring>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<algorithm>#include<string>#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;using namespace std;const ll maxn=1005;int T,n,m,f[maxn][maxn],c[maxn][maxn],tot;char s[maxn][maxn];void dfs(int x,int y){ if (c[x][y]) return ; c[x][y]=1; if (s[x][y]=='1') return; f[x][y]=1; if(x+y>tot) tot=x+y; if (x>1) dfs(x-1,y); if (x<n) dfs(x+1,y); if (y>1) dfs(x,y-1); if (y<m) dfs(x,y+1);}int main(){ scanf("%d",&T); while (T--) { memset(f,0,sizeof(f)); memset(c,0,sizeof(c)); scanf("%d%d",&n,&m); tot=1; for (int i=1;i<=n;i++) { scanf("%s",s[i]+1); } dfs(1,1); if(tot==n+m) {printf("0\n");continue;} if(tot==1) { tot=2; f[1][1]=1; printf("%d",1); } for (int i=tot;i<n+m;i++) { int flag=1; for (int j=max(1,i-m);j<=min(n,i-1);j++) if (f[j][i-j]) { int x=j<n?s[j+1][i-j]-'0':1; int y=i-j<m?s[j][i-j+1]-'0':1; flag=min(flag,min(x,y)); } for (int j=max(1,i-m);j<=min(n,i-1);j++) if (f[j][i-j]) { int x=j<n?s[j+1][i-j]-'0':1; int y=i-j<m?s[j][i-j+1]-'0':1; if (x==flag) f[j+1][i-j]=1; if (y==flag) f[j][i-j+1]=1; } printf("%d",flag); } printf("\n"); } return 0;}
0 0
- hdu 5335 Walk Out
- HDU 5335 Walk Out
- HDU 5335 Walk Out
- hdu 5335 Walk Out
- hdu 5335 Walk Out
- hdu 5335 Walk Out
- HDU 5335 Walk Out
- HDU 5335 Walk Out
- HDU 5335 Walk Out
- hdu 5335 Walk Out【搜索】
- hdu 5335 Walk Out 搜索+贪心
- HDU 5335 Walk Out 状压DP乱搞
- hdu 5335 Walk Out(dfs+bfs)
- 【HDU 5335】Walk Out(BFS)
- HDU 5335 - Walk Out (DFS + 贪心)
- HDU 5335 walk out(特殊bfs)
- HDU 5335 Walk Out(搜索+贪心)
- hdu 5335 Walk Out(暴力)
- 程序员加入新团队的那些坑
- 可动态切换日历demo
- iOS支付宝支付步骤
- hdu 5335 Walk Out
- JSON详解
- HDU 5335 Walk Out
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- UI01_UIView
- BZOJ 2208 [Jsoi2010]连通数 tarjan缩点+bitset优化DP
- 怎样VS2013下安装Qt5的插件
- flexpaper全屏缩放后 浏览器滚动条失效
- 杭电 2141 Can you find it?二分法+暴力查找
- HDU 5336 XYZ and Drops
- RSA ENCRYPT