zoukankan      html  css  js  c++  java
  • CodeForces-598D(BFS,染色)

    链接:

    https://vjudge.net/problem/CodeForces-598D

    题意:

    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.

    思路:

    Bfs加染色,先求出每个空格单个位置能看到画,Bfs的时候数一个联通快能看到的画,同时给一个联通快染色,优化重复查询的问题

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    
    const int MAXN = 1e3+10;
    const int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
    char Map[MAXN][MAXN];
    int Value[MAXN][MAXN];
    int Sum[MAXN*MAXN];
    int Vis[MAXN][MAXN];
    int n, m, q;
    
    void Bfs(int x, int y, int p)
    {
        int res = 0;
        queue<pair<int, int> > que;
        que.push(make_pair(x, y));
        Vis[x][y] = p;
        res += Value[x][y];
        while (!que.empty())
        {
            x = que.front().first;
            y = que.front().second;
            que.pop();
            for (int i = 0;i < 4;i++)
            {
                int tx = x+Next[i][0];
                int ty = y+Next[i][1];
                if (tx < 1 || tx > n || ty < 1 || ty > m)
                    continue;
                if (Map[tx][ty] == '*' || Vis[tx][ty] != 0)
                    continue;
                res += Value[tx][ty];
                Vis[tx][ty] = p;
                que.push(make_pair(tx, ty));
            }
        }
        Sum[p] = res;
    //    cout << Sum[p] << endl;
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n >> m >> q;
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= m;j++)
                cin >> Map[i][j];
        }
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= m;j++)
            {
                if (Map[i][j] == '*')
                    continue;
                for (int k = 0;k < 4;k++)
                {
                    int x = i+Next[k][0];
                    int y = j+Next[k][1];
                    if (x < 1 || x > n || y < 1 || y > m)
                        continue;
                    if (Map[x][y] == '.')
                        continue;
                    Value[i][j]++;
                }
            }
        }
        for (int i = 1;i <= q;i++)
        {
            int x, y;
            cin >> x >> y;
            if (Vis[x][y] != 0)
                cout << Sum[Vis[x][y]] << endl;
            else
            {
                Bfs(x, y, i);
                cout << Sum[Vis[x][y]] << endl;
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    python接口测试3-JSON格式
    python接口测试2-开发WEB接口
    接口测试1-基础
    Apifox接口测试管理工具
    python的pip安装超时问题解决
    ubuntu解决安装速度问题
    vim进入粘贴模式
    禅道数据库
    内存管理
    文件操作
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11361727.html
Copyright © 2011-2022 走看看