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). 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

题目大意:t组数据。每组n,m分别表示行和列。下面的n行m列用0、1表示地图情况。问你从(1,1)走到(n,m)所经过的点按顺序形成的二级制数最小是多少?

解题思路:先把所有的跟入口相连的0连通块都标记上。然后找到跟这个0连通块相邻接的1.这些1是可能的出发点。再在这些1中找到离出口最近的点。从这些点只需要往右往下走。因为当你确定离出口最近的点的时候,二进制数的位数已经确定。然后再广搜,贪心0。如果广搜时发现有0,那么将所有的0入队,如果没有发现0,那么将所有的1入队。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
const int INF=0x3f3f3f3f;
int vis[maxn][maxn];
int Map[maxn][maxn];
int way[2*maxn];
int f[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int n,m,k;
struct NODE{
    int x,y;
};
queue<NODE>Q;
stack<NODE>S;
int BFS(){
    NODE st,tmp;
    while(!Q.empty()){
        st=Q.front();
        Q.pop();
        int xx,yy;
        for(int i=0;i<4;i++){
            xx=st.x+f[i][0];
            yy=st.y+f[i][1];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(!vis[xx][yy])&&Map[xx][yy]==0){
                if(xx==n&&yy==m)
                    return -1;
                tmp.x=xx,tmp.y=yy;
                vis[xx][yy]=1;
                Q.push(tmp);
            }
        }
    }
}
bool check(int x,int y){
    int xx,yy;
    for(int i=0;i<4;i++){
        xx=x+f[i][0];
        yy=y+f[i][1];
        if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy]==1){
            return true;
        }
    }
    return false;
}
void Find1(int typ){
    while(!S.empty())
        S.pop();
    while(!Q.empty())
        Q.pop();
    int maxs=-INF;
    NODE st,tmp;

    if(typ==0){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(Map[i][j]==1){
                    if(check(i,j)){

                        if(i+j>maxs){
                            maxs=i+j;
                        }
                        st.x=i,st.y=j;
                        vis[i][j]=2;
                        S.push(st);
                    }
                }
            }
        }
    }else{
        st.x=1,st.y=1;
        vis[1][1]=2;
        maxs=2;
        S.push(st);
    }
    if(S.empty()==0)
        way[k++]=1;
    while(!S.empty()){
        st=S.top();
        S.pop();
        if(st.x+st.y==maxs){
            Q.push(st);
        }
    }
    while(1){
        int flag=0;
        while(!Q.empty()){
            st=Q.front();
            Q.pop();
            if(st.x==n&&st.y==m){
                return ;
            }
            int xx,yy;
            for(int i=0;i<2;i++){
                xx=st.x+f[i][0];
                yy=st.y+f[i][1];
                if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(!vis[xx][yy])){
                    if(Map[xx][yy]==0){
                        flag=1;
                    }
                    tmp.x=xx,tmp.y=yy;
                    vis[xx][yy]=2;
                    S.push(tmp);
                }
            }
        }
        if(flag==1){
            way[k++]=0;
            while(!S.empty()){
                st=S.top();
                S.pop();
                if(Map[st.x][st.y]==0){
                    Q.push(st);
                }
            }
        }else{
            way[k++]=1;
            while(!S.empty()){
                st=S.top();
                S.pop();
                Q.push(st);
            }
        }
    }
}
int main(){
    int t;
    char str[1020];
    scanf("%d",&t);
    while(t--){
        k=0;
        memset(vis,0,sizeof(vis));
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++){
           scanf("%s",str+1);
            for(int j=1;j<=m;j++){
                Map[i][j]=str[j]-‘0‘;
            }
        }
        if(n==m&&n==1&&Map[1][1]==0){
            printf("0\n");continue;
        }
        if(Map[1][1]==0){
            NODE st;
            st.x=1,st.y=1;
            vis[1][1]=1;
            while(!Q.empty())
                Q.pop();
            Q.push(st);
            if(BFS()==-1) {
                printf("0\n");
                continue;
            }
            Find1(0);
        }
        else{
            Find1(1);
        }
        for(int i=0;i<k;i++){
            printf("%d",way[i]);
        }printf("\n");
    }
    return 0;
}
/*
50
3 3
001
111
101

55
1 2
01

*/

  

时间: 2024-11-01 12:43:36

HDU 5335——Walk Out——————【贪心】的相关文章

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 (搜索+贪心,超详解)经典

题目链接: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

HDOJ 5335 Walk Out 贪心+BFS

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 (p

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

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

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 (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(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 want

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)