hdu 1728 escape the labyrinth (bfs deformation)

Problem Description
  Given an m × n (m rows, n columns) maze, there are two positions in the maze, gloria wants to go from one position to another in the maze, of course, some places in the maze are open spaces, gloria can pass through, some places It is an obstacle, and she has to go around it. From one position in the maze, she can only go to the 4 positions adjacent to it. 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 given locations are open spaces. Initially, the direction Gloria is facing is not determined, and she can choose any of the 4 directions to start, which does not count 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 Only these two kinds of 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
 
  
25 5...***.**...........*....1 1 1 1 35 5...***.**...........*....2 1 1 1 3
 
Sample Output
 
  
noyes

 Thought analysis:

      This problem is the minimum number of turns required to reach another point from a certain point in a graph, so I immediately thought that it could be solved with bfs, but this problem is not the common type of bfs in the past, this problem needs to start from the starting point four Go in one direction to the end, then put all the points into the team, and then repeat the operation of these points to update other related points. It should be noted that the situation where the starting point and the focus overlap need to be judged. The following is attached. Above code:

#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<string.h>
#include<stdio.h>
using  namespace std;
const int INF=0x3f3f3f3f;
const int maxn=102;
typedef long long ll;
char a[maxn][maxn];
int point[maxn][maxn];
struct node
{int x,y;
};
queue<node>q;
int m,n;
int dx[5]={0,1,-1,0,0};
int dy[5]={0,0,0,1,-1};
bool search(int x1,int y1,int c,int b,int k)//Search in four directions into the queue.
{ for(int i=1;i<=4;i++)
  { int x=x1+dx[i],y=y1+dy[i];
    while(true)
    {if(x>n||x<1||y>m||y<1||a[y][x]=='*') break;
     if(point[x][y]>point[x1][y1]+1)
	 {point[x][y]=point[x1][y1]+1;
	  if(point[c][b]<=k) return true;
	  q.push(node{x,y});
	 }
     x=x+dx[i];
     y = y + dy [i];
   }
  }
  return false;
}
intmain()
{ int t,k,x1,y1,x2,y2;
  scanf("%d",&t);
  while(t--)
  {   memset(point,0x3f,sizeof(point));
      scanf("%d%d",&m,&n);
      for(int i=1;i<=m;i++)scanf("%s",a[i]+1);
      scanf("%d%d%d%d%d",&k,&x1,&y1,&x2,&y2);
      if(x1==x2&&y1==y2)
       { printf("yes\n");
        continue;
        }
    q.push(node{x1,y1});//Enqueue.
    point[x1][y1]=-1;
    bool wang=false;
    while(!q.empty())
    {node d=q.front();q.pop();
	 int x=d.x,y=d.y;
	 if(point[x][y]>=k)break;
	 if(search(x,y,x2,y2,k))
	  {wang=true;break;
	  }
     }
	if(wang) printf("yes\n");
	else printf("no\n");
	while(!q.empty())q.pop();
  }
}

Guess you like

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