题意:中文题。

析:是一个简单的搜索,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. Java垃圾回收机制——finallize()

    其实了解JAVA的人,都知道JAVA的GC机制是其的一大优点,它令程序员不需要主动去考虑内存溢出和垃圾回收的问题,不像c++具有显式的析构函数对整个对象进行内存清理以及需要调用delete才可以进行显 ...

  2. webpack 配置简单说几句 ?

    前言 这几天在准备一个单页面应用, 准备试试webpack神器,在准备webpack下的知识点,顺便记录下一些使用的心得. webpack 的配置说明 在近来的前端开发中,业务逻辑复杂化,层次多样化, ...

  3. CentOS 7.2 PowerShell下安装Azure Module

    目前Linux版本的PowerShell还是Alpha版本,所以很多功能不能使用. 比如通过Powershell命令:install-module AzureRM在线安装Azure的Module.但我 ...

  4. 在Azure New Portal上创建基于ARM的带SLB的VM

    目前Azure的New Portal在国内已经上线了.本文将介绍最常见的一种场景:通过Azure的New Portal创建带有Server Load Balance的多台虚拟机. 1 创建Resour ...

  5. 蓝桥杯 算法训练 ALGO-151 6-2递归求二进制表示位数

    算法训练 6-2递归求二进制表示位数   时间限制:10.0s   内存限制:256.0MB 问题描述 给定一个十进制整数,返回其对应的二进制数的位数.例如,输入十进制数9,其对应的二进制数是1001 ...

  6. ps命令,性能监控,grep命令

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  7. 2016.7.10 SqlServer语句中类似decode、substr、instr、replace、length等函数的用法

    Decode() 对应 case when函数 case CHARINDEX('/',start_point_name) when 0 then start_point_name else subst ...

  8. python 函数和方法的区别

    一.函数和方法的区别 1.函数要手动传self,方法不用传 2.如果是一个函数,用类名去调用,如果是一个额方法,用对象去调用 举例说明: class Foo(object): def __init__ ...

  9. 在云服务器上体验Docker

    1. 添加Docker repository key sh -c "wget -qO- https://get.docker.io/gpg | apt-key add -" 2. ...

  10. 安装 MongoDB。

    1.安装 MongoDB. 1.为软件包管理系统导入公钥. Ubuntu 软件包管理工具为了保证软件包的一致性和可靠性需要用 GPG 密钥检验软件包.使用下列命令导入 MongoDB 的 GPG 密钥 ...