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;
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章