HDU 2102 A计划【三维BFS】
A计划
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 28414 Accepted Submission(s): 7129
Problem Description
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
Sample Input
1
5 5 14
S*#*.
.#...
.....
****.
...#.
..*.P
#.*..
***..
...*.
*.#..
Sample Output
YES
【分析】:遇到上下两层都是# 的,就把上下两层的这个位置都弄成 墙就行。还有遇到 一层是#一层是墙的,也直接把俩都弄城墙就行,省的要判断他撞死。首先开一个3维的空间,为方便,坐标设置为z、x、y(z用0或1表示,因为后面用起来会很方便)。
于是就是找坑点,#传送后不能是#或*,尤其注意#传送后可能直接是P,再就是#是不浪费时间的,这里经过处理后也不算是坑。还有就是对时间的把握。
【代码】:
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("+++++++++++++++++++++++++++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 25;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int d[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} };
int dir[4][2]={0,1,0,-1,1,0,-1,0};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n,m,t,T;
int ex,ey,ez,flag;
char a[2][35][35];
int v[2][35][35];
struct node
{
int x,y,z,s;
}st,ed;
bool check(int x,int y,int z)
{
if(x<0||x>=n||y<0||y>=m) return 0;
else if(v[z][x][y]==1) return 0;
else if(a[z][x][y]=='*') return 0;
else if(a[z][x][y]=='#' && a[1^z][x][y]=='*') return 0;
else if(a[z][x][y]=='#' && a[1^z][x][y]=='#') return 0;
else return 1;
}
void bfs()
{
queue<node>q;
st.x = st.y = st.z = st.s = 0;
q.push(st);
v[0][0][0]=1;
while(!q.empty())
{
st = q.front();
q.pop();
if(st.s > t) return ;
if(a[st.z][st.x][st.y] == 'P') //z、x、y的先后顺序很容易写错,而且写错了显示的是MLE,有点奇怪
{
printf("YES\n");
flag = 1;
return ;
}
for(int i=0; i<4; i++)
{
ed.x = st.x + dir[i][0];
ed.y = st.y + dir[i][1];
ed.z = st.z;
if(check(ed.x, ed.y, ed.z))
{
if(a[ed.z][ed.x][ed.y]=='#') //遇到#穿越不花时间只改变
ed.z = !ed.z;
ed.s = st.s + 1; //虽然上下楼不用时间,但是你走到#需要一步的时间
v[ed.z][ed.x][ed.y] = 1;
q.push(ed);
}
}
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
flag=0;
ms(v,0);
ms(a,'*');
scanf("%d%d%d",&n,&m,&t);
for(int i=0;i<n;i++){
scanf("%s",&a[0][i]);
}
for(int i=0;i<n;i++){
scanf("%s",&a[1][i]);
}
/*
for(int i=0;i<2;i++)
{
for(int j=0;j<n;j++)
{
scanf("%s",a[i][j]);
}
}
*/
bfs();
if(!flag) printf("NO\n");
//debug();
}
}
/*
6
5 5 14
S*#*.
.#...
.....
****.
...#.
..*.P
#.*..
***..
...*.
*.#..
5 5 13
S*#*.
.#...
.....
****.
...#.
..*.P
#.*..
***..
...*.
*.#..
5 5 13
S*#*.
.#...
.....
****.
...#.
..*.P
#.*..
***..
...*.
*.##.
5 5 8
S*#*#
.#**.
.....
*****
...#.
..*.P
#.*..
***..
...*.
*.##.
1 4 3
.#.#
*.#P
1 4 2
.#.#
*.#P
Y N N Y Y N
*/
HDU 2102 A计划【三维BFS】的更多相关文章
- HDU 2102 A计划(三维BFS)
这题太欢乐了......虽然wa了几次,但是想到骑士在两幅图的传送门中传来传去就觉得这骑士太坑了 #include <cstdio> #include <iostream> # ...
- HDU 2102 A计划 (三维的迷宫BFS)
题目链接:pid=2102">传送门 题意: 三维的一个迷宫,起点在第一层的S(0,0,0)处,问是否能在规定的时间内走到第二层的P 处.'*'代表不能走,'.'代表能够走,'#'代表 ...
- hdu - 2102 A计划 (简单bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=2102 题目还是不难,注意起点一定是(0,0,0),然后到达P点时间<=t都可以. 用一个3维字符数组存储图 ...
- HDU 2102 A计划(BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 题目大意:公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输 ...
- HDU - 2102 A计划 【BFS】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2102 思路 题目有两个坑点 0.Output 说 能在T时刻 找到公主 就输出 YES 但实际上 只要 ...
- HDU 2102 A计划 (BFS)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 2102 A计划(BFS,基础)
题目 //要仔细写的BFS,着重对#穿越的处理哦: //花了几个小时终于把这道简单的BFS给弄好了,我果然还需要增加熟练度,需要再仔细一些: //代码有点乱,但我不想改了,,,,, #include& ...
- HDU 2102 A计划(两层地图加时间限制加传送门的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Time Limit: 3000/1000 MS (Java/Others) Me ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- hdu 2102 A计划
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸 ...
随机推荐
- filebeat + logstash + elasticsearch + granfa
filebeat + logstash + elasticsearch + granfa https://www.cnblogs.com/wenchengxiaopenyou/p/9034213.ht ...
- 【bzoj3997】[TJOI2015]组合数学 Dilworth定理结论题+dp
题目描述 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一块财宝,至少走 ...
- 2017 Multi-University Training Contest - Team 3 RXD and functions(NTT)
题解: 我是参考的 http://blog.csdn.net/qq_32570675/article/details/76571666 这一篇 orz 原来可以这么变换,涨姿势 代码: #includ ...
- 利用FFT来进行字符串匹配
给定串A和串B,A由26个小写字母构成,B由?和26个小写字母构成 ?可以和任意字符匹配 求A中出现了多少次B 这里可以使用fft做法,定义向量A和向量B 然后求A和rev(B)的卷积结果C C的第i ...
- Visio中ShapeAdded和SelectionAdded
SelectionAdded 和 ShapeAdded 事件的相似之处在于它们都在创建形状之后触发.它们的区别在于,当单个操作添加多个形状时它们的行为方式不同.假定一个 Paste 操作创建三个新建形 ...
- WM_CTLCOLOR消息
文章参考地址:http://blog.csdn.net/hisinwang/article/details/8070393 在每个控件开始绘制之前,都会向其父窗口发送WM_CTLCOL ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- poj3375 Network Connection
Description There are \(M\) network interfaces in the wall of aisle of library. And \(N\) computers ...
- 使用setTimeout延时10ms执行onunloadcancel
在做Web开发时,我们经常用到页面关闭事件onbeforeunload,可以给用户一个选择放弃关闭的机会,就比如这个博客编辑器.如果用户选择了离开,那么onunload事件自然会触发:但若用户选择了取 ...
- eclipse web(Spring+SpringMVC+Hibernate)项目迁移至intellij idea
1.导入Eclipseweb项目 跟着导航一直下一步 出现警告不要担心,先点击确认,到后面再进行设置jdk 成功导入项目后如下图 2.对导入的项目进行配置按Ctrl+shift+alt+s(或下图中的 ...