CodeForces - 616C The Labyrinth 【搜索+预处理】

题目链接: http://codeforces.com/problemset/problem/616/C

Description(下方有汉语题意)

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 nstrings 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.

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

题意 

小O无意间发现了一张藏宝图,它跟随藏宝图的指引来到了一个宫殿,宫殿的地板被分成了n*m块格子,每个格子上放置了金子或者石头

藏宝图告诉小O,它可以选择一块石头变成金子,并且带走与变化后的金子联通区域的所有金子(联通指的是上下左右,不能斜着)

小O想计算一下点每个石头能带走的金子个数,帮帮他吧。

输入:
第一行两个数n,m (1 <= n,m <= 1000 )
随后n行,每行m个字符,表示宫殿地板上放置的物品,' * '表示放置的是石头,' . '表示放置的是金子

输出:
在每个石头位置输出如果将该位置点石成金,能带走的金子个数,方便起见,将这个数%10再输出

解题思路

容易想到每次遇到一块石头就进行一次搜索,正确性上没问题,但是会T。

这题难在预处理+判重。

解释看代码里的注释,

AC代码

#include <bits/stdc++.h>
using namespace std;
char mp[1001][1001];//记录位置(i,j)是石头还是金子 
int num[1001][1001];//如果是位置(i,j)上是金子,则记录从该点出发能得到的最多金子。否则为0 
int tye[1001][1001];//用于判重 
int n,m;
bool book[1001][1001];//标记位置(i,j)是否走过 
struct Node {
	int x,y;
	Node(int xx,int yy) {
		x = xx;
		y = yy;
	}
};
vector <Node > v;
int Next[4][2]={0,1,1,0,0,-1,-1,0}; 
void dfs(int x,int y)
{
	int tx,ty;
	for(int i=0;i<4;i++) {
		tx = x+Next[i][0];
		ty = y+Next[i][1];
		if(tx<1 || tx>n || ty<1 || ty>m)
			continue;
		if(!book[tx][ty] && !num[tx][ty] && mp[tx][ty] == '.') {
			v.push_back(Node(tx,ty));
			book[tx][ty] = true;
			dfs(tx,ty);
		}
	}
}
int main()
{
	cin >> n >> m;
	for(int i=1;i<=n;i++)
		cin >> mp[i]+1;
	int tot=1;
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			if(mp[i][j] == '.' && num[i][j] == 0) {	//位置(i,j)是金子切不知道从位置(i,j)开始能拿多少金子,去计算 
				v.clear();
				v.push_back(Node(i,j));
				book[i][j] = true;
				dfs(i,j);
				//从任意拿到这些金子的位置出发,拿到的金子数量都是一定的 
				for(int i=0;i<v.size();i++) {
					num[v[i].x][v[i].y] = v.size();
					tye[v[i].x][v[i].y] = tot;	//用于下边的判重。 
				} 
				tot++;
			}
		}
	}
	int c,tx,ty;
	set <int > s;
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			if(mp[i][j] == '*') {
				s.clear();
				for(int k=0;k<4;k++) {
					tx = i+Next[k][0];
					ty = j+Next[k][1];
					// 比如说,某个位置上方的金子和右方的金子是连通的,那么只需从一处出发搜索就可以了。
					// 判重就在这,自己好好想想,很有意思。 
					if(mp[tx][ty] == '.' && s.find(tye[tx][ty]) == s.end()) {
						num[i][j] += num[tx][ty];
						s.insert(tye[tx][ty]);
					}
				}
			} 
		}
	}
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			if(mp[i][j] == '*')
				cout << (num[i][j]+1)%10;
			else
				cout << '.';
		}
		cout << endl;
	}
}

 

CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[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^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[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^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值