题意:中文题。

析:是一个简单的搜索,BFS 和 DFS都可行, 主要是这个题有一个坑点,那就是如果有一层是#,另一个层是#或者*,都是过不去的,就可以直接跳过,

剩下的就是一个简单的搜索,只不过是两层而已,再加一维表示是哪一层就好,可能一个就是在#必须传送,不能再上下左右走,这个题目已经说的很清楚了,不算坑。

对于BFS,如果找到P就可以结束,如果超过了T,那么就是找不到了,返回找不到。

对于DFS,同样是找到就结束。

总体来说不难。。。。

代码如下:

BFS:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#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 <cmath>
#include <stack>
#include <sstream>
#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 freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "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 = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
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};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} char s[2][maxn][maxn];
int vis[2][maxn][maxn];
int t;
struct Node{
int pos, x, y;
Node() { }
Node(int p, int xx, int yy) : pos(p), x(xx), y(yy) { }
bool operator == (const Node &p) const{
return pos == p.pos && x == p.x && y == p.y;
}
}; bool bfs(){
memset(vis, -1, sizeof vis);
Node goal;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
if(s[0][i][j] == 'P') goal = Node(0, i, j);
else if(s[1][i][j] == 'P') goal = Node(1, i, j);
queue<Node> q;
vis[0][0][0] = 0;
q.push(Node(0, 0, 0)); while(!q.empty()){
Node u = q.front(); q.pop();
if(vis[u.pos][u.x][u.y] > t) return false;
if(u == goal) return true;
for(int i = 0; i < 4; ++i){
int x = u.x + dr[i];
int y = u.y + dc[i];
if(!is_in(x, y) || vis[u.pos][x][y] != -1 || s[u.pos][x][y] == '*') continue;
vis[u.pos][x][y] = vis[u.pos][u.x][u.y] + 1;
if(s[u.pos][x][y] == '#'){
vis[u.pos^1][x][y] = vis[u.pos][x][y];
if(s[u.pos^1][x][y] == '#' || s[u.pos^1][x][y] == '*') continue;
q.push(Node(u.pos^1, x, y));
continue;
}
q.push(Node(u.pos, x, y));
}
}
return false;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d %d %d", &n, &m, &t);
for(int i = 0; i < n; ++i) scanf("%s", s[0]+i);
for(int i = 0; i < n; ++i) scanf("%s", s[1]+i);
bool ans = bfs();
printf("%s\n", ans ? "YES" : "NO");
}
return 0;
}

 DFS:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#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 <cmath>
#include <stack>
#include <sstream>
#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 freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "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 = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
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};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} char s[2][maxn][maxn];
bool vis[2][maxn][maxn];
int t; bool dfs(int pos, int r, int c, int d){
if(d > t) return false;
if(s[pos][r][c] == 'P') return true;
for(int i = 0; i < 4; ++i){
int x = r + dr[i];
int y = c + dc[i];
if(!is_in(x, y) || vis[pos][x][y] || s[pos][x][y] == '*') continue;
vis[pos][x][y] = 1;
if(s[pos][x][y] == '#'){
vis[pos^1][x][y] = 1;
if(dfs(pos^1, x, y, d+1)) return true;
vis[pos^1][x][y] = 0;
}
else if(dfs(pos, x, y, d+1)) return true;
vis[pos][x][y] = 0;
}
return false;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d %d %d", &n, &m, &t);
for(int i = 0; i < n; ++i) scanf("%s", s[0]+i);
for(int i = 0; i < n; ++i) scanf("%s", s[1]+i);
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
if(s[0][i][j] == '#' && (s[1][i][j] == '*' || s[1][i][j] == '#')) s[0][i][j] = s[1][i][j] = '*';
else if(s[0][i][j] == '*' && s[1][i][j] == '#') s[1][i][j] = '*';
memset(vis, 0, sizeof vis);
vis[0][0][0] = 1;
bool ans = dfs(0, 0, 0, 0);
printf("%s\n", ans ? "YES" : "NO");
}
return 0;
}

  

