HDU 2102 A计划 (BFS或DFS)
题意:中文题。
析:是一个简单的搜索,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)的更多相关文章
- HDU 2102 A计划(BFS/DFS走迷宫)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 2102 A计划(优先队列+dfs)
改了好久,上午来实验室打出来了,运行就是不对,一直找啊找!还是没找到,最后突然停电了,打好的代码还没保存呢! 刚才来的时候又重新打了一遍!!!结果一个小小的错误wrong了好久!!! 在dfs值返回时 ...
- hdu 2102 A计划-bfs
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- HDU - 2102 A计划 (BFS) [kuangbin带你飞]专题二
思路:接BFS判断能否在限制时间内到达公主的位置,注意如果骑士进入传送机就会被立即传送到另一层,不会能再向四周移动了,例如第一层的位置(x, y, 1)是传送机,第二层(x, y, 2)也是传送机,这 ...
- HDU 2102 A计划(两层地图加时间限制加传送门的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Time Limit: 3000/1000 MS (Java/Others) Me ...
- hdu 2102 A计划
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2102 A计划 Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸 ...
- [HDU 2102] A计划(搜索题,典型dfs or bfs)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 2102 A计划 DFS与BFS两种写法 [搜索]
1.题意:一位公主被困在迷宫里,一位勇士前去营救,迷宫为两层,规模为N*M,迷宫入口为(0,0,0),公主的位置用'P'标记:迷宫内,'.'表示空地,'*'表示墙,特殊的,'#'表示时空传输机,走到这 ...
- hdu - 2102 A计划 (简单bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=2102 题目还是不难,注意起点一定是(0,0,0),然后到达P点时间<=t都可以. 用一个3维字符数组存储图 ...
随机推荐
- UVA - 1252 Twenty Questions (状压dp+vis数组加速)
有n个物品,每个物品有m个特征.随机选择一个物品让你去猜,你每次可以询问一个特征的答案,问在采取最优策略时,最坏情况下需要猜的次数是多少. 设siz[S]为满足特征性质集合S的特征的物品总数,dp[S ...
- 《Javascript高级程序设计》阅读记录(四):第五章 下
这个系列,我会把阅读<Javascript高级程序设计>之后,感觉讲的比较深入,而且实际使用价值较大的内容记录下来,并且注释上我的一些想法.做这个一方面是提升了我的阅读效果以及方便我以后阅 ...
- OSError: [Errno 22] Invalid argument: 'D:\x07ckup\test.txt'
使用with open("D:\backup\test.txt","rt") as f:报上面的错误,将上面的语句改成 with open(r"D:\ ...
- 第11篇 PSR-0 规范
Mandatory A fully-qualified namespace and class must have the following structure \<Vendor Name&g ...
- thinkphp <volist>标签中 <if> 判断的写法
thinkphp <volist>标签中 <if> 判断的写法 <volist name="data" id="vo"> & ...
- 【转】 Pro Android学习笔记(八七):了解Handler(1):组件和线程
目录(?)[-] 主线程消息队列和线程池 查看线程信息 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingf ...
- codeforce 103B Cthulhu
B. Cthulhu time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- MacOS配置Erlang开发环境
Mac下安装Erlang brew 的安装: $ curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C ...
- Spring MVC配置详解(2)---bai
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2 ...
- 实验吧CTF题库-安全杂项
XDCTF misc200: 题目提示: 下载文件 用foremost分离文件 接下来用archpr 4.53进行明文攻击 先把00002700.zip中的readme.txt提取出来压缩成zip文件 ...