hdu5335 多校联合第四场1009 搜索

来源:互联网 发布:直播间源码下载 编辑:程序博客网 时间:2024/06/05 16:17

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
22 211113 3001111101
 

Sample Output
111101
/** 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;}


0 0
原创粉丝点击