The Labyrinth 联通分量+搜索

来源:互联网 发布:js 文本框数值相加 编辑:程序博客网 时间:2024/05/22 09:46

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

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

思路:
题意求每个‘’上下左右相连的’.’的个数包括‘’本身。
不可以对每个‘*’直接进行bfs或者dfs 会tle
预处理出每个联通的‘.’的区域,编号,并且记录个数。再扫一遍判断上下左右的‘.’的个数即可

#include <bits/stdc++.h>using namespace std;char mp[1010][1010];int cnt;int vis[1010][1010];int mm[1010][1010];int k[1100];    int n,m;int nex[4][2]={1,0,0,1,-1,0,0,-1};int dfs(int x,int y,int id){    vis[x][y]=id;   int cnt=1;    for(int i=0;i<4;i++)    {        int xx=x+nex[i][0];        int yy=y+nex[i][1];        if(xx<1||xx>n||yy<1||yy>m||vis[xx][yy]||mp[xx][yy]=='*')            continue;        cnt+=dfs(xx,yy,id);    }    return cnt;}int get(int x,int y){    int res=0;    map<int,int> pp;    for(int i=0;i<4;i++)    {        int xx=x+nex[i][0];        int yy=y+nex[i][1];        if(xx<1||xx>n||yy<1||yy>m||pp[vis[xx][yy]]==1||mp[xx][yy]=='*')            continue;        pp[vis[xx][yy]]=1;        res+=k[vis[xx][yy]];    }    return res;}int main(){    cin>>n>>m;    int tot=1;    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        scanf(" %c",&mp[i][j]);    }    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        {            if(mp[i][j]=='.'&&!vis[i][j])            {                k[tot]=dfs(i,j,tot);                tot++;            }        }    }    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        {            if(mp[i][j]=='*')            {                int res=get(i,j);                res++;                res%=10;                printf("%d",res );            }            else printf("%c",mp[i][j] );        }        printf("\n");    }}
原创粉丝点击