HDU 5335 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

先用dfs推断最远的0能够到达的位置。然后依照斜行递推

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<algorithm>
#include<string>
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
using namespace std;
const ll maxn=1005;
int T,n,m,f[maxn][maxn],c[maxn][maxn],tot;
char s[maxn][maxn];

void dfs(int x,int y)
{
    if (c[x][y]) return ;
    c[x][y]=1;
    if (s[x][y]=='1') return;
    f[x][y]=1;
    if(x+y>tot) tot=x+y;
    if (x>1) dfs(x-1,y);
    if (x<n) dfs(x+1,y);
    if (y>1) dfs(x,y-1);
    if (y<m) dfs(x,y+1);
}

int main()
{
    scanf("%d",&T);
    while (T--)
    {
        memset(f,0,sizeof(f));
        memset(c,0,sizeof(c));
        scanf("%d%d",&n,&m);
        tot=1;
        for (int i=1;i<=n;i++)
        {
            scanf("%s",s[i]+1);
        }
        dfs(1,1);
        if(tot==n+m)
        {printf("0\n");continue;}
        if(tot==1)
        {
            tot=2;
            f[1][1]=1;
            printf("%d",1);
        }
        for (int i=tot;i<n+m;i++)
        {
            int flag=1;
            for (int j=max(1,i-m);j<=min(n,i-1);j++)
            if (f[j][i-j])
            {
                int x=j<n?s[j+1][i-j]-'0':1;
                int y=i-j<m?s[j][i-j+1]-'0':1;
                flag=min(flag,min(x,y));
            }
            for (int j=max(1,i-m);j<=min(n,i-1);j++)
            if (f[j][i-j])
            {
                int x=j<n?

s[j+1][i-j]-'0':1; int y=i-j<m?s[j][i-j+1]-'0':1; if (x==flag) f[j+1][i-j]=1; if (y==flag) f[j][i-j+1]=1; } printf("%d",flag); } printf("\n"); } return 0; }



转载于:https://www.cnblogs.com/wzjhoutai/p/6752975.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值