HDU 5335 Walk Out(搜索+贪心)

Walk Out

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


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
 

Author
XJZX
 

Source
 




     题意:在一个n*m的矩阵中只有0和1两种数字,现在要求从左上角到达右下角形成的二进制数最小,并且遇到第一个1之前的0可以去掉(例如000010写成10)


     思路:用BFS找离右下角最近的只包含0的一条路,找到以后贪心,在离右下角相同距离的数字中,如果有0的存在就只算0的情况,没有0再算1的情况




#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int n,m;
char map[1010][1010];
int v[1010][1010];
int num[2010];
int jx[] = {1,0,0,-1};
int jy[] = {0,1,-1,0};
int xx[] = {0,1};
int yy[] = {1,0};

struct node
{
    int x;
    int y;
    int z;
} q[1000010],a[1010];

void BFS()
{
    int h = 0;
    int pt = 0;
    memset(v,0,sizeof(v));
    for(int i=0;i<2008;i++)
    {
        num[i] = -1;
    }
    struct node t,f;
    int maxx = 0;
    int s = 0,e = 0;
    t.x = 0;
    t.y = 0;
    t.z = 0;
    if(map[0][0] == '0')
    {
        h = 0;
        q[e++] = t;
        v[t.x][t.y] = 1;
        while(s<e)
        {
            t = q[s++];
            for(int i=0; i<4; i++)
            {
                f.x = t.x + jx[i];
                f.y = t.y + jy[i];
                f.z = t.z + 1;
                if(f.x>=0 && f.x<n && f.y>=0 && f.y<m && v[f.x][f.y] == 0)
                {
                    v[f.x][f.y] = 1;
                    if(map[f.x][f.y] == '0')
                    {
                        if(f.x == n-1 && f.y == m-1)
                        {
                            printf("0\n");
                            return ;
                        }
                        q[e++] = f;
                    }
                    else
                    {
                        if(f.x+f.y>=maxx)
                        {
                            if((f.x+f.y) == maxx)
                            {
                                a[h].x = f.x;
                                a[h].y = f.y;
                                a[h].z = f.z;
                                h++;
                            }
                            else
                            {
                                maxx = f.x + f.y;
                                h = 0;
                                a[h].x = f.x;
                                a[h].y = f.y;
                                a[h].z = f.z;
                                h++;
                            }
                        }
                    }

                }
            }
        }
    }
    else
    {
        h = 0;
        a[h].x = 0;
        a[h].y = 0;
        a[h].z = 0;
        h++;
        //maxx = 1;
    }
    int pk = 0;
    num[0] = 1;
    for(int i=0;i<h;i++)
    {
        if(a[i].x == n-1 && a[i].y == m-1)
        {
            printf("1\n");
            return ;
        }
    }
    int ff = 0;
    while(ff == 0)
    {
        pk++;
        pt = 0;
        int flag = 0;
        for(int i=0; i<h; i++)
        {
            t = a[i];
            for(int j=0; j<2; j++)
            {
                f.x = t.x + xx[j];
                f.y = t.y + yy[j];
                f.z = t.z + 1;
                if(f.x>=0 && f.x<n && f.y>=0 && f.y<m && v[f.x][f.y] == 0 && ff == 0)
                {
                    v[f.x][f.y] = 1;
                    if(map[f.x][f.y] == '1' && flag == 0)
                    {
                        q[pt++] = f;
                    }
                    else if(map[f.x][f.y] == '0')
                    {
                        if(flag == 0)
                        {
                            flag = 1;
                            pt = 0;
                            q[pt++] = f;
                        }
                        else
                        {
                            q[pt++] = f;
                        }
                    }
                    if(f.x == n-1 && f.y == m-1)
                    {
                        ff = 1;
                    }
                }
            }
        }
        if(flag == 0)
        {
            num[pk] = 1;
        }
        else
        {
            num[pk] = 0;
        }
        h = pt;
        for(int i=0; i<pt; i++)
        {
            a[i] = q[i];
        }
    }
    for(int i=0; i<=pk; i++)
    {
        printf("%d",num[i]);
    }
    printf("\n");

}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%s",map[i]);
        }
        if(n == 1 && m == 1 && map[0][0] == '0')
        {
            printf("0\n");
            continue;
        }
        BFS();
    }
    return 0;
}




/*
#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;
}
*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值