hdu 5335 Walk Out(bfs+寻找路径)

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

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)

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<queue>
  5 #include<stdlib.h>
  6 using namespace std;
  7 #define N 1006
  8 int n,m;
  9 int mp[N][N];
 10 int vis[N][N];
 11 struct Node{
 12     int x,y;
 13 }st;
 14 int dirx[]={1,0,-1,0};
 15 int diry[]={0,1,0,-1};
 16 int wx,wy;
 17 void bfs(){
 18     queue<Node>q;
 19     q.push(st);
 20     Node t1,t2;
 21     while(!q.empty()){
 22         t1=q.front();
 23         q.pop();
 24         for(int i=0;i<4;i++){
 25             t2.x=t1.x+dirx[i];
 26             t2.y=t1.y+diry[i];
 27             if(vis[t2.x][t2.y]) continue;
 28             if(t2.x<0 || t2.x>=n || t2.y<0 || t2.y>=m) continue;
 29             vis[t2.x][t2.y]=1;
 30             if(mp[t2.x][t2.y]==0){
 31                 q.push(t2);
 32             }
 33             if(wx+wy<t2.x+t2.y){
 34                 wx=t2.x;
 35                 wy=t2.y;
 36             }
 37         }
 38     }
 39 }
 40 int main()
 41 {
 42     int t;
 43     scanf("%d",&t);
 44     while(t--){
 45         scanf("%d%d",&n,&m);
 46         char s[1006];
 47         for(int i=0;i<n;i++){
 48             scanf("%s",s);
 49             for(int j=0;j<m;j++){
 50                 mp[i][j]=s[j]-‘0‘;
 51             }
 52         }
 53         st.x=0;
 54         st.y=0;
 55         memset(vis,0,sizeof(vis));
 56         vis[0][0]=1;
 57         wx=0;
 58         wy=0;
 59         if(mp[0][0]==0){
 60             bfs();
 61         }
 62         if(mp[wx][wy]==0){
 63             printf("0\n");
 64             continue;
 65         }
 66         printf("1");
 67         int nowflag=0;
 68         for(int i=wx+wy;i<n+m-2;i++){
 69             int flag=0;
 70             int step=i;
 71             for(int j=0;j<=step;j++){
 72                 int x=j;
 73                 int y=i-j;
 74                 if(x<0 || x>=n || y<0 || y>=m) continue;
 75                 if(nowflag && mp[x][y]) continue;
 76                 if(!vis[x][y]) continue;
 77
 78                 for(int k=0;k<2;k++){
 79                     int sx=x+dirx[k];
 80                     int sy=y+diry[k];
 81                     if(sx<0 || sx>=n || sy<0 || sy>=m) continue;
 82                     vis[sx][sy]=1;
 83                     if(mp[sx][sy]==0){
 84                         flag=1;
 85                     }
 86                 }
 87
 88             }
 89             nowflag=flag;
 90             if(flag){
 91                 printf("0");
 92             }
 93             else{
 94                 printf("1");
 95             }
 96         }
 97         printf("\n");
 98     }
 99     return 0;
100 }

时间: 2024-11-17 05:43:02

hdu 5335 Walk Out(bfs+寻找路径)的相关文章

HDU 5335 Walk Out (BFS,技巧)

题意:有一个n*m的矩阵,每个格子中有一个数字,或为0,或为1.有个人要从(1,1)到达(n,m),要求所走过的格子中的数字按先后顺序串起来后,用二进制的判断大小方法,让这个数字最小.前缀0不需要输出!! 思路:主要考虑的是BFS解决. 如果grid[1,1]=1,那么这个二进制的位数也就定下来了,是n+m-1,很好解决,每个格子就只能往下或者往右,否则长度一定超过n+m+1,必定不是最优. 如果grid[1,1]=0,那么可能会出现绕了一个S型到终点的结果为0而已.所以不能用老办法,要先预处理

hdu 5335 Walk Out (搜索)

题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走的路线上的0/1组成的二进数最小,现在要为矫情无比的探险家找最优路径咯. 解题思路: 对于二进制数,前导零是对数字大小没有任何影响的.当到不得不走1的时候就只能向下,或者向右走了.所以先搜索出来一直走零,能走到的最靠近终点的位置,然后在类似搜索,找出最优路径. 1 #include <queue>

HDU 5335 Walk Out(Bfs搜索字典序最小的最短路)

 题意:nXm的地图, 问通过四个方向从(1,1)走到(1000,1000)所经过的最小二进制序列是多少,忽略前缀0. 思路:首先如果起点为0,那么我们bfs搜索和起点0联通的为0的连通块,这样我们第一步肯定是从与这个连通块相邻的且与重点最近的地方出发. 将所有可能起点加入队列,在bfs一遍找到字典序最小的那条路就是答案, 在这里可以用两个vector类型容器,一个是q2存储所有节点值存为0的结点, 另一个q3存储节点值为1的结点. 那么如果q2不为空那么也就是有可以走零,那么就从这里面选,

HDU 5335 Walk Out (搜索+贪心,超详解)经典

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5335 题面: Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2355    Accepted Submission(s): 459 Problem Description In an n?m maze, the righ

HDU 5335 Walk Out(多校)

Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2912    Accepted Submission(s): 599 Problem Description In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit).

HDU 5335——Walk Out——————【贪心】

Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1292    Accepted Submission(s): 239 Problem Description In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit).

HDU 5335 Walk Out

题意:在一个只有0和1的矩阵里,从左上角走到右下角, 每次可以向四个方向走,每个路径都是一个二进制数,求所有路径中最小的二进制数. 解法:先bfs求从起点能走到离终点最近的0,那么从这个点起只向下或向右走就可以获得位数最少的二进制数,然后贪心的想,如果后或下有0就一定走0,没0就把1都看一遍,以之前搜到的0做起点,一层一层遍历可行路径,直到终点. 代码: #include<stdio.h> #include<iostream> #include<algorithm> #

HDU 5335 多校第4场 1009 Walk Out

Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1794    Accepted Submission(s): 340 Problem Description In an n?m maze, the right-bottom corner is the exit (position (n,m) is the exit)

HDU1026--Ignatius and the Princess I(BFS记录路径)

Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrint