Roast Lezi Escape

Title Description
Roolez is a mischievous little boy. He didn't need to impress everyone, so he abandoned himself and broke the glass in the classroom of Tuyang Middle School. The director of the teaching office of Tuyang Middle School grabbed a paddle and rushed over, and Roolezi had to escape to the school's grove.
The school’s groves are not very dense due to negligence in management, and there are relatively few big trees for concealment. Roa Lezi knew that the closer he was to the nearest tree, the greater the probability that he would escape the dean's chase. But the school’s small forest area is a bit big, and Roose knows that if he finds the tree closest to each location, it will take a lot of time, and his life will not be saved. The grove of the school can be abstracted into a grid of n m. Each grid of 1 1 is a position, and each position has three forms: tree, open space, and obstacle. A grid with a distance of 1 from a grid is defined as its up, down, left, and right Of four lattices. (Except for the border), you can't go to the grid with obstacles.

The distance between two grids is defined as the shortest path length between the two grids without obstacles. If the two grids are unreachable, then we define the distance between them as "GoDie".

You ask for the risk value of each grid. The hazard value of a non-obstacle grid is defined as the smallest distance between it and all trees. In the process of finding the minimum value, you can regard GoDie as a value greater than all other distances. If the distance between this grid and all trees is GoDie, then the dangerous value of this grid will also be GoDie.

According to the above definition, it is clear that the dangerous value of the tree grid is 0.
Now Roolz has a specific map of the school grove. He wants you to tell him the danger value of each location, so that he can choose the best hidden place.

Enter
the two numbers n, m in the first line. Indicates the size of the school grove.
The next n rows, each with m characters, describe this grid.
Among them, "." represents an open space-"*" represents a tree, and "#" represents an obstacle.
Ensure that there are no extra spaces, and that there is at least one tree

Output
Output n rows, each with m components. For the j-th component of the i-th row: If the grid is a non-obstacle grid, output its risk value, namely the minimum distance or "GoDie". If the grid is a barrier grid, the output "ovo" does not contain quotation marks.
Each component is separated by a space.

*…
…#.
#
#…

0 1 2 3 4
0 1 2 egg 5
egg 0 egg 7 6

Tip
For 30% of the data, n,m ≤ 10.
For 70% of the data, n,m ≤ 50.
For 100% data, n,m ≤ 1000.

Explosive search questions, put each tree position into the team, and then use the tree position BFS to deduce the distance of each empty position to the tree position

#include<bits/stdc++.h>
using namespace std;
char a[1500][1500];
int ans[1500][1500],vis[1500][1500];
int n,m;
int dx[4]={
    
    0,0,1,-1};//移动方向
int dy[4]={
    
    1,-1,0,0};//移动方向
struct node 
{
    
    
    int x;
    int y;
};
queue<node>q;
int main()
{
    
    
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    {
    
    
      cin>>a[i][j];
      if(a[i][j]=='*')//树的位置
      {
    
    
        ans[i][j]=0;
        node st;st.x=i;st.y=j;
        q.push(st);
        vis[i][j]=1;
      }
      if(a[i][j]=='#')ans[i][j]=-1;
      if(a[i][j]=='.')ans[i][j]=1e9;
    }
    while(!q.empty())
    {
    
    
        node ne=q.front();q.pop();
        for(int i=0;i<4;i++)//朝四个方向运动
        {
    
    
            int xx=ne.x+dx[i];
            int yy=ne.y+dy[i];
            if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&a[xx][yy]!='#'&&!vis[xx][yy])//如果没有走过,没有超边界 不是陷阱
            {
    
    
                vis[xx][yy]=1;
                node k;k.x=xx;k.y=yy;
                q.push(k);
                ans[xx][yy]=ans[ne.x][ne.y]+1;//更新ans
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
    
    
        for(int j=1;j<=m;j++)
        {
    
    
            if(a[i][j]=='*')cout<<0<<' ';//是树
            if(a[i][j]=='.')
            if(ans[i][j]==1e9)cout<<"GoDie ";//不可以到
            else cout<<ans[i][j]<<' ';
            if(a[i][j]=='#')cout<<"ovo ";
        }
        cout<<endl;
    }
}

Guess you like

Origin blog.csdn.net/yhhy666/article/details/108220706