The original problem is in this page
https://vjudge.net/problem/306223/origin
And let’s skip to the point. The is a DFS problem and considered that the number could be very large,this time I mark every reachable blocks with same numbers if they’re adjacent.Only if I realize the mistakes I’ve made can I make something different.
#include <iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int N,M;
char m[1005][1005];
int vis[1005][1005];
int c[1005][1005];
int area[1005*1005];
int cnt;
void dfs(int x,int y)
{
vis[x][y]=1;
c[x][y]=cnt;
area[cnt]++;
for(int i=0;i<4;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(xx>=0&&yy>=0&&xx<N&&yy<M&&!vis[xx][yy]&&m[xx][yy]=='.')
{
dfs(xx,yy);
}
}
}
int main()
{
cin>>N>>M;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
cin>>m[i][j];
}
}
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
if(m[i][j]=='.'&&vis[i][j]==0)
{
cnt++;
dfs(i,j);
}
}
}
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
if(m[i][j]=='.')
{
cout<<".";
}
else
{
int ans,x,y;
int d[5];
for (int k = 0; k < 4; k++)
{
x = i + dx[k], y = j + dy[k];
d[k] = c[x][y];
}
sort(d, d + 4);
ans = 1 + area[d[0]];
for (int k = 1; k < 4; k++)
{
if (d[k] != d[k - 1]) {ans += area[d[k]];}
}
cout<<ans%10;
}
}
cout<<endl;
}
return 0;
}