您的位置:首页 > 其它

HDU 2102 A计划

2014-08-30 11:04 239 查看

A计划

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 2102
64-bit integer IO format: %I64d Java class name: Main

可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。

Input

输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。

Output

如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。

Sample Input

1
5 5 14
S*#*.
.#...
.....
****.
...#.

..*.P
#.*..
***..
...*.
*.#..

Sample Output

YES

Source

HDU 2007-6 Programming Contest

解题:bfs搜索

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int dir[4][2] = {0,-1,-1,0,1,0,0,1};
int ex,ey,ez,n,m,t;
char mp[2][25][25];
bool dfs(int step,int x,int y,int z,int pre) {
if(step <= t && mp[x][y][z] == 'P') return true;
if(abs(y-ey)+abs(z-ez)+step > t) return false;
if(step >= t) return false;
for(int i = 0; i < 4; i++) {
int tx = x;
int ty = y+dir[i][0];
int tz = z+dir[i][1];
if(i == pre || mp[tx][ty][tz] == '*') continue;
if(mp[tx][ty][tz] == '#') {
if(tx) tx = 0;
else tx = 1;
if(dfs(step+1,tx,ty,tz,-1)) return true;
} else if(dfs(step+1,tx,ty,tz,3-i)) return true;
}
return false;
}
int main() {
int ks,i,j,k;
scanf("%d",&ks);
while(ks--) {
memset(mp,'*',sizeof(mp));
scanf("%d %d %d",&n,&m,&t);
for(i = 0; i < 2; i++) {
for(j = 1; j <= n; j++)
for(k = 1; k <= m; k++)
cin>>mp[i][j][k];
}
for(i = 1; i <= n; i++) {
for(j = 1; j <= m; j++) {
if(mp[0][i][j] == 'P'){
ex = 0;
ey = i;
ez = j;
}
if(mp[1][i][j] == 'P'){
ex = 1;
ey = i;
ez = j;
}
if(mp[0][i][j] == '#' && mp[1][i][j] == '#')
mp[0][i][j] = mp[1][i][j] = '*';
if(mp[0][i][j] == '#' && mp[1][i][j] == '*')
mp[0][i][j] = '*';
if(mp[0][i][j] == '*' && mp[1][i][j] == '#')
mp[1][i][j] = '*';
}
}
if(t > max(n,m)*3) t = max(n,m)*3;
dfs(0,0,1,1,-2)?puts("YES"):puts("NO");
}
return 0;
}


View Code
i

if(abs(y-ey)+abs(z-ez)+step > t) return false;//A*评估,小小的剪枝优化

if(t > max(n,m)*3) t = max(n,m)*3;//能防止TLE
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: