题意:中文题。

析:是一个简单的搜索,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. HihoCoder 1190连通性·四

    连通性·四 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho从约翰家回到学校时,网络所的老师又找到了小Hi和小Ho. 老师告诉小Hi和小Ho:之前的分组出了 ...

  2. 内存优化总结:ptmalloc、tcmalloc和jemalloc

    概述 需求 系统的物理内存是有限的,而对内存的需求是变化的, 程序的动态性越强,内存管理就越重要,选择合适的内存管理算法会带来明显的性能提升.比如nginx, 它在每个连接accept后会malloc ...

  3. Makefile中的路径

    使用 $(shell pwd) 可以在Makefile中指定为当前Makefile所在目录的路径

  4. webrtc 术语

    参考网址 https://webrtcglossary.com/ ORTC stands for Object-RTC.ORTC is an initiative involving Google, ...

  5. 第一个springboot项目

    公司最近的项目在使用springboot和springcloud中的一些组件,刚开始就是主要写一些业务代码,并不了解具体要去怎么配置等,所以最近刚好有时间,就学习学习,记录总结一下,初学,欢迎指正. ...

  6. MyBatis 批量插入数据对插入记录数的限制

    <基于 MyBatis 框架的批量数据插入的性能问题的探讨>(作者:魏静敏 刘欢杰 来源:<计算机光盘软件与应用> 2013 年第 19 期)中提到批量插入的记录数不能超过10 ...

  7. Java-API-Package:org.springframework.beans.factory.annotation

    ylbtech-Java-API-Package:org.springframework.beans.factory.annotation 1.返回顶部 1. @NonNullApi @NonNull ...

  8. C# 不使用Task实现的多线程顺序执行

    多线程有很好的并发性即无序性,在某些特殊情况下需要用到多线程然而又要使其具备顺序性,这种时候就有了一个特殊的场景那就是多线程顺序执行,在现在VS2015中Task自带了顺序执行的方法,但在此之前的旧项 ...

  9. app自动更新(android)

    更新插件代码:https://github.com/shixy/UpdateApp 来源:http://aspoems.iteye.com/blog/1897300 检查更新的时候,通过指定的URL获 ...

  10. Android WebView 捕捉点击的URL中的信息

    项目要求,在WebView中点击搜索关键字,加载其他Web页面时,需要在一个文本输入框中,实时显示关键字 事实上,这种点击,是WebView内的,并没有跳出这个WebView,Activity也没有经 ...