Hero In Maze

Hero In Maze

时间限制(普通/Java):1000MS/10000MS          运行内存限制:65536KByte
总提交:377            测试通过:86

描述

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 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;                //地图大小
unsigned int day;                    //公主能坚持的天数
size_t princess_x, princess_y;        //公主坐标
size_t prince_x, prince_y;            //王子坐标

int main(void){
    while (cin >> size_y >> size_x >> day, size_x && size_y && day){
        set_map();
        map[prince_x][prince_y].set_step(0);
        dfs(prince_x, prince_y);
        cout << (map[princess_x][princess_y].step() > day ? "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);
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值