Tagged DFS

Tagged DFS

Subject link: https://codeforces.com/contest/616/problem/C

Question:
Give you a matrix of n rows and m columns, matrix * represents obstacles, and. Represents an open space.
For each obstacle, let you output after removing the obstacle (this point is counted as an empty point), the size of the connected block at this point is How much
Note: Each obstacle point is handled separately and the answer needs %10

Idea:
At first I thought of BFS directly, but this should be T, so I wonder if I can change the method.

Positive solution:
Find the number of squares of each interconnection block, so that when finding the size of the interconnection block corresponding to the obstacle, you can directly calculate the size of the interconnection block around it.

Note:
1. For each obstacle, the connected blocks from four adjacent grids may be the same. In this case, an f [][] array can be used to record which is the first connected block to remove duplicates
2. Every time dfs searches, put empty points into the queue and update the number of points already searched at any time (the last is the Unicom block size). After this dfs is completed, this Unicom block size is assigned to each point in the queue.

AC code:

#include<iostream>
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
#include<map>
using namespace std;
#define scan(n) scanf("%d",&n)
#define ll long long

struct ss
{
    
    
    int r,c;
};
typedef struct ss ss;

queue<ss> q;
int a[1005][1005];
int num[1005][1005];
int r[1005][1005];
int f[1005][1005];
int cnt;
int n,m;
int tot=1;

void dfs(int i,int j)
{
    
    
    if(i<1||i>n||j<1||j>m)
        return;
    if(num[i][j])
        return;
    if(a[i][j]==1)
        return;
    ss s;
    s.r=i;
    s.c=j;
    q.push(s);
    cnt++;
    num[i][j]=1;
    dfs(i+1,j);
    dfs(i-1,j);
    dfs(i,j+1);
    dfs(i,j-1);
    return;
}

int main()
{
    
    
    int i,j;
    char ch;
    cin>>n>>m;
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
        {
    
    
            cin>>ch;
            if(ch=='*')
                a[i][j]=1;
            else
                a[i][j]=0;
        }
    memset(num,0,sizeof(num));
    for(i=1;i<=n;i++)
    {
    
    
        for(j=1;j<=m;j++)
        {
    
    
            if(a[i][j]==0&&num[i][j]==0)
            {
    
    
                cnt=0;
                dfs(i,j);
                while(!q.empty())
                {
    
    
                    ss s=q.front();
                    q.pop();
                    int rr=s.r;
                    int cc=s.c;
                    num[rr][cc]=cnt;
                    f[rr][cc]=tot;
                }
                tot++;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
    
    
        for(j=1;j<=m;j++)
        {
    
    
            r[i][j]=num[i-1][j];
            int f1=f[i-1][j],f2=f[i+1][j],f3=f[i][j-1],f4=f[i][j+1];
            if(f2!=f1)
                r[i][j]+=num[i+1][j];
            if(f3!=f1&&f3!=f2)
                r[i][j]+=num[i][j-1];
            if(f4!=f1&&f4!=f2&&f4!=f3)
                r[i][j]+=num[i][j+1];
            r[i][j]++;
            r[i][j]%=10;
        }
    }
    for(i=1;i<=n;i++)
    {
    
    
        for(j=1;j<=m;j++)
        {
    
    
            if(a[i][j]==1)
                cout<<r[i][j];
            else
                cout<<'.';
        }
        cout<<endl;
    }
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_40534166/article/details/97816262
dfs