HDU 2102 A计划 (BFS或DFS)的更多相关文章

  1. HDU 2102 A计划(BFS/DFS走迷宫)

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

  2. hdu 2102 A计划(优先队列+dfs)

    改了好久,上午来实验室打出来了,运行就是不对,一直找啊找!还是没找到,最后突然停电了,打好的代码还没保存呢! 刚才来的时候又重新打了一遍!!!结果一个小小的错误wrong了好久!!! 在dfs值返回时 ...

  3. hdu 2102 A计划-bfs

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  4. HDU - 2102 A计划 (BFS) [kuangbin带你飞]专题二

    思路:接BFS判断能否在限制时间内到达公主的位置,注意如果骑士进入传送机就会被立即传送到另一层,不会能再向四周移动了,例如第一层的位置(x, y, 1)是传送机,第二层(x, y, 2)也是传送机,这 ...

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

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

  6. hdu 2102 A计划

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

  7. [HDU 2102] A计划(搜索题,典型dfs or bfs)

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

  8. HDU 2102 A计划 DFS与BFS两种写法 [搜索]

    1.题意:一位公主被困在迷宫里,一位勇士前去营救,迷宫为两层,规模为N*M,迷宫入口为(0,0,0),公主的位置用'P'标记:迷宫内,'.'表示空地,'*'表示墙,特殊的,'#'表示时空传输机,走到这 ...

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

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

随机推荐

  1. BZOJ - 2618 凸多边形 (半平面交)

    题意:求n个凸多边形的交面积. 半平面交模板题. #include<bits/stdc++.h> using namespace std; typedef long long ll; ty ...

  2. andriod&linux&c函数原型

    1.dlopen 功能:打开一个动态链接库,并返回动态链接库的句柄 包含头文件: #include <dlfcn.h> 函数定义: void * dlopen( const char * ...

  3. Python修复图像文件后缀名

    网上爬了很多图片,有很多错误. 有的不是图片文件,需要删除 有的后缀名错误,需要更正 用的的python脚本 #!/usr/bin/env python #-*- coding: utf-8 -*-# ...

  4. 应用层-day01

    主流应用程序体系结构:CS结构.P2P结构. CS结构:客户-服务器体系结构.有一台总是打开的主机称为服务器,它服务来自其他许多称为客户的主机的请求. P2P体系结构:应用程序在不同的主机间链接,被称 ...

  5. winrm service

    今天看脚本忽然发现一个服务,叫winRM服务,这是个PowerShell的远程管理.开启它可以很大程度的方便用PowerShell操控! 下面是我找到的一些资料: 在Linux中,我们可以使用安全的S ...

  6. 蘑菇街 IM 项目 TeamTalk

    源码 https://github.com/mogujie/TeamTalk 试用 http://tt.mogu.io/

  7. NB-LOT 科普

    最全科普!你一定要了解的NB-IoT 2017-06-19 21:04物联网/操作系统/科普 工信部下发通知推动150万NB-IoT基站落地.NB-IoT汹涌而来.很多网友要求雇佣军科普一篇NB-Io ...

  8. Zookeeper学习(八):Zookeeper的数据发布与订阅模式

     http://blog.csdn.net/ZuoAnYinXiang/article/category/6104448 1.发布订阅的基本概念        1.发布订阅模式可以看成一对多的关系:多 ...

  9. 忽略‘Chrome正在受到自动软件的控制’的提示语,以及后台静默模式启动。

    一.使用Chrome做的时候,会看到浏览器上方出现‘Chrome正在受到自动软件的控制’的提示语, 若想忽略此提示信息,在浏览器配置里加个参数:disable_infobars 代码如下 : # co ...

  10. Log4j 记录error 日志

    第一个bug的起始,是在线上日志发现一个频繁打印的异常——java.lang.ArrayIndexOutOfBoundsException.但是却没有堆栈,只有一行一行的ArrayIndexOutOf ...