616C - The Labyrinth - CodeForces Solution


dfs and similar *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
int t,n,m,cnt,vis[1010][1010],num[1000010],ans[1010][1010];
char ma[1010][1010];
int dfs(int x,int y,int id){
	if(vis[x][y]==id||ma[x][y]!='.')return 0;
	else vis[x][y]=id;
	return 1+dfs(x-1,y,id)+dfs(x+1,y,id)+dfs(x,y-1,id)+dfs(x,y+1,id);
}
int cal(int a,int b,int c,int d){
	int sum=1;
	sum+=num[a];
	if(b!=a) sum+=num[b];
	if(c!=b&&c!=a)sum+=num[c];
	if(d!=c&&d!=b&&d!=a)sum+=num[d];
	return sum%10;
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i)cin>>ma[i]+1;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if(ma[i][j]=='.'&&vis[i][j]==0)++cnt,num[cnt]=dfs(i,j,cnt);
	for(int i=1;i<=n;++i) {
		for(int j=1;j<=m;++j)
			if(ma[i][j]=='.') cout<<'.';
			else cout<<cal(vis[i-1][j],vis[i+1][j],vis[i][j-1],vis[i][j+1]);
		cout<<endl;
	}

	return 0;
}

			  				 		 		            	  	


Comments

Submit
0 Comments
More Questions

1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test