Hero In Maze

                                                                  Hero In Maze

时间限制(普通/Java):1000MS/10000MS     运行内存限制:65536KByte
总提交: 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;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值