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;
} 
            

  

 

posted @ 2015-08-01 15:08  Painting、时光  阅读(125)  评论(0编辑  收藏  举报