码迷,mamicode.com
首页 > 其他好文 > 详细

hdu5335Walk Out

时间:2015-07-30 23:27:58      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

题意:给出一个01矩阵,从左上角走到右下角,问路径形成的二进制最小是多少。


这个题目很显然,但是我的做法还是很有意思的,于是写下。


很显然,为了去掉前导0的影响,首先爆搜出所有离终点最近的点然后压入队列开始爆搜,此时只需要往下往右走即可。


爆搜的时候每次处理出下一层的所有状态,若能够出现0,则不考虑其他为1的情况,这样只需要维护两个数组即可,很方便。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int tor[4]={-1,1,0,0};
int toc[4]={0,0,-1,1};
struct point
{
    int r,c;
    point(){}
    point(int r,int c)
    {
        this->r=r;
        this->c=c;
    }
};
char s[1010][1010];
bool vis[1010][1010];
point q[2][1000010];
int len[2];
int n,m,x;
bool beyond(int r,int c)
{
    return r<0||c<0||r>=n||c>=m;
}
void seek()
{
    memset(len,0,sizeof(len));
    q[0][len[0]++]=point(0,0);
    if(s[0][0]=='1')
    {
        x=1;
        return;
    }
    x=0;
    q[1][len[1]++]=point(0,0);
    memset(vis,0,sizeof(vis));
    vis[0][0]=1;
    int mx=0;
    while(len[1]>0)
    {
        point from=q[1][--len[1]];
        int r=from.r,c=from.c;
        for(int i=0;i<4;i++)
        {
            int tr=r+tor[i],tc=c+toc[i];
            if(beyond(tr,tc)||vis[tr][tc]||s[tr][tc]!='0')
                continue;
            vis[tr][tc]=1;
            int t=tr+tc;
            if(t>=mx)
            {
                if(t>mx)
                    len[0]=0;
                mx=t;
                q[0][len[0]++]=point(tr,tc);
            }
            q[1][len[1]++]=point(tr,tc);
        }
    }
}
int ans[10000];
int bfs()
{
    bool p=0;
    memset(vis,0,sizeof(vis));
    for(int i=0;;i++)
    {
        int j=i%2;
        bool flag=0;
        while(len[j]>0)
        {
            len[j]--;
            int r=q[j][len[j]].r,c=q[j][len[j]].c;
            if(s[r][c]=='1'&&p)
                continue;
            for(int k=0;k<4;k++)
                if(k&1)
                {
                    int tr=r+tor[k],tc=c+toc[k];
                    if(beyond(tr,tc)||vis[tr][tc])
                        continue;
                    vis[tr][tc]=1;
                    q[j^1][len[j^1]++]=point(tr,tc);
                    if(s[tr][tc]=='0')
                        flag=1;
                }
        }
        p=flag;
        ans[i]=flag?0:1;
        if(vis[n-1][m-1])
            return i;
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        seek();
        if(x==0&&vis[n-1][m-1])
        {
            printf("0\n");
            continue;
        }
        if(x==1)
        {
            putchar('1');
            if(n==1&&m==1)
            {
                puts("");
                continue;
            }
        }
        int lg=bfs();
        for(int i=0;i<=lg;i++)
            printf("%d",ans[i]);
        puts("");
    }
    return 0;
}



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


Problem Description
In an n?m 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




版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu5335Walk Out

标签:

原文地址:http://blog.csdn.net/stl112514/article/details/47156819

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!