hdu5335 多校联合第四场1009 搜索

http://acm.hdu.edu.cn/showproblem.php?pid=5335



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
/** 
hdu5335 多校联合第四场1009 搜索 
题目大意:给定一个由0和1组成的棋盘,从左上角走到右下角路径(上下左右四种行走方式)组成二进制数问最小的是什么 
解题思路:(转)如果我们规定这个人只能向下走或者向右走的话,问题会变的简单,二进制长度为n-m+1,然后我们可以一步一步求它每一步走的情况。 
          首先他的二进制第一位一定要为0。在第一位为0之后一定只会向下或者向右走到终点,因为中间如果向上走或者向左走的话,二进制的 
          位数会增加,大小肯定增大,所以问题的关键是找出从原点一直走0的位置,中间经过的0的位置距离终点最短的点,在这之后便只能向 
          下走或者向右走。 
*/  
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=1003;
int n,m,ans;
bool flag[maxn][maxn];
char a[maxn][maxn];
int dx[][2]= {1,0,0,1,-1,0,0,-1};
void bfs()
{
    queue <pair<int,int> >q;
    q.push(make_pair(0,0));
    while(!q.empty())
    {
        int x=q.front().first;
        int y=q.front().second;
        q.pop();
        for(int i=0; i<4; i++)
        {
            int xx=x+dx[i][0];
            int yy=y+dx[i][1];
            if(flag[xx][yy]||xx<0||xx>=n||yy<0||yy>=m)continue;
            flag[xx][yy]=1;
            ans=max(xx+yy,ans);
            if(a[xx][yy]=='0') q.push(make_pair(xx,yy));
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%s",a[i]);
        }
        ans=0;
        memset(flag,0,sizeof(flag));
        flag[0][0]=1;
        if(a[0][0]=='0')bfs();
        if(ans==n+m-2)
        {
            printf("%c\n",a[n-1][m-1]);
            continue;
        }
        printf("1");
        bool ok=0;
        for(int i=ans; i<n-1+m-1; i++)
        {
            bool judge=0;
            for(int k=0; k<=i; k++)
            {
                int x=k;
                int y=i-k;
                if(x<0||x>=n||y<0||y>=m||flag[x][y]==0)continue;
                if(ok&&a[x][y]=='1')continue;
                for(int j=0; j<2; j++)
                {
                    int xx=x+dx[j][0];
                    int yy=y+dx[j][1];
                    if(xx<0||xx>=n||yy<0||yy>=m)continue;
                    flag[xx][yy]=1;
                    if(a[xx][yy]=='0')judge=1;
                }
            }
            ok=judge;
            if(judge)printf("0");
            else printf("1");
        }
        printf("\n");
    }
    return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值