zoukankan      html  css  js  c++  java
  • 多校4-Walk Out 分类: 比赛 2015-08-02 17:15 21人阅读 评论(0) 收藏

    Walk Out

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

    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
    经过一下午的努力,终于AC了,还是要靠特殊的数据测题啊
    加组数据
    1
    5 5
    00000
    11110
    00000
    01111
    00000
    Sample Output
    0

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <map>
    #include <queue>
    #include <stack>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <algorithm>
    #define INF 0x3f3f3f3f
    #define PI acos(-1.0)
    #define eps 1e-9
    #define WW freopen("output","w",stdout)
    #define RR freopen("input","r","stdin")
    
    using namespace std;
    
    const int MAX = 1010;
    
    struct node
    {
        int x;
        int y;
    } q[2*MAX];
    
    char str[MAX][MAX];
    bool vis[MAX][MAX];
    int n,m;
    int top;
    int sum;
    int dir[][2]= {{0,1},{1,0},{0,-1},{-1,0}};
    bool Judge(int x,int y)
    {
        if(x>=0&&x<n&&y>=0&&y<m)
        {
            return true;
        }
        return false;
    }
    void bfs()//如果str[0][0]=='0',则找到一个通过一系列的零可以到达且离终点最近
    {
        memset(vis,false,sizeof(vis));
        queue<node>Q;
        node a,b;
        a.x=0;
        a.y=0;
        Q.push(a);
        vis[0][0]=true;
        while(!Q.empty())
        {
            b=Q.front();
            Q.pop();
            for(int i=0; i<4; i++)
            {
                a.x=b.x+dir[i][0];
                a.y=b.y+dir[i][1];
                if(!Judge(a.x,a.y)||vis[a.x][a.y])
                {
                    continue;
                }
                if(str[a.x][a.y]=='1')
                {
                    if(a.x+a.y>sum)
                    {
                        top=0;
                        q[top].x=a.x;
                        q[top].y=a.y;
                        top++;
                        sum=a.x+a.y;
                    }
                    else if(a.x+a.y==sum)
                    {
                        q[top].x=a.x;
                        q[top].y=a.y;
                        top++;
                    }
                }
                else
                {
                    if(a.x+a.y==n+m-2)//重要的一点,因为开始没有写WA了好几次。
                    {
                        top=0;
                        return ;
                    }
                    Q.push(a);
                }
                vis[a.x][a.y]=true;
            }
        }
    }
    
    void DFS()
    {
        memset(vis,false,sizeof(vis));
        queue<node>q0;
        queue<node>q1;
        node a,b;
        for(int i=0; i<top; i++)
        {
            vis[q[i].x][q[i].y]=true;
            q0.push(q[i]);
        }
        printf("1");
        if(vis[n-1][m-1])
        {
            return ;
        }
        while(1)
        {
            int flag=1;
            while(!q0.empty())
            {
                b=q0.front();
                q0.pop();
                for(int i=0; i<2; i++)
                {
                    a.x=b.x+dir[i][0];
                    a.y=b.y+dir[i][1];
                    if(!Judge(a.x,a.y)||vis[a.x][a.y])
                    {
                        continue;
                    }
                    if(str[a.x][a.y]=='0')
                    {
                        flag=0;
                    }
                    q1.push(a);
                    vis[a.x][a.y]=true;
    
                }
            }
    
            printf("%d",flag);
    
            if(vis[n-1][m-1])
            {
    
                return ;
            }
            while(!q1.empty())
            {
                b=q1.front();
                q1.pop();
                if(flag)
                {
                    q0.push(b);
                }
                else
                {
                    if(str[b.x][b.y]=='0')
                    {
                        q0.push(b);
                    }
                }
            }
        }
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d %d",&n,&m);
            top=0;
            sum=0;
            for(int i=0; i<n; i++)
            {
                scanf("%s",str[i]);
            }
            if(str[0][0]=='1')
            {
                q[0].x=0;
                q[0].y=0;
                top++;
            }
            else
            {
                bfs();
            }
            if(top==0)
            {
                printf("0");
            }
            else
            {
                DFS();
            }
            printf("
    ");
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    端口监听与telnet
    strace
    Java Web SSH框架总是无法写入无法读取Cookie
    很反感Java Web 三层框架
    关于武侠游戏的一些想法(长期整理)
    奇怪的Lisp和难懂的计算机程序的构造和解释
    编程的智慧(转自王垠个人博客)——满满的编程实践经验不看就错过
    最近买了个Mac Pro,用起来感觉是去年买了个表
    Python手动构造Cookie模拟登录后获取网站页面内容
    奇怪的JS正则之 /[A-z]/.test("\"); // true
  • 原文地址:https://www.cnblogs.com/juechen/p/4721938.html
Copyright © 2011-2022 走看看