Hdu 1728 escape from the maze

escape the maze

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 26874    Accepted Submission(s): 6550

Problem Description

  Given a maze of m × n (m rows, n columns), there are two positions in the maze, and gloria wants to go from one position to another in the maze. Of course, some places in the maze are open spaces, and gloria can pass through some places. It is an obstacle, she has to go around, from one position in the maze, she can only go to the 4 adjacent positions. Of course, during the walking process, Gloria cannot go outside the maze. The headache is that gloria is a person with no sense of direction, so she can't make too many turns while walking, otherwise she will faint. We assume that the two positions given are open spaces. Initially, the direction Gloria is facing is undetermined, and she can choose any of the 4 directions to start, not counting as a turn. Can gloria go from one location to another?

 

 

Input

  The first line is an integer t (1 ≤ t ≤ 100), indicating the number of test data, followed by t groups of test data, in each group of test data,
  the first line is two integers m, n (1 ≤ m, n ≤ 100), representing the number of rows and columns of the maze respectively, the next m rows, each row includes n characters, where the character '.' indicates that the position is an open space, and the character '*' indicates that the position is an obstacle, in the input data There are only these two characters, the last line of each set of test data is 5 integers k, x 1 , y 1 , x 2 , y 2  (1 ≤ k ≤ 10, 1 ≤ x 1 , x 2  ≤ n, 1 ≤ y 1 , y 2  ≤ m), where k represents the maximum number of turns that gloria can turn, (x 1 , y 1 ), (x 2 , y 2 ) represent two positions, where x 1 , x 2 correspond to columns, y 1 , y 2 corresponds to the row.

 

 

Output

  Each set of test data corresponds to a row. If gloria can go from one position to another, output "yes", otherwise output "no".

 

 

Sample Input

2

5 5

...**

*.**.

.....

.....

*....

1 1 1 1 3

5 5

...**

*.**.

.....

.....

*....

2 1 1 1 3

 

 

Sample Output

no

yes

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
using namespace std;
const int SIZE = 110;
const int dx[] = {1, -1, 0, 0};
const int dy [] = {0, 0, -1, 1};

char maze[SIZE][SIZE];
int v[SIZE][SIZE];
int ex, ey, bx, by;
int m, n, limit;

struct node
{
    int x, y;
    int step;
};

queue<node> Q;

int check(int x, int y)
{
    if(x >= 1 && y >= 1 && x <= m && y <= n && maze[x][y] != '*')
        return 1;
    return 0;
}


void init()
{
    memset(v, 0, sizeof(v));
    memset(maze, 0, sizeof(maze));
    while(!Q.empty()) Q.pop();
}


 void bfs(int bx, int by)
{
    if(bx == ex && by == ey)
    {
        printf("yes\n");
        return ;
    }
    v[bx][by] = 1;
    node in, out;
    in.x = bx; in.y = by;
    in.step = -1;
    Q.push(in);
    while(!Q.empty())
    {
        out = Q.front();
        Q.pop();
        int x = out.x;
        int y = out.y;
        for(int i = 0; i < 4; i++)
        {
            int xx = x + dx[i];
            int yy = y + dy[i];
            while(check(xx, yy))
            {
                if(!v[xx][yy])
                {
                    in.x = xx;
                    in.y = yy;
                    in.step = out.step+1;
                    Q.push(in);
                    v[xx][yy] = 1;
                    if(xx == ex && yy == ey && in.step <= limit) //Note that the judgment condition is here. {
                        printf("yes\n");
                        return ;
                    }
                }
                xx += dx[i];
                yy += dy[i];
            }
        }
    }
    printf("no\n");
    return;
}



intmain()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        init();
        scanf("%d%d", &m, &n);
        for(int i = 1; i <= m; i++)     
        {
            for(int j = 1; j <= n; j++) //It starts from 1 and cannot be entered as a string. {
                scanf(" %c", &maze[i][j]);
            }        
        }
        scanf("%d%d%d%d%d", &limit, &by, &bx, &ey, &ex);
        bfs(bx, by);
    }
    return 0;
}

  

Guess you like

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