• Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集


    D. Igor In the Museum

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/598/problem/D

    Description

    Igor is in the museum and he wants to see as many pictures as possible.

    Museum can be represented as a rectangular field of n × m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture.

    At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one.

    For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see.

    Input

    First line of the input contains three integers nm and k (3 ≤ n, m ≤ 1000, 1 ≤ k ≤ min(n·m, 100 000)) — the museum dimensions and the number of starting positions to process.

    Each of the next n lines contains m symbols '.', '*' — the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum.

    Each of the last k lines contains two integers x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) — the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns — from left to right. It is guaranteed that all starting positions are empty cells.

    Output

    Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.

    Sample Input

    5 6 3
    ******
    *..*.*
    ******
    *....*
    ******
    2 2
    2 5
    4 3

    Sample Output

    6
    4
    10

    HINT

    题意

    给你一个n*m的矩阵,.表示能走,*表示是墙,每一面墙上都挂了一幅画

    然后问你k次,分别从xi,yi走最多能看多少画

    题解:

    直接BFS暴力预处理就好了,然后对于每次询问,我是用并查集去维护的

    代码

    #include<iostream>
    #include<stdio.h>
    #include<queue>
    #include<cstring>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    
    int n,m,q;
    int fa[1005005];
    char s[1005][1005];
    int vis[1005][1005];
    int ans[1005005];
    int getid(int x,int y)
    {
        return x*m+y;
    }
    int fi(int x)
    {
        if(x!=fa[x])
            fa[x]=fi(fa[x]);
        return fa[x];
    }
    void uni(int x,int y)
    {
        int p = fi(x),q = fi(y);
        if(p==q)return;
        fa[y]=fa[x];
        ans[y]+=ans[x];
    }
    
    int dx[4]={1,-1,0,0};
    int dy[4]={0,0,1,-1};
    void bfs(int x,int y)
    {
        queue<pair<int,int> >Q;
        Q.push(make_pair(x,y));
        while(!Q.empty())
        {
            pair<int,int>now = Q.front();
            Q.pop();
            if(vis[now.first][now.second])continue;
            vis[now.first][now.second]=1;
            for(int i=0;i<4;i++)
            {
                pair<int,int> next = now;
                next.first +=dx[i];
                next.second += dy[i];
                if(next.first<0||next.first>n)continue;
                if(next.second<0||next.second>m)continue;
                if(vis[next.first][next.second])continue;
                if(s[next.first][next.second]=='*')
                {
                    ans[getid(x,y)]++;
                    continue;
                }
                uni(getid(x,y),getid(next.first,next.second));
                Q.push(next);
            }
        }
    }
    int main()
    {
        memset(vis,0,sizeof(vis));
        memset(s,0,sizeof(s));
        memset(fa,0,sizeof(fa));
        memset(ans,0,sizeof(ans));
        scanf("%d%d%d",&n,&m,&q);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                fa[getid(i,j)]=getid(i,j);
            }
        }
    
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(s[i][j]=='*')continue;
                if(vis[i][j])continue;
                bfs(i,j);
            }
        }
        for(int i=0;i<q;i++)
        {
            int x,y;scanf("%d%d",&x,&y);x--,y--;
            int k = fi(getid(x,y));
            printf("%d
    ",ans[fa[k]]);
        }
    }
  • 相关阅读:
    三地气温的典型相关分析
    android 使用 BroadcastReceiver 总结
    发布网站问题笔记
    Android 两个Activity 实现数据的来回传递
    input 标签屏蔽谷歌浏览器高亮显示
    javascript 获取当前html 标签的位置
    c# 四舍五入
    利用SQL的charindex实现字符串数组和Split函数
    ExtJs 一些常用的控件
    C#中return、break、continue的用法
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4966143.html
Copyright © 2020-2023  润新知