A计划
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 23 Accepted Submission(s) : 5
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是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
WA:第88行==少了一个等号。。。
#include<iostream>
#include<string>
#include<queue>
using namespace std;
const int MAX=15;
typedef struct
{
int x,y;
int floor,cost;
} node;
int N,M,T;
char map[2][MAX][MAX];
bool vi[2][MAX][MAX];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
void init()//读取原始数据
{
cin >> N >> M >> T ;
for(int c=0;c<2;c++)
{
for(int i=0;i<N;i++)
{
cin >> map[c][i];
}
}
}
void remake()//对数据的预处理
{
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
char &a=map[0][i][j],&b=map[1][i][j];
if(a=='#' && b=='#')
a = b = '*';
else if((a=='#' && b=='*') || (a=='*' && b=='#'))
a = b = '*';
}
}
}
int ok(int i,int j)//边界函数
{
if(i>=0&&i<N&&j>=0&&j<M)
return 1;
else return 0;
}
int bfs()//广搜
{
queue<node> Q;
node a,b;
a.x = a.y = a.floor = a.cost = 0 ;
vi[a.floor][a.x][a.y] = true;
Q.push(a);
while(!Q.empty())
{
a = Q.front();
Q.pop();
if(map[a.floor][a.x][a.y] == '#')//时空穿梭机
{
b.x = a.x; b.y = a.y; b.cost = a.cost; b.floor = !a.floor;
if(!vi[b.floor][b.x][b.y])//如果另一层是没有访问过的
{
if(map[b.floor][b.x][b.y]=='P')
{
if (b.cost <= T)
return true ;
else
return false;
}
vi[b.floor][b.x][b.y] = true;
Q.push(b);
}
continue;
}
for(int i = 0;i < 4;i++)//遍历
{
b.x = a.x + dir[i][0];
b.y = a.y + dir[i][1];
b.floor = a.floor;
b.cost = a.cost + 1;
if(ok(b.x,b.y) && map[b.floor][b.x][b.y] != '*' && !vi[b.floor][b.x][b.y])
{
if(map[b.floor][b.x][b.y] == 'P')
{
if (b.cost <= T)
return true ;
else
return false;
}
vi[b.floor][b.x][b.y] = true;
Q.push(b);
}
}
}
return false;
}
int main()
{
int C ;
cin >> C ;
while(C--)
{
memset(vi,false,sizeof(vi));//重置访问标记数组
init() ;//读取数据
remake() ;//预处理数据
if(bfs()) cout << "YES" << endl ;
else cout << "NO" << endl ;
}
return 0 ;
}