Hero In Maze (BFS wide search)

Description

 

500 years ago, Jesse was the most prominent swordsman in our country. He is handsome and witty ^_^.
Suddenly one day, Jesse's beloved princess is trapped in a huge labyrinth by the devil. It has been two days since Jesse heard the news. He knew that the princess could last in the labyrinth for T days, so he hurried to the labyrinth and started looking for the whereabouts of the princess. Time passed and Jesse still couldn't find the princess. When he finally found the princess, the beautiful princess was already dead. Since then, Jesse has been unhappy, not thinking about tea and rice, and followed the princess a year later. T_T Today, 500 years later, Jesse asks you a dream, I hope you can help him judge whether he has a chance to find the princess in the given time.
He will give you the map of the maze and the time T left. Please judge whether he can rescue the beloved princess.

 

Input

 

Questions include multiple sets of test data. Each set of test data starts with three integers N, M, T (0<n, m≤20, t>0), which represent the length and height of the maze, and the number of days the princess can last. It is followed by M lines and N columns of characters, consisting of ".", "*", "P", and "S". Where "." represents a walkable open space. "*" represents a wall, through which Jesse cannot pass. "P" is where the princess is. "S" is Jesse's starting position. In each time period, Jesse can only choose to take a step in any direction of "up, down, left, right". Input ends with 0 0 0.

 

Output

 

If the princess can be rescued within the specified time, output "YES", otherwise output "NO".

 

Sample Input

4 4 10
....
....
....
S**P
0 0 0

Sample Output

YES 


problem-solving ideas:
Breadth-first search is implemented using a queue, and the whole process can also be seen as an upside-down tree:
1. Put the root node at the end of the queue.
2. Take an element from the head of the queue at a time, look at all the next-level elements of this element, and put them at the end of the queue. And record this element as the predecessor of its next-level element.
3. End the program when the element you are looking for is found.
4. If traversing the entire tree does not find it, end the program.
#include <iostream>
#include <stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char map[25][25];
int vis[25][25];
int dir[4][2]= { {1,0},{-1,0},{0,1},{0,-1} };
int n,ans,m,num,c,d;
struct state
{
    int x;
    int y;
    int count;
};
int   bfs ( int x, int y)
{
    queue<state>p;
    struct state now,t,beg;
    beg.x=x;
    beg.y = y;
    beg.count=0;
    vis[x][y] = 1 ;
    p.push(beg);
    while(!p.empty())
    {
        int i,a,b;
        t=p.front();
        if(t.x==c&&t.y==d)
        {
            return t.count;
        } /// Princess found 
        for (i= 0 ; i< 4 ; i++ )
        {
            a = t.x + dir [i] [ 0 ];
            b=t.y+dir[i][1];
            if(a>=1&&a<=m&&b>=1&&b<=n&&vis[a][b]==0 )
            {
                now.count=t.count+1;
                force[a][b ] = 1
                now.x=a;
                now.y=b;
                p.push(now);
            }
        }
        p.pop();
    }
    return  10000000 ; /// If the queue has been emptied (it can be considered that the swordsman has gone to a dead end and cannot reach the place where the princess is imprisoned, return a very large number) 
}

intmain ()
{
    while(scanf("%d%d%d",&n,&m,&num)!=EOF)
    {
        if(n==0&&m==0&&num==0)
            break;
        int i,j,a,b;
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        for(i=1; i<=m; i++)
        {
            for(j=1; j<=n; j++)
            {
                scanf( " %c " ,& map[i][j]);
                 if (map[i][j]== ' S ' ) /// starting position 
                {
                    a=i;
                    b=j;
                }
                else  if (map[i][j]== ' * ' ) /// * represents a wall, cannot pass 
                    vis[i][j]= 1 ;
                 else  if (map[i][j]== ' P ' ) /// Princess's position 
                {
                    c=i;
                    d = j;
                }
            }
        }
        ans=bfs(a,b);
        if(ans>num)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324673716&siteId=291194637