hdu5335

Walk Out

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


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
 

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>

using namespace std;

char mp[1005][1005];
int vis[1005][1005];

int ans[20005];

int dir[4][2]= {0,1,0,-1,1,0,-1,0};

struct node
{
    int x,y;
    int f;
};


vector<node> v;
int maxn;

int n,m;
bool check(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0)
    {
        return true;
    }
    else
        return false;
}

void bfs()
{
    queue<node> q;
    node sta;
    sta.x=0;
    sta.y=0;

    vis[0][0]=1;
    if(mp[0][0]=='0')
    {
        q.push(sta);

    }
    else
    {
        maxn=0;
        v.push_back(sta);
    }

    while(!q.empty())
    {
        node cur=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int xx=cur.x+dir[i][0];
            int yy=cur.y+dir[i][1];
            if(check(xx,yy))
            {
                node nex;
                nex.x=xx;
                nex.y=yy;

                vis[xx][yy]=1;
                if(mp[xx][yy]=='0')
                {
                    q.push(nex);
                    if((xx+yy)>maxn)
                    {
                        v.clear();
                        maxn=xx+yy;
                    }
                }
                else
                {
                    if((xx+yy)==maxn)
                    {
                        v.push_back(nex);
                    }
                    else if((xx+yy)>maxn)
                    {
                        v.clear();
                        maxn=xx+yy;
                        v.push_back(nex);
                    }
                }



            }


        }

    }

}

int cnt;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        v.clear();
        maxn=0;
        memset(vis,0,sizeof(vis));
        cnt=0;

        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
            scanf("%s",mp[i]);

        bfs();

//cout<<v.size()<<endl;
        /*for(int i=0;i<v.size();i++)
        {
            cout<<v[i].x<<" "<<v[i].y<<endl;
        }
        */
        queue<node> qq;

        while(1)
        {

            int len=v.size();
            if(len==0)
                break;

            int minn=1;
            for(int i=0; i<len; i++)
            {
                node p=v[i];
                int x=v[i].x;
                int y=v[i].y;

                int xx=x+1;
                int yy=y;

                if(check(xx,yy))
                {
                    if(mp[xx][yy]=='0')
                        minn=0;

                    node nex;
                    nex.x=xx;
                    nex.y=yy;
                    nex.f=mp[xx][yy]-'0';
                    qq.push(nex);
                    vis[xx][yy]=1;
                }

                xx=x;
                yy=y+1;

                if(check(xx,yy))
                {
                    if(mp[xx][yy]=='0')
                        minn=0;

                    node nex;
                    nex.x=xx;
                    nex.y=yy;
                    nex.f=mp[xx][yy]-'0';
                    qq.push(nex);
                    vis[xx][yy]=1;
                }



            }

            ans[cnt++]=minn;
            v.clear();
            //cout<<v.size()<<endl;
            while(!qq.empty())
            {
                node cur=qq.front();
                qq.pop();

                if(cur.f==minn)
                    v.push_back(cur);
            }


        }
        if(cnt!=0)
        {
            printf("1");
            for(int i=0; i<cnt-1; i++)
                printf("%d",ans[i]);
        }

        if(cnt==0)
            printf("0");
        printf("\n");
    }
}


/*
4
3 3
001
010
001
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值