CSU-ACM2017 Summer Training 6-bfs i-the Labyrinth codeforces-616c__ Search

Source: Internet
Author: User
topic:

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 cells adjacent 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 and M columns. The j-th symbol of the i-th row should is "." 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 10. 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 length m 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 use scanf/p rintf instead of Cin/cout in C + +, prefer to use bufferedreader/printwriter instead of scanner/system.out in Java. 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. Example Input

3 3
*.*
. *.
*.*
Output
3.3
. 5.
3.3
Input
4 5
* *.. *
.. ***
.*.*.
*.*.*
Output
46..3
.. 732
. 6.4.
5.4.3
Note

In-in-example, if we imagine that the "the" is empty then it would be included to component of Size 5&nbs p; (cross). If any of the corner cell would be empty then it would be included to component of size 3  (corner).   
            topic: give you a picture with ' * ' and '. ' Ask you about the "." In each direction. And then the output is represented by the number of * (number to 10).             thinking: At first it's natural to think of BFS for each *, of course it's timed out, and the right thing to do is to use BFS or Dfs to '. ' The connected blocks are preprocessed, that is to the different unicom fast numbering and counting. And then directly to the ' * ' up and down to see which unicom fast can, here attention to the top and bottom may also appear the same unicom fast, so remember to judge heavy.           code:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include <
cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue>
using namespace Std;
typedef long Long LL;
Const double PI = ACOs (-1.0);
Const double EPS = 1e-6;
const int INF = 1000000000;
const int MAXN = 100;
int t,n,m,k=1;
Char maps[1111][1111];
Long Long book[1111][1111];
Long Long sums[1111][1111];
Long Long num[11111111];

int nexts[4][2]={0,1,-1,0,1,0,0,-1};
    void Dfs (int a,int b) {book[a][b]=k;
    num[k]++;
        for (int i=0;i<4;i++) {int aa=a+nexts[i][0];
        int bb=b+nexts[i][1]; if (AA&GT;=0&AMP;&AMP;BB&GT;=0&AMP;&AMP;AA&LT;N&AMP;&AMP;BB&LT;M&AMP;&AMP;BOOK[AA][BB]==0&AMP;&AMP;MAPS[AA][BB]
            =='.')
    DFS (AA,BB);
} return; int main () {scanf ("%d%d", &amP;N,&AMP;M);
    memset (book,0,sizeof (book));
    memset (num,0,sizeof (num));
    for (int i=0;i<n;i++) scanf ("%s", Maps[i]); for (int i=0;i<n;i++) for (int j=0;j<m;j++) if (maps[i][j]== '. ')
                &&book[i][j]==0) {DFS (I,J);
            k++;
            for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {int sum=0;
                if (maps[i][j]== ' * ') {set<int>t;
                    for (int p=0;p<4;p++) {int a=i+nexts[p][0];
                    int b=j+nexts[p][1]; if (maps[a][b]== '. ')
                        &&a>=0&&b>=0&&a<n&&b<m&&!t.count (Book[a][b])) {
                        T.insert (Book[a][b]);
                    sum+= (NUM[BOOK[A][B]]%10);
            }} sums[i][j]=sum;
}
        }
    }

    for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (maps[i][j]== '. ')
            printf (".");
            else {printf ("%d", (sums[i][j]+1)%10);
    printf ("\ n");
return 0; }


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.