zoukankan      html  css  js  c++  java
  • 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
    "); 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("
    ");
        }
        return 0;
    } 
                
    

      

  • 相关阅读:
    学习——HTML5
    XML解析——SAX解析以及更方便的解析工具(JDOM、DOM4J)
    C#笔记 -----扩展方法
    Socket实现异步通信
    基础套接字的C#网络编程
    线程
    sqlserver 索引
    SQL Server用户自定义函数
    sqlserver函数
    用户控件和自定义控件的区别
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4694125.html
Copyright © 2011-2022 走看看