数据结构练习题――Hero In Maze 简单版
时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte
总提交:306 测试通过:85
总提交:306 测试通过:85
描述
500年前,Jesse是我国最卓越的剑客。他英俊潇洒,而且机智过人^_^。
突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中。Jesse听说这个消息已经是两天以后了,他急忙赶到迷宫,开始到处寻找公主的下落。
请你判断他是否能救出心爱的公主。(假设有路可以通到公主那就可以找到公主)。
输入
题目包括多组测试数据。
每组测试数据以两个整数n,m(0<n, m≤20)开头,分别代表迷宫的长和高。紧接着有m行,n列字符,由".","*","P","S"组成。其中
"." 代表能够行走的空地。
"*" 代表墙壁,Jesse不能从此通过。
"P" 是公主所在的位置。
"S" 是Jesse的起始位置。
Jesse只能选择“上、下、左、右”任意一方向走一步。
输入以0 0结束。
输出
如果能救出公主输出“YES”,否则输出“NO”。
样例输入
4 4
....
....
....
S**P
4 4
....
....
****
S**P
0 0
....
....
....
S**P
4 4
....
....
****
S**P
0 0
样例输出
YES
NO
NO
提示
输入0 0结束可以这样,
参考:
while(scanf("%d %d",&n,&m),m||n)
{
}
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<climits>
const size_t MAX_SIZE_X = 20; //地图大小
const size_t MAX_SIZE_Y = 20; //地图大小
const short int MAX_DIRECTION = 4; //遍历方向个数
const short int DIRECTION_X[MAX_DIRECTION] = { 0, 0, 1, -1 };
const short int DIRECTION_Y[MAX_DIRECTION] = { -1, 1, 0, 0 };
void show_map_sign(void); //显示地图
void show_map_step(void); //显示地图上所有走到该点所需的步数
void set_map(void); //地图的输入函数,及查找公主,王子的坐标
void dfs(const size_t& x, const size_t& y);
class Map{ //地图上每个点的类
char m_sign; //地图上该点的类型
unsigned int m_step; //走到该点所需的步数
public:
void set_sign(void){
cin >> m_sign;
m_step = UINT_MAX;
}
void set_step(unsigned int step){
m_step = step;
}
char sign(void){
return m_sign;
}
unsigned int step(void){
return m_step;
}
friend void show_map_sign(void);
friend void show_map_step(void);
};
Map map[MAX_SIZE_X][MAX_SIZE_Y]; //地图数组
size_t size_x, size_y; //地图大小
size_t princess_x, princess_y; //公主坐标
size_t prince_x, prince_y; //王子坐标
int main(void){
while (cin >> size_y >> size_x, size_x && size_y){
set_map();
map[prince_x][prince_y].set_step(0);
dfs(prince_x, prince_y);
cout << (map[princess_x][princess_y].step() == UINT_MAX ? "NO" : "YES") << endl;
}
return 0;
}
void show_map_sign(void){
cout << "以下是地图情况:" << endl;
size_t x, y;
for (x = 0; x != size_x; ++x){
for (y = 0; y != size_y; ++y){
cout << map[x][y].m_sign << " ";
}
cout << endl;
}
cout << "-----------------------------------\n";
}
void show_map_step(void){
size_t x, y;
cout << "以下是走到各点所需的天数情况:" << endl;
for (x = 0; x != size_x; ++x){
for (y = 0; y != size_y; ++y){
cout << map[x][y].m_step << " ";
}
cout << endl;
}
cout << "-----------------------------------\n";
}
void set_map(void){
size_t x, y;
for (x = 0; x != size_x; ++x){
for (y = 0; y != size_y; ++y){
map[x][y].set_sign();
if (map[x][y].sign() == 'P'){
princess_x = x;
princess_y = y;
}
else if (map[x][y].sign() == 'S'){
prince_x = x;
prince_y = y;
}
}
}
}
void dfs(const size_t& x, const size_t& y){
size_t current_x, current_y;
unsigned int temp;
for (short int direction(0); direction != MAX_DIRECTION; ++direction){
current_x = x + DIRECTION_X[direction];
current_y = y + DIRECTION_Y[direction];
temp = map[x][y].step() + 1;
if ((current_x >= 0 && current_x < size_x)
&& (current_y >= 0 && current_y < size_y)
&& map[current_x][current_y].sign() != '*'
&& map[current_x][current_y].step() > temp)
{
map[current_x][current_y].set_step(temp);
dfs(current_x, current_y);
}
}
}