http://acm.hdu.edu.cn/showproblem.php?pid=2102

题目还是不难,注意起点一定是(0,0,0),然后到达P点时间<=t都可以。

用一个3维字符数组存储图,另一个三维数组标记走过的点。每次遇到#号传送过去之后,都要判断传过去的点是否被访问过。

并且还要注意传过去是‘*’的情况,和传来传去的情况都可以不用考虑。

没注意细节,WA了几次。

 #include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
char field[][][];
int used[][][];
int n,m,t;
int dir[][]={{-,},{,},{,-},{,}};
struct point
{
int z,x,y,time;
}; int bfs()
{
memset(used,,sizeof(used));
point s;
s.x=,s.y=,s.z=,s.time=;
queue<point>que;
que.push(s);
used[s.z][s.x][s.y]=;
while(!que.empty())
{
point e=que.front();
que.pop();
// printf("%d %d %d %d\n",z,e.x,e.y,e.time);
if(field[e.z][e.x][e.y]=='P'&&e.time<=t) return ;
for(int i=;i<;i++)
{
s=e;
s.x=e.x+dir[i][];
s.y=e.y+dir[i][];
if(!used[s.z][s.x][s.y]&&s.x>=&&s.x<n&&s.y>=&&s.y<m&&field[s.z][s.x][s.y]!='*')
{
if(field[s.z][s.x][s.y]=='#')
{
s.z^=;
if(used[s.z][s.x][s.y]) continue;
}
used[s.z][s.x][s.y]=;
s.time++;
que.push(s);
}
}
}
return ;
} int main()
{
//freopen("a.txt","r",stdin);
int c;
scanf("%d",&c);
while(c--)
{
scanf("%d%d%d",&n,&m,&t);
getchar();
for(int i=;i<;i++)
{
for(int j=;j<n;j++)
{
scanf("%s",field[i][j]);
// printf("%s\n",field[i][j]);
}
//getchar();
}
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(field[][i][j]=='#'&&field[][i][j]=='*') field[][i][j]='*';
if(field[][i][j]=='#'&&field[][i][j]=='*') field[][i][j]='*';
if(field[][i][j]=='#'&&field[][i][j]=='#') field[][i][j]=field[][i][j]='*';
}
// for(int i=0;i<2;i++)
// for(int j=0;j<n;j++)
// printf("%s\n",field[i][j]);
if(bfs()) printf("YES\n");
else printf("NO\n");
}
return ;
}

因为并没要求求最快到达时间,并且n比较小,所以深搜也可以。

 #include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
char field[][][];
int used[][][];
int n,m,t,flag;
int dir[][]={{-,},{,},{,-},{,}}; void dfs(int z,int x,int y,int time)
{
if(flag) return;
// printf("%d %d %d %d\n",z,x,y,time);
if(field[z][x][y]=='P'&&time<=t)
{
flag=;
printf("YES\n");
return;
}
else if(field[z][x][y]=='#')
{
int zz;
if(z==) zz=;
else zz=;
int xx=x;
int yy=y;
if(!used[zz][xx][yy]&&field[zz][xx][yy]!='*')
{
used[zz][xx][yy]=;
dfs(zz,xx,yy,time);
used[zz][xx][yy]=;
}
}
else
{
for(int i=;i<;i++)
{
int zz=z;
int xx=x+dir[i][];
int yy=y+dir[i][];
if(!used[zz][xx][yy]&&xx>=&&xx<n&&yy>=&&yy<m&&field[zz][xx][yy]!='*')
{
if(time+>t) continue;
else
{
used[zz][xx][yy]=;
dfs(zz,xx,yy,time+);
used[zz][xx][yy]=;
}
}
}
}
} int main()
{
//freopen("a.txt","r",stdin);
int c;
scanf("%d",&c);
while(c--)
{
scanf("%d%d%d",&n,&m,&t);
getchar();
for(int i=;i<;i++)
{
for(int j=;j<n;j++)
{
scanf("%s",field[i][j]);
// printf("%s\n",field[i][j]);
}
//getchar();
}
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(field[][i][j]=='#'&&field[][i][j]=='*') field[][i][j]='*';
if(field[][i][j]=='#'&&field[][i][j]=='*') field[][i][j]='*';
if(field[][i][j]=='#'&&field[][i][j]=='#') field[][i][j]=field[][i][j]='*';
}
// for(int i=0;i<2;i++)
// for(int j=0;j<n;j++)
// printf("%s\n",field[i][j])
memset(used,,sizeof(used));
flag=;
used[][][]=;
dfs(,,,);
if(!flag) printf("NO\n");
}
return ;
}

hdu - 2102 A计划 (简单bfs)的更多相关文章

  1. HDU 2102 A计划 (BFS)

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

  2. HDU 2102 A计划(BFS)

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

  3. HDU - 2102 A计划 【BFS】

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

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

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

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

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

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

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

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

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

  8. hdu 2102 A计划

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

  9. hdu 2102 A计划(双层BFS)(具体解释)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php ...

随机推荐

  1. linshiwendang12--匈牙利

    #include<bits/stdc++.h> #define N 10007 using namespace std; vector<int> p[N]; bool vis[ ...

  2. 堆(heap)和栈(stack)的区别

    转: 一.预备知识―程序的内存分配 一个由c/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)― 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中 ...

  3. Leetcode#129 Sum Root to Leaf Numbers

    原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...

  4. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppCommon.targets(611,5): error MSB

    project options, linker, manifest, Generate Manifest-> NO. 项目->属性->链接器->清单文件->生成清单  改 ...

  5. 更改DEVExpress的Column的DisplayFormat为自定义的方法。

    更改DEVExpress的Column的DisplayFormat为自定义的方法. public partial class Form1 : XtraForm { public Form1() { I ...

  6. shell 运算

    一个下午折腾一个脚本,shell好久不用,重新学起 一个小成果 size= ] do table=albums_index_${table_num} count=$size times= while ...

  7. C#正则表达式大全{转}

    只能输入数字:"^[0-9]*$". 只能输入n位的数字:"^\d{n}$". 只能输入至少n位的数字:"^\d{n,}$". 只能输入m~ ...

  8. Level2行情和传统行情的区别

    序号 Level2行情 传统行情 Level 2特点 Level 2行情优势 1 每3秒钟发送一次行情信息 每6秒钟发送一次 行情显示速度更快 投资者更及时地获得交易信息 2 证券逐笔成交明细信息 证 ...

  9. (转)xmpp 环境配置-支持扩展

    第一种方法直接拖 1> 拖入文件夹 在网盘链接的xmppFramework文件夹 :http://pan.baidu.com/s/1jGxLa3G 也可以直接去github搜索下载. 2> ...

  10. C# TryXXXX模式

    public static int? TrayParse(string text) { int ret; if (int.TryParse(text,out ret)) { return ret; } ...