【HDU - 3085】Nightmare Ⅱ(bfs)
-->Nightmare Ⅱ
原题太复杂,直接简单的讲中文吧
Descriptions:
X表示墙
.表示路
M,G表示两个人
Z表示鬼
M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G先走M能走3步,G能走1步,Z每次向边上2步内变出分身。求所需最短时间。
鬼能穿墙,人不能
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
题目链接:
https://vjudge.net/problem/HDU-3085
设step为人一共走的轮数,每轮M走3步,G走1步,Z走2步
假设第step轮M、G碰面,碰面的地方为tmp,两个鬼为 zz[i],i可以是0或1表示两个鬼的编号,设横纵坐标分别为x,y
则tmp和zz[i]的距离为
abs(tmp.x-zz[i].x)+abs(tmp.y-zz[i].y) 这个即是曼哈顿距离
鬼在step轮可以走2*step步
若 abs(tmp.x-zz[i].x)+abs(tmp.y-zz[i].y)<=2*step
那就表示这个地方鬼能来,则这个地方不能碰面
相反若 abs(tmp.x-zz[i].x)+abs(tmp.y-zz[i].y)>2*step
那就表示这个地方鬼不能来,则这个地方能碰面
具体操作见代码
AC代码
- #include <iostream>
- #include <cstdio>
- #include <fstream>
- #include <algorithm>
- #include <cmath>
- #include <deque>
- #include <vector>
- #include <queue>
- #include <string>
- #include <cstring>
- #include <map>
- #include <stack>
- #include <set>
- #include <sstream>
- #define mod 1000000007
- #define eps 1e-6
- #define ll long long
- #define INF 0x3f3f3f3f
- #define MEM(x,y) memset(x,y,sizeof(x))
- #define Maxn 1000
- using namespace std;
- int T,n,m;
- int step;//走了几轮
- char mp[Maxn][Maxn];//原始地图
- int dt[][]= {{,},{-,},{,},{,-}};//四个方向
- struct node
- {
- int x,y;//坐标
- };
- node now,net;
- node zz[],mm,gg;//鬼 M G
- queue<node>q[];//分别表示队列M和G
- queue<node>qt;//后面函数要用到,过渡
- bool judge(node tmp)
- {
- for(int i=; i<; i++)
- {
- if(abs(tmp.x-zz[i].x)+abs(tmp.y-zz[i].y)<=step*||mp[tmp.x][tmp.y]=='X'||mp[tmp.x][tmp.y]=='\0')
- return false;
- }
- return true;
- }
- bool bfs(int pos,int steps,char start,char endd)//队列的编号 M或G可以走的步数 开始标志 结束标志
- {
- qt=q[pos];//替代,后面要根据不同的steps进行走路
- for(int j=; j<steps; j++)//这一轮走几步
- {
- while(!qt.empty())
- {
- now=qt.front();
- qt.pop();
- q[pos].pop();
- if(!judge(now))//不满足
- continue;
- for(int i=; i<; i++)//四个方向
- {
- net.x=now.x+dt[i][];
- net.y=now.y+dt[i][];
- if(!judge(net)||mp[net.x][net.y]==start)
- continue;
- if(mp[net.x][net.y]==endd)//M找到G或G找到M
- return true;
- mp[net.x][net.y]=start;//将走过的地方表示为开始标志,即M或G已经来过这
- q[pos].push(net);
- }
- }
- qt=q[pos];//都向外走了一步,重新初始化qt,再向外走一步
- }
- return false;
- }
- int solve()
- {
- step=;//初始化
- while(!q[].empty())//初始化队列,清空
- q[].pop();
- while(!q[].empty())
- q[].pop();
- while(!qt.empty())
- qt.pop();
- q[].push(mm);//分别入队
- q[].push(gg);
- while(!q[].empty()&&!q[].empty())
- {
- step++;//走了几轮
- if(bfs(,,'M','G')||bfs(,,'G','M'))
- return step;
- }
- return -;
- }
- int main()
- {
- scanf("%d",&T);//这里都要用scanf和printf,不然会超时
- while(T--)
- {
- scanf("%d%d",&n,&m);
- MEM(mp,'X');//把地图初始化为X
- for(int cnt=,i=; i<=n; i++)
- {
- scanf("%s",&mp[i][]);//一行一行存地图
- for(int j=; j<=m; j++)
- {
- // scanf("%c",&mp[i][j]);
- // cin>>mp[i][j];
- if(mp[i][j]=='M')
- {
- mm.x=i;
- mm.y=j;
- }
- if(mp[i][j]=='G')
- {
- gg.x=i;
- gg.y=j;
- }
- if(mp[i][j]=='Z')
- {
- zz[cnt].x=i;
- zz[cnt].y=j;
- cnt++;
- }
- }
- }
- printf("%d\n",solve());
- }
- }
【HDU - 3085】Nightmare Ⅱ(bfs)的更多相关文章
- 【HDU - 3533】Escape(bfs)
Escape Descriptions: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y问这个人能不能安全到 ...
- 【HDU 3085】 Nightmare Ⅱ
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3085 [算法] 双向BFS [代码] #include<bits/stdc++.h> ...
- 【HDU 2853】Assignment (KM)
Assignment Problem Description Last year a terrible earthquake attacked Sichuan province. About 300, ...
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- 【Aizu - 0558】Cheese(bfs)
-->Cheese 原文是日语,这里就写中文了 Descriptions: 在H * W的地图上有N个奶酪工厂,每个工厂分别生产硬度为1-N的奶酪.有一只老鼠准备从出发点吃遍每一个工厂的奶酪.老 ...
- 【HDU - 4345 】Permutation(DP)
BUPT2017 wintertraining(15) #8F 题意 1到n的排列,经过几次置换(也是一个排列)回到原来的排列,就是循环了. 现在给n(<=1000),求循环周期的所有可能数. ...
- 【HDU 6005】Pandaland(Dijkstra)
Problem Description Mr. Panda lives in Pandaland. There are many cities in Pandaland. Each city can ...
- 【HDU - 6581】Vacation(思维)
Vacation 题意 有n+1辆车,属性有长度l,距离终点的距离s,速度v问你最末尾的车到达终点的时间 Sample Input 1 2 2 7 1 2 1 2 1 2 2 10 7 1 6 2 1 ...
- 【HDU 5750】Dertouzos(数学)
题目给定n和d,都是10的9次方以内,求1到n里面有几个数最大因数是d?1000000组数据.解:求出d的满足p[i]*d<n的最小质因数是第几个质数.即为答案. #include<cst ...
随机推荐
- Raw-OS备用事件源代码分析
作为分析的内核版本2014-04-15,基于1.05正式版,blogs我们会跟上的内核开发进度的最新版本,如果出现源代码的目光"???"的话.没有深究的部分是理解. Raw-OS官 ...
- JAVA 加密方法
1. RSA非对称加密 生成密钥对代码: //生成秘钥对 public static KeyPair getKeyPair() throws NoSuchAlgorithmException { Ke ...
- NYOJ 298 相变点(矩阵高速功率)
点的变换 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描写叙述 平面上有不超过10000个点.坐标都是已知的.如今可能对全部的点做下面几种操作: 平移一定距离(M),相对X ...
- 索引Index
优缺点 索引是对数据库表中一列或多列的值进行排序的一种结构为了提高查询的效率索引一般建立在需要经常查询的地方 优点 创建索引可以大大提高系统的性能第一,通过创建唯一性索引,可以保证数据库表中每一行数据 ...
- Wrapped的返回值取值
Bared Wrapped using Newtonsoft.Json; using Newtonsoft.Json.Linq; string str = JsonConvert.Serial ...
- Bootstrap 标签徽章巨幕页头
@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport&q ...
- Java Socket 爬虫
# 地址 https://github.com/mofadeyunduo/crawler # 前言 1.代码不断优化更新. 2.有建议请留言. # 介绍 1.多线程,基于 ExcutorServcie ...
- WPF扩展标记X:STATIC
原文:WPF扩展标记X:STATIC public class XStaic { public static string Content = "确定"; ...
- 在 Laravel 中通过 Artisan View 扩展包创建及删除应用视图文件
1.简介 本扩展包添加了两个视图相关的Artisan命令到Laravel应用,以便我们通过Artisan命令即可创建和管理视图文件,可谓是进一步解放了生产力. 2.安装 还是通过Composer安装: ...
- C# Oracle数据库操作类
using System; using System.Data; using System.Collections.Generic; using System.Configuration; using ...