hdu 5335 Walk Out

Walk Out

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


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
 

Source

先dfs出最远的0点 再从最远的对角线开始扫,一直到最后一个点

#include <bits/stdc++.h>
using namespace std;
char g[1005][1005];
int vis[1005][1005];
char ans[5005];
int top=0;
int dirx[4]= {0,0,-1,1};
int diry[4]= {1,-1,0,0};
int n,m;
bool dp[1005][1005];
int legal(int x,int y)
{
    if(x<1||x>n||y<1||y>m)
        return 0;
    return 1;
}
int mn;
queue < pair<int,int> > q;
void bfs(int x,int y)
{
    while(!q.empty())
        q.pop();
    q.push(make_pair(x,y));
    while(!q.empty())
    {
        int x=q.front().first;
        int y=q.front().second;
        q.pop();
        if(vis[x][y]<1000009)
            continue;
        if(g[x][y]=='1')
        {
            vis[x][y]=n-x+m-y;
            mn=min(mn,n-x+m-y);
            continue;
        }
        vis[x][y]=n-x+m-y;
        mn=min(mn,n-x+m-y);
        for(int k=0; k<4; k++)
            if(legal(x+dirx[k],y+diry[k]))
                q.push(make_pair(x+dirx[k],y+diry[k]));
    }
}
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        memset(vis,0x3f,sizeof(vis));
        mn=1000000009;
        n=m=1000;
        scanf("%d %d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            scanf(" %s",g[i]+1);
        }
        bfs(1,1);
        if(mn==0&&g[n][m]=='0')
        {
            puts("0");
            continue;
        }

        memset(dp,0,sizeof(dp));
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                if(vis[i][j]==mn)
                    dp[i][j]=1;
        mn--;
        top=0;
        ans[top++]='1';
        while(mn>=0)
        {
            int flag=1;
            for(int j=m; j>=1; j--)
            {
                int i=n+m-j-mn;
                if(i<1||i>n)
                    continue;
                if(i==1)
                {
                    if(dp[i][j-1]==0)
                        continue;
                }
                else if(j==1)
                {
                    if(dp[i-1][j]==0)
                        continue;
                }
                else if(dp[i-1][j]==0&&dp[i][j-1]==0)
                    continue;
                if(g[i][j]=='0')
                    flag=0;
            }
            if(flag)
            {
                for(int j=m; j>=1; j--)
                {
                    int i=n+m-j-mn;
                    if(i<1||i>n)
                        continue;
                    if(i==1)
                    {
                        if(dp[i][j-1]==0)
                            continue;
                    }
                    else if(j==1)
                    {
                        if(dp[i-1][j]==0)
                            continue;
                    }
                    else if(dp[i-1][j]==0&&dp[i][j-1]==0)
                        continue;
                    dp[i][j]=1;
                }
            }
            else
            {
                for(int j=m; j>=1; j--)
                {
                    int i=n+m-j-mn;
                    if(i<1||i>n)
                        continue;
                    if(i==1)
                    {
                        if(dp[i][j-1]==0)
                            continue;
                    }
                    else if(j==1)
                    {
                        if(dp[i-1][j]==0)
                            continue;
                    }
                    else if(dp[i-1][j]==0&&dp[i][j-1]==0)
                        continue;
                    if(g[i][j]=='0')
                        dp[i][j]=1;
                }
            }
            mn--;
            ans[top++]='0'+flag;
        }
        for(int i=0; i<top; i++)
            printf("%c",ans[i]);
        puts("");
    }
    return 0;
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值