hdu3085Nightmare Ⅱ(双向BFS)

HDU 3085 Nightmare Ⅱ(双向BFS 题意   A、B在迷宫中,A每回合能走3步,B每回合能走1步,迷宫中还有2个鬼,每回合最先活动,会向两步内的格子分裂,A、B不能碰鬼,求他们最快见面的回合数 思路 ​ 题目中描述A能走3步,实际上意思是A每回合能移动[0,3]步,B也同理,所以只要考虑A、B尽可能最远到达的区域,在当前回合这些区域内都是可达的   将整个题目可以考虑为一个像染色的模型,A、B、鬼分别以每回合3、1、2的速度染色,... 阅读详情

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;
}

HDU 3085 Nightmare Ⅱ【双向bfs Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3927    Accepted Submission(s): 1131   Problem Description Last night, little er... 阅读详情

相关推荐

fuck girl magazines+18(3)

fuck girl magazines+18(3)病毒,可以直接运行,插入到你电脑你的U盘上的文件全部变成快捷方式!!!小心使用(删除不了,可以用策略组解决问题)

C. Little Girl and Maximum Sum Codeforces Round #169 (Div. 2)

http://codeforces.com/contest/276/problem/C 原题连接 解题说明:题目的意思是有若干对区间和的查询,问如何重组数组使查询结果的和最大。 我的解法就是将对应的频率排序之后在求(求频率那里需要点思维嘻嘻 直接贴代码了 #include <algorithm> #include <stack> #...

axian1654的博客 4234

HDU 3038 How Many Answers Are Wrong 带权并查集

一、内容 TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored). Then, FF can choose a continuous subsequence fro

js prototype 巨大疑惑

function FuckYou(){ } FuckYou.prototype =object_test = {     name:"John",  sex:'male',  getName:function(){     return this.name;  }

1783

Eighth season fifteenth episode,Joey fell in love with Rachel??????

[Scene: Central Perk. Rachel is getting a cup of coffee as Joey and Phoebe enter and sit down.] Phoebe: Oh! Hey, Rach! Rachel: Hi! Hey, Happy Valentine's Day! Phoebe: Oh, you, too. Joey: Hey, so, uh, how's it going living over at Ross'? Rachel: It's good.

xiyangxia666hui的博客 3万+

The use of fuck: A sociolinguistic approach to the usage of fuck in the BNC and blog authorship 【翻译】

Acknowledgments Thank you to my advisor, Veronica, for allowing me to write and research this controversial topic. Also thank you to my family for supporting me throughout my education, and my friends...

hongfu951的专栏 2640

双向bfs --HDU - 3085 Nightmare

HDU - 3085 Nightmare Ⅱ:https://vjudge.net/problem/HDU-3085 题意: G和M在迷宫里寻找对方,M每秒可以走3步,G每次走一步,且只能上下左右走。迷宫里有鬼,鬼每次分裂到上下左右两格位置,直到占满所有格子,鬼可以穿墙,求G和M在不遇到鬼的情况下的最短相遇时间。 思路: 双向bfs,由于M每次走3步,所以M每次要处理3次,这要处理的点就是M每次要...

dajiangyou123456的博客 188

HDU3085 - Nightmare Ⅱ(双向BFS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085题目大意:在一个N*M的网格里,有两个人 G 和 M,并且有两只鬼。 G每秒可以走一步,G每秒可以走三步,每只鬼可以分裂,分裂到周围的的两格。假设#为鬼分裂后为1,如下图所示。每只分裂后的新鬼可以继续分裂。

ACM_Fish的博客 451

HDU3085 Nightmare双向BFS

题目描述 给定一张N*M的地图,地图中有1个男孩,1个女孩和2个鬼。 字符“.”表示道路,字符“X”表示墙,字符“M”表示男孩的位置,字符“G”表示女孩的位置,字符“Z”表示鬼的位置。 男孩每秒可以移动3个单位距离,女孩每秒可以移动1个单位距离,男孩和女孩只能朝上下左右四个方向移动。 每个鬼占据的区域每秒可以向四周扩张2个单位距离,并且无视墙的阻挡,也就是在第k秒后所有与鬼的曼哈顿距离不超过2k的...

正月看雪花的博客 550

HDU 3085 —— Nightmare双向BFS

原题:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题意: ' X ' 表示墙;' . ' 表示空地;' Z ' 表示鬼(鬼有两只); M每秒走三步;G每秒走一步;鬼每秒分裂,每次分裂距离鬼两步距离的地方都会被鬼覆盖,且鬼可以穿墙(当然鬼的分身也可分裂); 每秒鬼先分裂,然后M 和 G才走; 问在不遇到鬼的情况下,至少过几秒M和G可以相遇;

0x3f3f3f3f 479

HDU3085 Nightmare Ⅱ[双向bfs]

D - Nightmare Ⅱ Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Last night, little erriyue had a horrible nightmare. He dreamed t

ControlBear的博客 1204

HDU - 3085 Nightmare Ⅱ(双向BFS+曼哈顿距离)

HDU - 3085 Nightmare Ⅱ #include <cstring> #include <iostream> #include <algorithm> #include <queue> using namespace std; typedef pair<int, int> PII; const int N = 810; int n, m; char g[N][N]; int st[N][N]; PII ghost[2];

00后的博客 421

hdu3085 Nightmare双向BFS 模板题

Nightmare ⅡTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3068    Accepted Submission(s): 874Problem DescriptionLast night, little erriyue had ...

popcjz的博客 2124

hdu 3085 Nightmare Ⅱ (双向bfs

Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 716    Accepted Submission(s): 133 Problem Description Last night, litt

To be what you what to be! 1604

HDU 3085 Nightmare Ⅱ [双向BFS]

Nightmare Ⅱ 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...

Janice_zh的博客 376

HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2790    Accepted Sub

A man can be destroyed, but not defeated. 1203

HDU - 3085 Nightmare双向bfs

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

zamaochick的博客 205
上一篇: hdu1560DNA sequence(bfs)
下一篇: hdu1067Gap(bfs)
IDrandom
博客等级 码龄12年 31粉丝 125原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值