HDU5335——贪心+BFS——Walk Out

Problem Description
In an  nm 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 (1n,m1000). 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
 
/*
题意:从左上到右下要求二进制最小,删去前置0
贪心+bfs
先用bfs求出前置0的长度
再以距离为状态进行贪心
*/
#include <bits/stdc++.h>
using namespace std;

int dirx[] = {1, -1, 0, 0};
int diry[] = {0, 0, 1, -1};
const int MAX = 1000 + 10;
char a[MAX][MAX];
int vis[MAX][MAX];
int b[MAX][MAX];
typedef struct {
    int x, y;
}P;
queue <P> q;
int n, m;

int main()
{
    int T;
    scanf("%d", &T);
    while(T--){
    scanf("%d%d", &n, &m);
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; i++)
        scanf("%s", a[i]+1);
    for(int i = 0; i <= n; i++)
        b[i][0] = b[i][m+1] = 2;
    for(int i = 0; i <= m; i++)
        b[0][i] = b[n+1][i] = 2;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            b[i][j] = a[i][j]-'0';
        }
    }
    vis[1][1] = 1;
    if(b[1][1] == 0){
        while(!q.empty()) q.pop();
        q.push((P){1, 1});
        while(!q.empty()){
            P now = q.front();
            q.pop();
            int x = now.x;
            int y = now.y;
            for(int i = 0 ; i< 4; i++){
                int X = x + dirx[i], Y = y + diry[i];
                if(X >= 1 && X <= n && Y >= 1 && Y <= m && !vis[X][Y] && b[X][Y] == 0){
                    vis[X][Y] = 1;
                    q.push((P){X, Y});
                }
            }
        }
    }
   // for(int i = 1; i <= n; i++){
   //     for(int j = 1; j <= m; j++)
   //         printf("%d",vis[i][j]);
   //     puts("");
   // }
    if(vis[n][m] && b[n][m] == 0) {
        printf("0\n"); continue;
    }
    if(b[1][1] == 1) printf("1");
    int max_dis = 2;
     for(int i = 1; i <= n ; i++)
        for(int j = 1; j <= m; j++)
            if(vis[i][j])
              max_dis = max(max_dis, i+j);
    for(int i = max_dis; i < n + m; i++){
        int min1 = 1;
        for(int j = 1; j <= n; j++)
            if(i - j >= 1 && i - j <= m && vis[j][i-j])
                min1 = min(min1,min(b[j+1][i-j], b[j][i-j+1]));
                    printf("%d", min1);
        for(int j = 1; j <= n; j++)
            if(i - j >= 1 && i - j <= m && vis[j][i-j]){
                if(b[j+1][i-j] == min1) vis[j+1][i-j] = 1;
                if(b[j][i-j+1] == min1) vis[j][i-j+1] = 1;
            }
    }
    printf("\n");
    }
    return 0;
} 
            

  

 

转载于:https://www.cnblogs.com/zero-begin/p/4694125.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值