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】的更多相关文章

  1. HDU 2102 A计划(三维BFS)

    这题太欢乐了......虽然wa了几次,但是想到骑士在两幅图的传送门中传来传去就觉得这骑士太坑了 #include <cstdio> #include <iostream> # ...

  2. HDU 2102 A计划 (三维的迷宫BFS)

    题目链接:pid=2102">传送门 题意: 三维的一个迷宫,起点在第一层的S(0,0,0)处,问是否能在规定的时间内走到第二层的P 处.'*'代表不能走,'.'代表能够走,'#'代表 ...

  3. hdu - 2102 A计划 (简单bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=2102 题目还是不难,注意起点一定是(0,0,0),然后到达P点时间<=t都可以. 用一个3维字符数组存储图 ...

  4. HDU 2102 A计划(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 题目大意:公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输 ...

  5. HDU - 2102 A计划 【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2102 思路 题目有两个坑点 0.Output 说 能在T时刻 找到公主 就输出 YES 但实际上 只要 ...

  6. HDU 2102 A计划 (BFS)

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. hdu 2102 A计划(BFS,基础)

    题目 //要仔细写的BFS,着重对#穿越的处理哦: //花了几个小时终于把这道简单的BFS给弄好了,我果然还需要增加熟练度,需要再仔细一些: //代码有点乱,但我不想改了,,,,, #include& ...

  8. HDU 2102 A计划(两层地图加时间限制加传送门的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Time Limit: 3000/1000 MS (Java/Others)    Me ...

  9. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  10. hdu 2102 A计划

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸 ...

随机推荐

  1. 洛谷 P1502 窗口的星星 解题报告

    P1502 窗口的星星 题目背景 小卡买到了一套新房子,他十分的高兴,在房间里转来转去. 题目描述 晚上,小卡从阳台望出去,"哇~~~~好多星星啊",但他还没给其他房间设一个窗户, ...

  2. 【BZOJ 2553】[BeiJing2011]禁忌 AC自动机+期望概率dp

    我一开始想的是倒着来,发现太屎,后来想到了一种神奇的方法——我们带着一个既有期望又有概率的矩阵,偶数(2*id)代表期望,奇数(2*id+1)代表概率,初始答案矩阵一列,1的位置为1(起点为0),工具 ...

  3. input checkbox 多选 验证

    # input checkbox 多选 验证``` <ul class="orderMsg_radio"> <!--单选--> <input valu ...

  4. vector 搜索

    http://classfoo.com/ccby/article/cIBahI #include <iostream> #include <algorithm> #includ ...

  5. 使用JMeter进行一次简单的带json数据的post请求测试

    使用JMeter进行一次简单的带json数据的post请求测试 原文:https://www.cnblogs.com/summer-mm/p/7717812.html 1.启动jmeter:在bin下 ...

  6. Dilworth定理证明

    命题:偏序集能划分成的最少的全序集的个数与最大反链的元素个数相等. (离散数学结构第六版课本P245:把一个偏序集划分成具有全序的子集所需要的最少子集个数与元素在偏序下都是不可比的最大集合的基数之间有 ...

  7. Google MapReduce中文版

    英文原文链接: Google Map Reduce 译文原文链接: Google MapReduce中文版 Google MapReduce中文版 译者: alex 摘要 MapReduce是一个编程 ...

  8. c#中数据库字符串的连接几种方式

    ADO.net 中数据库连接方式(微软提供) 微软提供了以下四种数据库连接方式:System.Data.OleDb.OleDbConnectionSystem.Data.SqlClient.SqlCo ...

  9. windows10-seaslog安装笔记

    1.seasLog在windows下的安装 首先,要下载seasLog的dll文件,下载地址:http://pecl.php.net/package/SeasLog  选择对应你的系统和php版本的d ...

  10. Java输入输出流备忘

    重要博客: http://blog.csdn.net/hguisu/article/details/7418161 File dir = new File("\\root");   ...