hero in maze

描述

        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;
}

------------------------------

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
typedef struct
{
 int X;
 int Y;
}Point;
Point A[400];
int BFS(char buff[][22],int T,int x,int y,int n,int m)
{
 int tear=0;
 int first=0;
 A[tear].X=x;
 A[tear].Y=y;
 tear++;
 int p;
 buff[x][y]='*';
 while(T>0)
 {
   p=tear;
   for(int j=first;j!=p;j=(j+1)%400)
   {
    x=A[j].X;
    y=A[j].Y;
    if(x-1>=0&&buff[x-1][y]!='*')
    {
     if(buff[x-1][y]=='P')
     {
      return 1;
     }
     A[tear].X=x-1;
     A[tear].Y=y;
     tear=(tear+1)%400;
     buff[x-1][y]='*';
    }
    if(y-1>=0&&buff[x][y-1]!='*')
    {
     if(buff[x][y-1]=='P')
      return 1;
     A[tear].X=x;
     A[tear].Y=y-1;
     tear=(tear+1)%400;
     buff[x][y-1]='*';
    }
    if(x+1<m&&buff[x+1][y]!='*')
    {
     if(buff[x+1][y]=='P')
      return 1;
     A[tear].X=x+1;
     A[tear].Y=y;
     tear=(tear+1)%400;
     buff[x+1][y]='*';
    }
    if(y+1<n&&buff[x][y+1]!='*')
    {
     if(buff[x][y+1]=='P')
      return 1;
     A[tear].X=x;
     A[tear].Y=y+1;
     tear=(tear+1)%400;
     buff[x][y+1]='*';
    }
   }
   first=p;
   T--;
 }
 return 0;
}
int main()
{
 int n,m,T;
 char buff[22][22];
 while(scanf("%d %d %d",&n,&m,&T)&&n!=0&&m!=0&&T!=0)
 {
   int flag=0;
   int x,y;
   for(int i=0;i<m;i++)
   {
    scanf("%s",buff[i]);
    if(flag==0)
     for(int j=0;j<n;j++)
     {
      if(buff[i][j]=='S')
      {
       x=i;
       y=j;
       flag=1;
       break;
      }
     }
   }
   if(BFS(buff,T,x,y,n,m)==1)
    printf("YES\n");
   else
    printf("NO\n");
 }
 return 0;
}

----------------------------

#include<iostream>
 2#include<queue>
 3using namespace std;
 4char a[105][105];
 5bool f[105][105];
 6int dist[105][105];
 7struct point 
 8{ int x,y; };
 9
10bool valid(int x,int y)
11{ return !f[x][y]&&a[x][y]!='*'&&a[x][y]; }
12
13void bfs(int i,int j)
14{
15     dist[i][j]=0; f[i][j]=1;
16     struct point tem;
17     tem.x=i; tem.y=j;
18     queue<struct point>q;
19     q.push(tem);
20      while(q.size())
21      {
22         tem=q.front();q.pop(); 
23         int x=tem.x,y=tem.y;
24         if(valid(x-1,y)){f[x-1][y]=1;  dist[x-1][y]=dist[x][y]+1;tem.x=x-1; tem.y=y; q.push(tem);}
25         if(valid(x,y-1)){f[x][y-1]=1;  dist[x][y-1]=dist[x][y]+1;tem.x=x; tem.y=y-1; q.push(tem);}
26         if(valid(x+1,y)){f[x+1][y]=1;  dist[x+1][y]=dist[x][y]+1;tem.x=x+1; tem.y=y; q.push(tem);}
27         if(valid(x,y+1)){f[x][y+1]=1;  dist[x][y+1]=dist[x][y]+1;tem.x=x; tem.y=y+1; q.push(tem);}
28      }
29    
30}
31
32int main()
33{
34    int n,m,t,i,j,u;
35    int x,y,x2,y2;
36    while(cin>>m>>n>>t,n||m||t)
37    {
38      memset(a,0,sizeof a);
39      memset(f,0,sizeof f);
40      memset(dist,0,sizeof dist);
41      x=y=x2=y2=1;
42      for(i=1; i<=n; i++)
43      for(j=1; j<=m;j++)
44       {
45                cin>>a[i][j];
46                if(a[i][j]=='S'){x=i; y=j;}
47                else if(a[i][j]=='P'){x2=i; y2=j; }
48       }
49    
50       bfs(x,y);
51   
52      if(x==x2&&y==y2)cout<<"YES"<<endl;
53      else if(dist[x2][y2]==0)cout<<"NO"<<endl;
54      else if(dist[x2][y2]<=t)cout<<"YES"<<endl;
55      else cout<<"NO"<<endl;
56    }
57 
58   return 0;   
59}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值