zoukankan      html  css  js  c++  java
  • CodeForces

    传送门:

    http://codeforces.com/problemset/problem/616/C

    C. The Labyrinth
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

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


    题目意思:
    求每个*能够到达的格子数量,只有.可以走(四个方向扩展),结果mod 10,替换 * 后输出

    分析:
    开始是想对每个*号进行bfs,但是思考了一下,这种做法态蠢了,也耗时
    冥思苦想无果,看了一下xp大佬的博客
    得到了思路:
    对‘.’号进行遍历扩展,得到每个连通块的标号和大小,标号用vis数组储存
    map储存标号对应的连通块的大小,然后对每个*号,把它上下左右的连通块的标号放入set中(去重重复的连通块标号,这里注意理解)
    然后在map中找到对应标号的连通块大小,加起来之后加1(*自身)就是该*号周围的.号的个数


    code:
    #include<stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <math.h>
    #include <cstdlib>
    #include <queue>
    #include<map>
    #include<set>
    using namespace std;
    #define max_v 1005
    char G[max_v][max_v];
    int vis[max_v][max_v];//记录连通块的标号
    int n,m;
    
    struct node
    {
        int x,y;
    };
    
    queue<node> q;
    map<int,int> mm;//记录对应标号连通块的大小
    set<int> ss;//去除*号周围重复的标号的连通块
    set<int>::iterator it;
    int dir[4][2]= {1,0,0,1,-1,0,0,-1};
    
    int check(int x,int y)
    {
        if(x<0||x>=n||y<0||y>=m)
            return 0;
        if(vis[x][y])
            return 0;
        if(G[x][y]!='.')
            return 0;
        return 1;
    }
    void bfs(int x,int y,int cnt)
    {
        node p,next;
        vis[x][y]=cnt;
        int ans=0;
    
        p.x=x;
        p.y=y;
        q.push(p);
    
        while(!q.empty())
        {
            ans++;
            p=q.front();
            q.pop();
    
            for(int i=0; i<4; i++)
            {
                next.x=p.x+dir[i][0];
                next.y=p.y+dir[i][1];
    
                if(check(next.x,next.y))
                {
                    vis[next.x][next.y]=cnt;
                    q.push(next);
                }
            }
        }
        mm[cnt]=ans;
        // printf("%d-->%d
    ",cnt,ans);
    }
    int main()
    {
        scanf("%d %d",&n,&m);
        for(int i=0; i<n; i++)
            scanf("%s",G[i]);
    
        memset(vis,0,sizeof(vis));
    
        int cnt=1;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(G[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(G[i][j]=='*')
                {
                    ss.clear();
    
                    //将*周围连通块的标号放入set去重
                    if(i>0)
                        ss.insert(vis[i-1][j]);
                    if(i<n-1)
                        ss.insert(vis[i+1][j]);
                    if(j>0)
                        ss.insert(vis[i][j-1]);
                    if(j<m-1)
                        ss.insert(vis[i][j+1]);
    
    
                    int res=1;
                    for(it=ss.begin(); it!=ss.end(); it++)
                    {
                        res+=mm[*it];//通过map找到对应标号连通块的大小
                        // printf("set中值:%d map中的值:%d
    ",*it,mm[*it]);
                    }
    
                    //   printf("res=%d
    ",res);
                    G[i][j]=char('0'+res%10);
                }
            }
        }
        for(int i=0; i<n; i++)
        {
            printf("%s
    ",G[i]);
        }
    }














  • 相关阅读:
    idea找不到或无法加载主类
    Scala核心编程_第09章 面向对象编程(高级特性)
    spring源码:学习线索
    Redis
    spring源码:Aware接口
    spring源码:核心组件(li)
    java socket编程
    spring源码:ApplicationContext的增强功能(li)
    spring源码:web容器启动
    spring源码:BeanPostProcessor(li)
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9392044.html
Copyright © 2011-2022 走看看