BJFU 1406

Walk Out

时间限制(C/C++):5000MS/10000MS          运行内存限制:81920KByte

描述

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.

输入

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 (1≤n,m≤1000). The i-th line of the next n lines contains one 01 string of length m, which represents i-th row of the maze.

输出

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).


这题是一道bfs搜索题,但如果只用一次bfs会超时,所以需要用两次bfs

第一次bfs搜索起点位置(也就是从起点开始搜索为0的路径),选出里重点最近的起点。可以通过比较x+y的大小得出。

第二次bfs从第一次bfs得出的起点开始搜索路径,搜索中,如果存在为0的路径则弃掉所有为1 的路径,否则值为1的路径全部存入。

有一点需要注意的是,长宽均为1 的时候需要特判,否则会出现死循环的错误。

这种算法有一个问题就是如果起点(0,0)为1的话第一次bfs就会无效,接下来就相当于还是只用一次bfs求路径,所以可能会超时,这个算法有待改进。

#include<iostream>      
#include<stdio.h>    
#include<math.h>    
#include<string>        
#include<vector>    
#include<queue>    
#include<algorithm>        
using namespace std;   
char a[1005][1005]; 
int vis[1005][1005]; 
int fang[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; 
int n,m,flag; 
int shu[3000]; 
struct point 
{ 
    int x; 
    int y; 
    int step; 
}; 
bool judge(int x,int y) //判断是否越界
{ 
    if(x<0||x>=n||y<0||y>=m||vis[x][y]) 
        return false; 
    else return true; 
} 
int step; 
queue<point>q; 
void bfs(int x,int y) //搜索起点
{ 
    if(x==n-1&&y==m-1) //<span style="color:#ff0000;font-weight: bold;">特判是否长宽为1且(0,0)点为0</span>
    { 
        flag=1; 
        return ; 
    } 
    point max; 
    max.x=x; 
    max.y=y; 
    max.step=0; 
    int maxx=x+y; 
    queue<point>qq,qqq; 
    if(judge(x,y)&&a[x][y]=='0') 
    qq.push(max); 
    vis[x][y]=1; 
    while(!qq.empty()) 
    { 
        point t; 
        t=qq.front(); 
        qq.pop(); 
        for(int i=0;i<4;i++) 
        if(judge(t.x+fang[i][0],t.y+fang[i][1])) 
        { 
            vis[t.x+fang[i][0]][t.y+fang[i][1]]=1; 
            if(a[t.x+fang[i][0]][t.y+fang[i][1]]=='1') //如果为1存入
            { 
            point tt; 
            tt.x=t.x+fang[i][0]; 
            tt.y=t.y+fang[i][1]; 
            tt.step=0; 
            qqq.push(tt); 
            if(t.x+fang[i][0]+t.y+fang[i][1]>maxx) 
            { 
                maxx=t.x+fang[i][0]+t.y+fang[i][1]; 
            } 
            } 
            if(a[t.x+fang[i][0]][t.y+fang[i][1]]=='0') //如果为0继续判断
            { 
                if(t.x+fang[i][0]==n-1&&t.y+fang[i][1]==m-1) //已经搜索到终点(数组全为0)
                { 
                    flag=1; 
                    return ; 
                } 
                point tt; 
                tt.x=t.x+fang[i][0]; 
                tt.y=t.y+fang[i][1]; 
                tt.step=0; 
                qq.push(tt); 
            } 
        } 
    } 
    while(!qqq.empty()) //找出最远的起点
    { 
        point tp=qqq.front(); 
        qqq.pop(); 
        if(tp.x+tp.y==maxx)q.push(tp); 
    } 
} 
vector<point>zero; 
vector<point>one; 
int len; 
void bfs2() //搜索路径
{ 
    while(1) 
    { 
        zero.clear(); 
        one.clear(); 
    while(!q.empty()) //这里引入两个vector数组分别用来储存值为0和1的路径
    { 
        point t=q.front(); 
        q.pop(); 
        shu[t.step]=a[t.x][t.y]-'0'; 
        len=t.step; 
        if(t.x==n-1&&t.y==m-1)return ; //到达终点
        if(judge(t.x+1,t.y)&&a[t.x+1][t.y]=='1') 
        { 
            point tt; 
            tt.x=t.x+1; 
            tt.y=t.y; 
            tt.step=t.step+1; 
            vis[tt.x][tt.y]=1; 
            one.push_back(tt); 
        } 
        if(judge(t.x+1,t.y)&&a[t.x+1][t.y]=='0') 
        { 
            point tt; 
            tt.x=t.x+1; 
            tt.y=t.y; 
            tt.step=t.step+1; 
            vis[tt.x][tt.y]=1; 
            zero.push_back(tt); 
        } 
        if(judge(t.x,t.y+1)&&a[t.x][t.y+1]=='1') 
        { 
            point tt; 
            tt.x=t.x; 
            tt.y=t.y+1; 
            tt.step=t.step+1; 
            vis[tt.x][tt.y]=1; 
            one.push_back(tt); 
        } 
        if(judge(t.x,t.y+1)&&a[t.x][t.y+1]=='0') 
        { 
            point tt; 
            tt.x=t.x; 
            tt.y=t.y+1; 
            tt.step=t.step+1; 
            vis[tt.x][tt.y]=1; 
            zero.push_back(tt); 
        } 
    } 
        if(!zero.empty()) //如果存在值为0的路径存入队列,值为1的路径删除
        { 
            for(int i=0;i<zero.size();i++) 
                q.push(zero[i]); 
        } 
        else 
        { 
            for(int i=0;i<one.size();i++) 
                q.push(one[i]); 
        } 
    } 
} 
int main() 
{ 
    int T; 
    scanf("%d",&T); 
    while(T--) 
    { 
        memset(a,0,sizeof(a)); 
        memset(vis,0,sizeof(vis)); 
        memset(shu,0,sizeof(shu)); 
        len=0,flag=0; 
        while(!q.empty())q.pop(); 
        zero.clear(); 
        one.clear(); 
        scanf("%d%d",&n,&m); 
        for(int i=0;i<n;i++) 
                scanf("%s",a[i]); 
        if(a[0][0]=='1') 
        { 
            point aaa; 
            aaa.x=0; 
            aaa.y=0; 
            aaa.step=0; 
            q.push(aaa); 
        } 
        else bfs(0,0); 
        if(flag) 
        { 
            printf("0\n"); 
            continue; 
        } 
        bfs2(); 
        for(int i=0;i<=len;i++) 
            printf("%d",shu[i]); 
        printf("\n"); 
    } 
    return 0; 
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值