HDU 5335 Walk Out(搜索+贪心)

Walk OutTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2810Accepted Submission(s): 571

Problem Description

In anmaze, the right-bottom corner is the exit (positionis the exit). In every position of this maze, there is either aor awritten 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. 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, indicating the number of testcases.For each testcase, the first line contains two integers. The-th line of the nextlines contains one 01 string of length, which represents-th row of the maze.

Output

For each testcase, print the answer in binary system. Please eliminate all the precedingunless the answer itself is(in this case, printinstead).

Sample Input

22 211113 3001111101

Sample Output

111101

Author

XJZX

Source

题意:在一个n*m的矩阵中只有0和1两种数字,,现在要求从左上角到达右下角形成的二进制数最小,并且遇到第一个1之前的0可以去掉(例如000010写成10)

思路:用BFS找离右下角最近的只包含0的一条路,找到以后贪心,在离右下角相同距离的数字中,如果有0的存在就只算0的情况,没有0再算1的情况

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>using namespace std;int n,m;char map[1010][1010];int v[1010][1010];int num[2010];int jx[] = {1,0,0,-1};int jy[] = {0,1,-1,0};int xx[] = {0,1};int yy[] = {1,0};struct node{int x;int y;int z;} q[1000010],a[1010];void BFS(){int h = 0;int pt = 0;memset(v,0,sizeof(v));for(int i=0;i<2008;i++){num[i] = -1;}struct node t,f;int maxx = 0;int s = 0,e = 0;t.x = 0;t.y = 0;t.z = 0;if(map[0][0] == '0'){h = 0;q[e++] = t;v[t.x][t.y] = 1;while(s<e){t = q[s++];for(int i=0; i<4; i++){f.x = t.x + jx[i];f.y = t.y + jy[i];f.z = t.z + 1;if(f.x>=0 && f.x<n && f.y>=0 && f.y<m && v[f.x][f.y] == 0){v[f.x][f.y] = 1;if(map[f.x][f.y] == '0'){if(f.x == n-1 && f.y == m-1){printf("0\n");return ;}q[e++] = f;}else{if(f.x+f.y>=maxx){if((f.x+f.y) == maxx){a[h].x = f.x;a[h].y = f.y;a[h].z = f.z;h++;}else{maxx = f.x + f.y;h = 0;a[h].x = f.x;a[h].y = f.y;a[h].z = f.z;h++;}}}}}}}else{h = 0;a[h].x = 0;a[h].y = 0;a[h].z = 0;h++;//maxx = 1;}int pk = 0;num[0] = 1;for(int i=0;i<h;i++){if(a[i].x == n-1 && a[i].y == m-1){printf("1\n");return ;}}int ff = 0;while(ff == 0){pk++;pt = 0;int flag = 0;for(int i=0; i<h; i++){t = a[i];for(int j=0; j<2; j++){f.x = t.x + xx[j];f.y = t.y + yy[j];f.z = t.z + 1;if(f.x>=0 && f.x<n && f.y>=0 && f.y<m && v[f.x][f.y] == 0 && ff == 0){v[f.x][f.y] = 1;if(map[f.x][f.y] == '1' && flag == 0){q[pt++] = f;}else if(map[f.x][f.y] == '0'){if(flag == 0){flag = 1;pt = 0;q[pt++] = f;}else{q[pt++] = f;}}if(f.x == n-1 && f.y == m-1){ff = 1;}}}}if(flag == 0){num[pk] = 1;}else{num[pk] = 0;}h = pt;for(int i=0; i<pt; i++){a[i] = q[i];}}for(int i=0; i<=pk; i++){printf("%d",num[i]);}printf("\n");}int main(){int T;scanf("%d",&T);while(T–){scanf("%d%d",&n,&m);for(int i=0; i<n; i++){scanf("%s",map[i]);}if(n == 1 && m == 1 && map[0][0] == '0'){printf("0\n");continue;}BFS();}return 0;}/*#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<iostream>#include<algorithm>#include<bitset>#include<climits>#include<list>#include<iomanip>#include<stack>#include<set>using namespace std;int tor[4]={-1,1,0,0};int toc[4]={0,0,-1,1};struct point{int r,c;point(){}point(int r,int c){this->r=r;this->c=c;}};char s[1010][1010];bool vis[1010][1010];point q[2][1000010];int len[2];int n,m,x;bool beyond(int r,int c){return r<0||c<0||r>=n||c>=m;}void seek(){memset(len,0,sizeof(len));q[0][len[0]++]=point(0,0);if(s[0][0]=='1'){x=1;return;}x=0;q[1][len[1]++]=point(0,0);memset(vis,0,sizeof(vis));vis[0][0]=1;int mx=0;while(len[1]>0){point from=q[1][–len[1]];int r=from.r,c=from.c;for(int i=0;i<4;i++){int tr=r+tor[i],tc=c+toc[i];if(beyond(tr,tc)||vis[tr][tc]||s[tr][tc]!='0')continue;vis[tr][tc]=1;int t=tr+tc;if(t>=mx){if(t>mx)len[0]=0;mx=t;q[0][len[0]++]=point(tr,tc);}q[1][len[1]++]=point(tr,tc);}}}int ans[10000];int bfs(){bool p=0;memset(vis,0,sizeof(vis));for(int i=0;;i++){int j=i%2;bool flag=0;while(len[j]>0){len[j]–;int r=q[j][len[j]].r,c=q[j][len[j]].c;if(s[r][c]=='1'&&p)continue;for(int k=0;k<4;k++)if(k&1){int tr=r+tor[k],tc=c+toc[k];if(beyond(tr,tc)||vis[tr][tc])continue;vis[tr][tc]=1;q[j^1][len[j^1]++]=point(tr,tc);if(s[tr][tc]=='0')flag=1;}}p=flag;ans[i]=flag?0:1;if(vis[n-1][m-1])return i;}}int main(){int T;scanf("%d",&T);while(T–){scanf("%d%d",&n,&m);for(int i=0;i<n;i++)scanf("%s",s[i]);seek();if(x==0&&vis[n-1][m-1]){printf("0\n");continue;}if(x==1){putchar('1');if(n==1&&m==1){puts("");continue;}}int lg=bfs();for(int i=0;i<=lg;i++)printf("%d",ans[i]);puts("");}return 0;}*/

版权声明:本文为博主原创文章,如有特殊需要请与博主联系 QQ : 793977586。

而其实你还爱着他,你一点也不好。

HDU 5335 Walk Out(搜索+贪心)

相关文章:

你感兴趣的文章:

标签云: