站外题求助,玄关
  • 板块灌水区
  • 楼主x_q_p
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/5/1 23:01
  • 上次更新2024/5/1 23:12:16
查看原帖
站外题求助,玄关
401293
x_q_p楼主2024/5/1 23:01

这是题解的代码:

# include<bits/stdc++.h>
# define int long long
using namespace std;
const int N = 2e3 + 10;
char a[N][N];
int n , m , cnt , f[N][N] , sz[N] , vis[N][N] , x[] = {1 , -1 , 0 , 0} , y[] = {0 , 0 , -1 , 1};
void bfs(int sx ,int  sy){
	queue<pair<int , int>> que;
	que.push({sx , sy});
	sz[cnt] = 1;
	f[sx][sy] = cnt;
	vis[sx][sy] = 1;
	while(!que.empty()){
		auto top = que.front();
		for(int i = 0 ; i < 4 ; i ++){
			int nx = top.first + x[i] , ny = top.second + y[i];
			if(nx < 1 || ny < 1 || nx > n || ny > m) continue;
			if(vis[nx][ny] || a[nx][ny] == '*') continue;
			vis[nx][ny] = 1;
			f[nx][ny] = cnt ;
			sz[cnt] ++;
			que.push({nx , ny}) ;
		}
		que.pop();
	}
}
signed main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m ;
	for(int i = 1 ; i <= n ; i ++)for(int j = 1 ; j <= m ; j ++) cin >> a[i][j];
	for(int i = 1 ; i <= n ; i ++) for(int j = 1 ; j <= m ; j ++) if(a[i][j] == '.' && !vis[i][j]) cnt ++ , bfs(i , j);
	for(int i = 1 ; i <= n ; i ++){
		for(int j = 1 ; j <= m ; j ++){
			if(a[i][j] == '.') cout << '.';
			else{
				int under = f[i - 1][j] , on = f[i + 1][j] , left = f[i][j - 1] , right = f[i][j + 1];
				if(under == on) under = 0;
				if(under == left) under = 0 ;
				if(under == right) under = 0 ;
				if(on == left) on = 0 ;
				if(on == right) on = 0 ;
				if(left == right) left = 0;
				cout << (sz[under] + sz[on] + sz[left] + sz[right] + 1 ) % 10;
			}
		}
		cout << '\n';
	}
	return 0 ;
}

然后这是我的代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int dx[4]={0,0,-1,1},dy[4]={1,-1,0,0};
char a[2005][2005];
int n,m,t=0,f[2005][2005],s[2005],vis[2005][2005];
void bfs(int x,int y)
{
	queue<pair<int,int>>q;
	q.push({x,y});
	s[t]=1;
	f[x][y]=t;
	vis[x][y]=1;
	while(!q.empty())
	{
		auto p=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			int ax=p.first+dx[i],ay=p.second+dy[i];
			if(ax<1||ax>n||ay<1||ay>m||vis[ax][ay]==1||a[ax][ay]=='*') continue;
			vis[ax][ay]=1;
			f[ax][ay]=t;
			s[t]++;
			q.push({ax,ay});
		}
	}
}
signed main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>a[i][j];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if(a[i][j]=='.'&&vis[i][j]==0)
			{
				t++;
				bfs(i,j);
			}
		}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(a[i][j]=='.') cout<<'.';
			else
			{
				int d=f[i-1][j],u=f[i+1][j],l=f[i][j-1],r=f[i][j+1];
				if(d==u||d==l||d==r) d=0;
				if(u==l||u==r) u=0;
				if(l==r) l=0;
				cout<<(s[d]+s[u]+s[l]+s[r]+1)%10;
			}
		}
		cout<<endl;
	}
	return 0;
}

我真的调了一个多点了,就是找不出来哪里错了,求求各位大佬们帮我看一下吧

2024/5/1 23:01
加载中...