Educational codeforces Round 5 C. The Labyrinth (DFS * *) __ Search

Source: Internet
Author: User
C. The labyrinth time limit per test 1 second memory limit/test 256 megabytes input standard input output standard out Put

You are given a rectangular field of nxm cells. Each of the cell is either empty or impassable (contains a obstacle). The Empty cells are marked with '. ' and impassable cells are with ' * '. Let's call two empty cellsadjacent if they share a side.

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

For each impassable cell (x, y) Imagine this is the it is a empty cell (all other cells remain unchanged) and find the size (the Number of cells) of the connected component which contains (x, y). You are should do it to each impassable cell independently.

The answer should is printed as a matrix with n rows andm columns. thej-th symbol of thei-th row should is '. ' If the cell is empty at the start. Otherwise thej-th symbol of the i-th row should contain the only digit--the answer modulo10. The matrix should is printed without any spaces.

To make your output faster it are recommended to builds the output as an array ofn strings have lengthm and print it as a sequence of lines. It is much faster than writing character-by-character.

As Input/output can reach huge size it's recommended to use fast input/output methods:for example, prefer to USESCANF/PR intf instead ofcin/cout in C + +, prefer to use Bufferedreader/printwriter instead the ofscanner/system.out. Input

The 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. 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 the ' example ', if we imagine that the ' the ' is empty then it would be included to component of Size5 (cross). If any of the corner cell would be empty then it would be included to component of Size3 (corner).



Give you a map and let you look around '. ' The number of connections also counted, the result of 10


Idea: For every '. ' Search, the way to mark which group, and then the point of a group to join the queue, and finally assigned to its value, the value of the group points, and finally check each ' * ' belong to the group's similarities and differences.


Summary: When judging the similarities and differences of the group, WA a few times, is also drunk.



AC Code:

#include <stdio.h> #include <math.h> #include <string.h> #include <stack> #include <queue
> #include <vector> #include <iostream> #include <algorithm> #define MAXN 1001000 #define LL Long
#define LL __int64 #define INF 0x7fffffff #define MEM (x) memset (x,0,sizeof (x)) #define PI ACOs ( -1) #define MOD 1000000007
using namespace Std;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
Char map[1010][1010];
int v[1010][1010];
int cnt[1010][1010];
int VV;
int n,m;
int num;
struct S {int a,b;};
queue<s>q; int check (int x,int y) {if (x<0| | x>=n| | y<0| | y>=m| | v[x][y]| |
	map[x][y]== ' * ') return 0;
return 1;
	} void Dfs (int x,int y) {num++;
	V[X][Y]=VV;
		for (int i=0;i<4;i++) {int nx=x+dir[i][0];
		int ny=y+dir[i][1];
			if (check (Nx,ny)) {s bb;
			Bb.a=nx;bb.b=ny;q.push (BB);
		DFS (NX,NY);
	int main () {int i,j; while (scanf ("%d%d", &n,&m)!=eof) {for (i=0;i<n;i++) scanf ('%s ', map[i]);//For (i=0;i<n;i++)//		printf ("%s\n", Map[i]);
		Vv=1;mem (CNT); mem (v); for (i=0;i<n;i++) {for (j=0;j<m;j++) {if map[i][j]== ' * ' | |
				V[I][J]) continue;
				num=0;
				DFS (I,J);
				printf ("%d\n", VV);
				Cnt[i][j]=num;
				vv++;
					while (!q.empty ()) {s aa=q.front ();
					Q.pop ();
				CNT[AA.A][AA.B]=CNT[I][J];
				}} for (i=0;i<n;i++) {for (j=0;j<m;j++) {if (map[i][j]== '. ')
				printf (".");
					else//is this WA a few times {int ccnt=1;
					int aa[5];
					int cc=0;
						for (int w=0;w<4;w++) {int nx=i+dir[w][0];
						int ny=j+dir[w][1];
						if (nx>=0&&nx<n&&ny>=0&&ny<m&&map[nx][ny]== '. ')
							{int bz=0;
									for (int e=0;e<cc;e++) {if (Aa[e]==v[nx][ny]) {bz=1;
								Break
								} if (bz==0) {Aa[cc++]=v[nx][ny];
							Ccnt+=cnt[nx][ny];
					}} ccnt=ccnt%10;
		printf ("%d", ccnt);		printf ("\ n");
} return 0; }



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.