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;
}

转载于:https://www.cnblogs.com/YDDDD/p/11361727.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值