codeforces616C The Labyrinth

原题链接:http://codeforces.com/contest/616/problem/C


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/printfinstead 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.

Examples
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).



说题之前先啰嗦几句:

说来从4月中旬到5月初一直在学java和前端,很长时间没有刷题了,感觉又落下了不少,学习新知识的过程中我看过很多有用的和没用的资料,最直观的感觉是,很多途径教你怎么速成一门手艺,但是却没有教你怎么深入进去,过程中我也认识了几个一起学习的小伙伴,觉得这些只注重速度的教学不是无中生有的,因为确实存在这麽一些人,他们恨不能用最短的时间吧所有都学完,甚至我有时也会不由自主的加快学习速度,从而使得我得到的新知识不能深入理解;

要说大学期间让我坚持这麽久还没有放弃的事情,应该就是刷题了,这周开始刷题,感觉脑细胞又重新运转起来了,走神的几率大大减少,不得不承认,算法的学习让我改变了大脑懒惰的坏习惯,想想总算有件事情能让我在大学期间学习的热情能有那么点儿提高了。。。




题解:

给你一个矩阵,有'*'和'.'两种字符,对于每一个字符'*',计算其上下左右四个方向所有相连的'.'的个数。


肯定不能直接对于每个'*'搜索,所以可以从'.'下手;于是我先dfs搜索记录每个'.'的联通块,并且要用一个数组记录连通块个数,这里标记时我们对于每个联通块用不同的数字标记,然后就可以对于每一个'*'上下左右搜索。这里要注意,搜索时,不同方向的'.'的个数可能是同一个联通块里面的,所以,要判重,一开始没有考虑到这点,就找了半小时错误。。



代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <map>

using namespace std;

int n, m;
char e[1005][1005];
int dir[4][2] = { 1, 0, 0, 1, -1, 0, 0, -1 };
int vis[1005][1005];
int cnt, num;
set<int> s;
map<int, int> mp;

void dfs(int x, int y)
{
	int i, j;
	for (i = 0; i < 4; i++)
	{
		int tx = x + dir[i][0];
		int ty = y + dir[i][1];
		if (tx <= 0 || tx > n || ty <= 0 || ty > m || vis[tx][ty] || e[tx][ty] == '*')
			continue;
		cnt++;
		vis[tx][ty] = num;
		dfs(tx, ty);
	}
}

int main()
{
	int i, j;
	scanf("%d%d", &n, &m);
	for (i = 1; i <= n; i++)
		scanf("%s", e[i] + 1);
	for (i = 1; i <= n; i++)
		for (j = 1; j <= m; j++)
		{
			if (!vis[i][j] && e[i][j] == '.')
			{
				num++;
				cnt = 1;
				vis[i][j] = num;
				dfs(i, j);
				mp[num] = cnt;
			}
		}
	for (i = 1; i <= n; i++){
		for (j = 1; j <= m; j++)
		{
			if (e[i][j] == '*'){
				int cc = 0;
				s.clear();
				for (int k = 0; k < 4; k++)
				{
					int tx = i + dir[k][0];
					int ty = j + dir[k][1];
					if (e[tx][ty] == '.'&&!s.count(vis[tx][ty]))
					{
						s.insert(vis[tx][ty]);
						cc += mp[vis[tx][ty]];
					}
				}
				printf("%d", (cc + 1) % 10);
			}
			else
				printf("%c", e[i][j]);
		}
		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值