Educational Codeforces Round 5-C. The Labyrinth(简单dfs)

C. The Labyrinth
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a rectangular field of n × m cells. Each cell is either empty or impassable (contains an obstacle). Empty cells are marked with '.', impassable cells are marked with '*'. Let's call two empty cells adjacent if they share a side.

Let's call a connected component any non-extendible set of cells such that any two of them are connected by the path of adjacent cells. It is a typical well-known definition of a connected component.

For each impassable cell (x, y) imagine that it is an empty cell (all other cells remain unchanged) and find the size (the number of cells) of the connected component which contains (x, y). You should do it for each impassable cell independently.

The answer should be printed as a matrix with n rows and m columns. The j-th symbol of the i-th row should be "." if the cell is empty at the start. Otherwise the j-th symbol of the i-th row should contain the only digit —- the answer modulo 10. The matrix should be printed without any spaces.

To make your output faster it is recommended to build the output as an array of n strings having length m and print it as a sequence of lines. It will be much faster than writing character-by-character.

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 1000) — the number of rows and columns in the field.

Each of the next n lines contains m symbols: "." for empty cells, "*" for impassable cells.

Output

Print the answer as a matrix as described above. See the examples to precise the format of the output.

Sample test(s)
Input
3 3
*.*
.*.
*.*
Output
3.3
.5.
3.3
Input
4 5
**..*
..***
.*.*.
*.*.*
Output
46..3
..732
.6.4.
5.4.3
Note

In first example, if we imagine that the central cell is empty then it will be included to component of size 5 (cross). If any of the corner cell will be empty then it will be included to component of size 3 (corner).


这道题一开始就用dfs写了一半了,之后就估计了一下时间复杂度,发现dfs会超时就没写下去了,之后就断网了就看电影了,结果早上看了看

官方题解:

Let's enumerate all the connected components, store their sizes and for each empty cell store the number of it's component. It can be done with a single dfs. Now the answer for some impassable cell is equal to one plus the sizes of all different adjacent connected components. Adjacent means the components of cells adjacent to the current impassable cell (in general case each unpassable cell has four adjacent cells).

就是用dfs啊,结果发现傻逼了,时间复杂度估计错误大哭


总结:已经在时间复杂度估计上出错好几次了,以后对于不确定的时间复杂度还是认认真真的写下去吧。。。


耗时: 280ms多吧。


AC代码:


#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#include<set>
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
typedef  __int64 ll;
#define T 1010
#define mod 1000000007

int c[T][T],n,m;
int vis[T][T];
char s[T][T];

int fx[][2]={{1,0},{0,1},{-1,0},{0,-1}};

bool jugde(int x,int y)
{
	if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&s[x][y]!='*')
		return true;
	return false;
}

int cnt,cc;

vector< pair<int,int> > ve;

void dfs(int x,int y)
{
	for(int i=0;i<4;++i){
		int tx = x + fx[i][0];
		int ty = y + fx[i][1];
		if(jugde(tx,ty)){
			ve.push_back(make_pair(tx,ty));
			cnt++;
			vis[tx][ty]=cc;
			dfs(tx,ty);
		}
	}
}

int main()
{
#ifdef zsc
    freopen("input.txt","r",stdin);
#endif

	int i,j,k,ma;
	while(~scanf("%d%d",&n,&m))
	{
		cc = 0;
		for(i=0;i<n;++i){
			for(j=0;j<m;++j){
				c[i][j] = 1;
				vis[i][j]=0;
				scanf("\n%c",&s[i][j]);
			}
		}
		for(i=0;i<n;++i){
			for(j=0;j<m;++j){
				if(!vis[i][j]&&s[i][j]=='.'){
					cc++;
					vis[i][j] = cc;
					cnt = 1;
					dfs(i,j);
					for(int f=0;f<ve.size();++f)
					{
						c[ve[f].first][ve[f].second] = cnt;
					}
					ve.clear();
					c[i][j] = cnt;
				}
			}
		}

		for(i=0;i<n;++i){
			for(j=0;j<m;++j){
				if(s[i][j]!='.'){
					k = 0;
					int Down=i+1,Up=i-1,Left=j-1,Right=j+1;
					int f1=0,f2=0,f3=0;
					//上
					if(Up>=0&&s[Up][j]=='.')k+=c[Up][j],f1=1;
					//下
					if(
						(
							Down<n&&s[Down][j]=='.'&&
							(!f1||vis[Down][j]!=vis[Up][j])
						)
					  )k+=c[Down][j],f2=1;
					//左
					if(
						(
							Left>=0&&s[i][Left]=='.'&&
							(!f1||vis[Up][j]!=vis[i][Left])&&
							(!f2||vis[Down][j]!=vis[i][Left])
						)
					  )k+=c[i][Left],f3=1;
					//右
					if(
						(
							Right<m&&s[i][Right]=='.'&&
							(!f1||vis[Up][j]!=vis[i][Right])&&
							(!f2||vis[Down][j]!=vis[i][Right])&&
							(!f3||vis[i][Left]!=vis[i][Right])
						)
					
					   )k+=c[i][Right];
					
					printf("%d",(k+1)%10);
				}
				else {
					printf(".");
				}
			}
			printf("\n");
		}
	}

    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值