[bfs+贪心] 多校第四场 HDU5335 Walk Out

Walk Out

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1224 Accepted Submission(s): 229

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

首先很明显可以得到,在前导0存在时,在0上走到与终点距离尽量近的位置,就可以让得到的二进制位尽量小。
首先用bfs判断从开始时0能达到距离终点最近的位置,具体的做法是开bfs遍历,把每一个点的位置遍历一遍,目的是计算出每一个可达的点的与终点的距离。完成bfs以后,可以得到几个与终点距离最近的点。
然后从这些点出发,贪心地向右,向下选取尽量0多的位置即可。

#pragma comment(linker, "/STACK:102400000,102400000")  
#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<vector>  
#include<queue>  
using namespace std;  
#define maxn 1001  

char ma[maxn][maxn];  
int check[maxn][maxn];  
int dep[maxn][maxn];  

struct Node{  
    int x,y;  
    Node(int _x=0,int _y=0):x(_x),y(_y){}  
};  

queue<Node> que;  
vector<int> ans;  
vector<Node> one;  
vector<Node> zero;  
int n,m;  
void dfs(int x,int y){  
    if(check[x][y] || x  == n || y == m || x < 0 || y < 0 || ma[x][y] == '1') return;  
    que.push(Node(x,y));  
    check[x][y] = 1;  
    dfs(x+1,y);  
    dfs(x,y+1);  
    dfs(x-1,y);  
    dfs(x,y-1);  
}  
void work(){  
    while(que.size() > 0)  
        que.pop();  
    if(ma[0][0] == '1'){  
        check[0][0] = 1;  
        que.push(Node(0,0));  
    }  
    else {  
        dfs(0,0);  
    }  
}  
void bfs(){  
    Node a;  
    while(que.size() > 0){  
        a = que.front();  
        que.pop();  
        if(a.x + 1 < n && a.y < m && check[a.x+1][a.y] == 0){  
            que.push(Node(a.x+1,a.y));  
            check[a.x+1][a.y] = 1;  
            dep[a.x+1][a.y] = dep[a.x][a.y] + 1;  
        }  
        if(a.x < n && a.y + 1 < m && check[a.x][a.y+1] == 0){  
            que.push(Node(a.x,a.y+1));  
            check[a.x][a.y+1] = 1;  
            dep[a.x][a.y+1] = dep[a.x][a.y] + 1;  
        }  
    }  
}  
int mark[maxn][maxn];  
void nidfs(int x,int y){  
    mark[x][y] =  1;  
    if(x-1>=0 && y >= 0 && mark[x-1][y] == 0 && dep[x-1][y] == dep[x][y] - 1 && dep[x-1][y] > 0)  
        nidfs(x-1,y);  
    if(x >= 0 && y-1>=0 && mark[x][y-1] == 0 && dep[x][y-1] == dep[x][y] - 1 && dep[x][y-1] > 0)  
        nidfs(x,y-1);  
}  

void getans(){  
    ans.clear();  
    Node a;  
    while(1){  
        one.clear();  
        zero.clear();  
        while(que.size() > 0){  
            a = que.front();  
            que.pop();  
            if(a.x == n -1 && a.y == m -1 ) return ;  
            if(a.x + 1 < n && a.y < m && check[a.x+1][a.y] == 0&& dep[a.x+1][a.y] == dep[a.x][a.y] + 1 && mark[a.x+1][a.y]){  
                if(ma[a.x+1][a.y] == '1') one.push_back(Node(a.x+1,a.y));  
                else zero.push_back(Node(a.x+1,a.y));  
                check[a.x+1][a.y] = 1;  
            }  
            if(a.x < n && a.y + 1< m && check[a.x][a.y+1] == 0 && dep[a.x][a.y+1] == dep[a.x][a.y] + 1 && mark[a.x][a.y+1]){  
                if(ma[a.x][a.y+1] == '1') one.push_back(Node(a.x,a.y+1));  
                else zero.push_back(Node(a.x,a.y+1));  
                check[a.x][a.y+1] = 1;  
            }  
        }  
        if(zero.size() > 0){  
            ans.push_back(0);  
            for(int i = 0;i < zero.size();i++)  
                que.push(zero[i]);  
        }  
        else {  
            ans.push_back(1);  
            for(int i = 0;i < one.size();i++)  
                que.push(one[i]);  
        }  
    }  
}  
int main(){  
    int t;  
    scanf("%d",&t);  
    while(t--){  
        scanf("%d%d",&n,&m);  
        for(int i = 0;i < n; i++){  
            scanf("%s",ma[i]);  
        }  
        memset(check,0,sizeof(check));  
        work();  
        memset(dep,0,sizeof(dep));  
        bfs();  
// for(int i = 0;i < n; i++){  
//            for(int j = 0;j < m; j++){  
//                printf("%d ",dep[i][j]);  
//            }cout<<endl;  
//        }  
//        cout<<endl;  
        memset(mark,0,sizeof(mark));  
        nidfs(n-1,m-1);  

        memset(check,0,sizeof(check));  
        work();  
        getans();  
//        for(int i = 0;i < n; i++){  
//            for(int j = 0;j < m; j++){  
//                printf("%d ",dep[i][j]);  
//            }cout<<endl;  
//        }  
        if(ans.size() == 0 && ma[0][0] =='0'){  
            printf("0\n");  
        }  
        else {  
            if(ma[0][0] == '1') printf("1");  
            for(int i = 0;i < ans.size(); i++)  
                printf("%d",ans[i]);  
            printf("\n");  
        }  

    }  
    return 0;  

}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值