Hero In Maze
總提交: 1335 測試通過: 340
描述
500年前,Jesse是我國最卓越的劍客。他英俊瀟灑,而且機智過人^_^。
突然有一天,Jesse心愛的公主被魔王困在了一個巨大的迷宮中。Jesse聽說這個消息已經是兩天以後了,他知道公主在迷宮中還能堅持T天,他急忙趕到迷宮,開始到處尋找公主的下落。
時間一點一點的過去,Jesse還是無法找到公主。最後當他找到公主的時候,美麗的公主已經死了。從此Jesse鬱鬱寡歡,茶飯不思,一年後追隨公主而去了。T_T
500年後的今天,Jesse託夢給你,希望你幫他判斷一下當年他是否有機會在給定的時間內找到公主。
他會爲你提供迷宮的地圖以及所剩的時間T。請你判斷他是否能救出心愛的公主。
輸入
題目包括多組測試數據。
每組測試數據以三個整數N,M,T(0<n, m≤20, t>0)開頭,分別代表迷宮的長和高,以及公主能堅持的天數。
緊接着有M行,N列字符,由".","*","P","S"組成。其中
"." 代表能夠行走的空地。
"*" 代表牆壁,Jesse不能從此通過。
"P" 是公主所在的位置。
"S" 是Jesse的起始位置。
每個時間段裏Jesse只能選擇“上、下、左、右”任意一方向走一步。
輸入以0 0 0結束。
輸出
如果能在規定時間內救出公主輸出“YES”,否則輸出“NO”。
樣例輸入
4 4 10
....
....
....
S**P
0 0 0
樣例輸
YES
#include <iostream>
using namespace std;
#include <cstdlib>
#include <cstdio>
const int INIT_SIZE = 10000;
///==========================queue========================
template <typename T>
class Queue
{
private:
T _queue[INIT_SIZE];
int front,rear;
public:
Queue();
bool EnQueue(T e); //入隊
bool DeQueue(T &e); //出隊
inline bool IsEmpty();
};
template <typename T>
Queue<T>::Queue()
{
front = rear = 0;
}
template <typename T>
bool Queue<T>::EnQueue(T e)
{
if((rear + 1) % INIT_SIZE == front)
return false;
_queue[rear] = e;
rear = (rear + 1) % INIT_SIZE;
return true;
}
template <typename T>
bool Queue<T>::DeQueue(T &e)
{
if(front == rear)
return false;
e = _queue[front];
front = (front + 1)%INIT_SIZE;
return true;
}
template <typename T>
inline bool Queue<T>::IsEmpty()
{
return rear == front;
}
///=======================================================
struct Coordinate
{
int x,y;
};
class RescueThePrincess
{
private:
int N,M,T; //N,M迷宮的長和高;T公主能存活的天數
int **mazeGraphics;//輸入的迷宮圖
struct Coordinate cavalier,princess;//
public:
RescueThePrincess();//初始化迷宮圖
~RescueThePrincess();
void DisplayMazeGraphics();
bool FindTheShortestPath();//尋找解救公主的最短路徑
} ;
RescueThePrincess::RescueThePrincess()
{
cin>>N>>M>>T;
getchar();
if(N == 0 && M == 0 && T == 0)
exit(EXIT_FAILURE);
mazeGraphics = (int**)calloc(M,sizeof(int*));
for(int i = 0; i < M; ++i)
{
mazeGraphics[i] = (int*)calloc(N,sizeof(int));
for(int j = 0; j < N; ++j)
{
char cTemp;
cin>>cTemp;
switch(cTemp)
{
case '.':
mazeGraphics[i][j] = 0; //可通過
break;
case '*':
mazeGraphics[i][j] = -1;//牆
break;
case 'P':
mazeGraphics[i][j] = 0; //公主的位置
princess.x = j;
princess.y = i;
break;
case 'S':
mazeGraphics[i][j] = 0; //騎士的位置
cavalier.x = j;
cavalier.y = i;
break;
default:
exit(EXIT_FAILURE);
}
}
getchar();
}
}
RescueThePrincess::~RescueThePrincess()
{
for(int i = 0; i < M; ++i)
free(mazeGraphics[i]);
free(mazeGraphics);
mazeGraphics = NULL;
}
void RescueThePrincess::DisplayMazeGraphics()
{
for(int i = 0; i < M; i++)
{
for(int j = 0; j < N; ++j)
cout<<mazeGraphics[i][j]<<' ';
cout<<endl;
}
}
bool RescueThePrincess::FindTheShortestPath()
{
Queue<Coordinate> Q;
Q.EnQueue(cavalier);
while(!Q.IsEmpty())
{
Coordinate e;
Q.DeQueue(e);
if(e.y - 1 >= 0 && mazeGraphics[e.y - 1][e.x] != -1) //shang
{
if(mazeGraphics[e.y - 1][e.x] > (mazeGraphics[e.y][e.x] + 1) || mazeGraphics[e.y - 1][e.x] == 0)
{
mazeGraphics[e.y - 1][e.x] = mazeGraphics[e.y][e.x] + 1;
Coordinate temp;
temp.x = e.x;
temp.y = e.y - 1;
Q.EnQueue(temp);
}
}
if(e.x + 1 < N && mazeGraphics[e.y][e.x + 1] != -1) //you
{
if(mazeGraphics[e.y][e.x + 1] > (mazeGraphics[e.y][e.x] + 1) || mazeGraphics[e.y][e.x + 1] == 0)
{
mazeGraphics[e.y][e.x + 1] = mazeGraphics[e.y][e.x] + 1;
Coordinate temp;
temp.x = e.x + 1;
temp.y = e.y;
Q.EnQueue(temp);
}
}
if(e.y + 1 < M && mazeGraphics[e.y + 1][e.x] != -1) //xia
{
if(mazeGraphics[e.y + 1][e.x] > (mazeGraphics[e.y][e.x] + 1) || mazeGraphics[e.y + 1][e.x] == 0)
{
mazeGraphics[e.y + 1][e.x] = mazeGraphics[e.y][e.x] + 1;
Coordinate temp;
temp.x = e.x;
temp.y = e.y + 1;
Q.EnQueue(temp);
}
}
if(e.x - 1 >= 0 && mazeGraphics[e.y][e.x - 1] != -1) //zuo
{
if(mazeGraphics[e.y][e.x - 1] > (mazeGraphics[e.y][e.x] + 1) || mazeGraphics[e.y][e.x - 1] == 0)
{
mazeGraphics[e.y][e.x - 1] = mazeGraphics[e.y][e.x] + 1;
Coordinate temp;
temp.x = e.x - 1;
temp.y = e.y;
Q.EnQueue(temp);
}
}
}
if(mazeGraphics[princess.y][princess.x] != 0 && mazeGraphics[princess.y][princess.x] <= T - 2)
return true;
else
return false;
}
int main(void)
{
while(true)
{
RescueThePrincess* problem = new RescueThePrincess();
if(problem->FindTheShortestPath())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
problem->~RescueThePrincess();
}
return 0;
}