hdu3085Nightmare Ⅱ(双向BFS)

7 篇文章 0 订阅
该博客讨论了如何使用双向BFS算法解决一个迷宫问题,其中主角和其女友试图在两个鬼魂的威胁下找到彼此。每秒主角可以移动3步,女友移动1步,而鬼魂会分裂并向周围扩散。题目给出了输入输出示例,并提到在某些情况下他们可能无法相遇。博主以124ms的时间实现了解决方案并通过了测试。
摘要由CSDN通过智能技术生成

Nightmare Ⅱ
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4397 Accepted Submission(s): 1269

Problem Description
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.

Input
The input starts with an integer T, means the number of test cases.
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.

Output
Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.

Sample Input
3
5 6
XXXXXX
XZ…ZX
XXXXXX
M.G…

5 6
XXXXXX
XZZ…X
XXXXXX
M…
…G…

10 10

…X…
…M.X…X.
X…
.X…X.X.X.
…X
…XX…X.
X…G…X
…ZX.X…
…Z…X…X

Sample Output
1
1
-1

Author
二日月

Source
HDU 2nd “Vegetable-Birds Cup” Programming Open Contest

Recommend
lcy

双向bfs,124msAC

#include<bits/stdc++.h>
using namespace std;
struct node{
    int x,y;
    node(){}
    node(int a,int b){x=a,y=b;}
};
char mp[805][805];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
vector<node>zp;
queue<node>mq[2];
int n,m,ans,tim,f;
int cnt[5];
int no(int x,int y){
    if(x<1||x>n||y<1||y>m||mp[x][y]=='X')return 1;
    if(abs(x-zp[0].x)+abs(y-zp[0].y)<=2*tim)return 1;
    if(abs(x-zp[1].x)+abs(y-zp[1].y)<=2*tim)return 1;
    return 0;
}
void bfs(int id){
    int siz=mq[id].size();
    while(siz--){
        node q=mq[id].front();
        mq[id].pop();
        if(no(q.x,q.y))continue;
        for(int i=0;i<4;i++){
            int nx=q.x+dx[i];
            int ny=q.y+dy[i];
            if(no(nx,ny))continue;
            if(mp[nx][ny]==(id^1)){f=1;return ;}
            if(mp[nx][ny]==id)continue;
            mp[nx][ny]=id;
            mq[id].push(node(nx,ny));
        }
    }
}
void solve(node st,node ed){
    ans=-1;f=0;tim=0;
    while(!mq[0].empty())mq[0].pop();
    while(!mq[1].empty())mq[1].pop();
    mq[0].push(ed);mq[1].push(st);
    while(!(mq[0].empty()||mq[1].empty())){
        tim++;
        bfs(0);
        bfs(1);
        bfs(1);
        bfs(1);
        if(f){ans=tim;return ;}
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(int i=1;i<=n;i++)cin>>mp[i]+1;
        node ep,gp;
        zp.clear();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mp[i][j]=='M'){ep=node(i,j);mp[i][j]=1;}
                if(mp[i][j]=='G'){gp=node(i,j);mp[i][j]=0;}
                if(mp[i][j]=='Z')zp.push_back(node(i,j));
            }
        }
        solve(ep,gp);
        cout<<ans<<endl;
    }
    return 0;
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值