CodeForces 616C:The Labyrinth(BFS)

The Labyrinth

Time limit:1000 ms Memory limit:32768 kB OS:Windows


Problem 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 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

这里写图片描述

这里写图片描述

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


题意:

求每个*能够到达的格子数量,只有.可以走,结果mod 10,替换 * 后输出

解题思路:

一开始想的是直接对每个*来BFS,得到下面的代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

const int maxn=1000+5;
int vis[maxn][maxn];
char mp[maxn][maxn];
int n,m;
int dx[]= {0,1,0,-1};
int dy[]= {1,0,-1,0};

struct Node
{
    int x,y;
};
queue<Node> Q;

bool check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m)
        return false;
    if(vis[x][y])
        return false;
    if(mp[x][y]!='.')
        return false;
    return true;
}

void BFS(int x1,int y1)
{
    vis[x1][y1]=1;
    int ans=0;
    Q.push((Node)
    {
        x1,y1
    });
    while(!Q.empty())
    {
        ans++;
        Node &u=Q.front();
        Q.pop();
        for(int i=0; i<4; i++)
        {
            int x=u.x+dx[i];
            int y=u.y+dy[i];
            if(check(x,y))
            {
                vis[x][y]=1;
                Q.push(Node{x,y});
            }
        }
    }
    mp[x1][y1]=(char)('0'+ans%10);
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
        scanf("%s",mp[i]);

    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            if(mp[i][j]=='*')
            {
                mem(vis,0);
                BFS(i,j);
            }

    for(int i=0;i<n;i++)
    {
        printf("%s\n",mp[i]);
    }

    return 0;
}

结果果然超时。。正确做法是对.来遍历,这样每个连通块就只用搜一次了,每个连通块用vis来记录,再用map来记录连通块的大小,然后用set把上下左右的连通块的vis都加进去,避免重复,最后用set中的vis值对应map求出连通块大小之和就可以了。


Code:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

const int maxn=1000+5;
int vis[maxn][maxn];
char mp[maxn][maxn];
int n,m;
int dx[]= {0,1,0,-1};
int dy[]= {1,0,-1,0};

struct Node
{
    int x,y;
};
queue<Node> Q;
map<int,int> M;
set<int> S;
set<int>::iterator it;

bool check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m)
        return false;
    if(vis[x][y])
        return false;
    if(mp[x][y]!='.')
        return false;
    return true;
}

void BFS(int x1,int y1,int cnt)
{
    vis[x1][y1]=cnt;
    int ans=0;
    Q.push((Node)
    {
        x1,y1
    });
    while(!Q.empty())
    {
        ans++;
        Node &u=Q.front();
        Q.pop();
        for(int i=0; i<4; i++)
        {
            int x=u.x+dx[i];
            int y=u.y+dy[i];
            if(check(x,y))
            {
                vis[x][y]=cnt;
                Q.push(Node{x,y});
            }
        }
    }
    M[cnt]=ans;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
        scanf("%s",mp[i]);

    mem(vis,0);
    int cnt=1;
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            if(mp[i][j]=='.'&&vis[i][j]==0)
            {
                BFS(i,j,cnt++);
            }

    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
        {
            if(mp[i][j]=='*')
            {
                S.clear();

                if(i>0)
                    S.insert(vis[i-1][j]);
                if(i<n-1)
                    S.insert(vis[i+1][j]);
                if(j>0)
                    S.insert(vis[i][j-1]);
                if(j<m-1)
                    S.insert(vis[i][j+1]);
                int res=1;
                for(it=S.begin();it!=S.end();it++)
                    res+=M[*it];
                mp[i][j]=char('0'+res%10);
            }
        }
    for(int i=0; i<n; i++)
    {
        printf("%s\n",mp[i]);
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值