题意:中文题。

析:是一个简单的搜索,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. HDU1815 Building roads(二分+2-SAT)

    Problem Description Farmer John's farm has N barns, and there are some cows that live in each barn. ...

  2. python_广州房价热力图

    调用百度地图api,获取经纬度数据,然后在调用百度地图api,生成热力图 import pandas as pd import numpy as np data = pd.read_excel(r'D ...

  3. luogu P4848 崂山白花蛇草水

    https://www.luogu.org/problemnew/show/P4848 我的数据结构大概已经废了. 外层权值线段树内层kdtree,外层线段树上二分答案. 码数据结构一时爽,码完deb ...

  4. swing之记事本的简单实现

    package gui1; import java.awt.BorderLayout; import javax.swing.ImageIcon; import javax.swing.JButton ...

  5. pair对组

    一.pair基本概念 对组(pair)将一对值组合成一个值,这一对值可以具有不同的数据类型,两个值可以分别用pair的两个公有函数first和second访问. 类模板:template <cl ...

  6. oracle获得当前时间,精确到毫秒并指定精确位数

    oracle获得当前时间的,精确到毫秒   可以指定精确豪秒的位数 select to_char(systimestamp, 'yyyymmdd hh24:mi:ss.ff ') from dual; ...

  7. C# Winform中窗体的美化—— 用IrisSkin轻松实现换肤功能

    今天经前辈提醒才知道winform窗体还有美化工具,呵呵,不得不说,孤陋寡闻了.下面总结一下irisskin2的使用步骤和遇到的问题及解决办法. 1.网址:http://www.pc6.com/sof ...

  8. 在winform下实现左右布局多窗口界面的方法(二)

    这篇文章主要介绍了在winform下实现左右布局多窗口界面的方法之续篇 的相关资料,需要的朋友可以参考下 在上篇文章在winform下实现左右布局多窗口界面的方法(一)已经实现了左右布局多窗口界面,今 ...

  9. 【转】深入剖析Java中的装箱和拆箱

    深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱 ...

  10. poj 2115 C Looooops——exgcd模板

    题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...