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;
}
CodeForces-598D(BFS,染色)的更多相关文章
- HDU 2444 二分图判断 (BFS染色)+【匈牙利】
<题目链接> 题目大意: 有N个人,M组互相认识关系互相认识的两人分别为a,b,将所有人划分为两组,使同一组内任何两人互不认识,之后将两个组中互相认识的人安排在一个房间,如果出现单人的情况 ...
- 【Luogu】P1330封锁阳光大学(bfs染色)
题目链接 这题恶心死我了. bfs染色,统计每个联通块两色的个数,ans加它们的最小值. #include<cstdio> #include<cctype> #include& ...
- XMU 1617 刘备闯三国之汉中之战 【BFS+染色】
1617: 刘备闯三国之汉中之战 Time Limit: 1000 MS Memory Limit: 128 MBSubmit: 6 Solved: 5[Submit][Status][Web B ...
- UVALive 3977 BFS染色
这个题意搞了半天才搞明白 就是如果定义一个d-summit,即从该点到另一个更高的点,经过的路径必定是比当前点低至少d高度的,如果该点是最高点,没有比他更高的,就直接视为顶点 其实就是个BFS染色,先 ...
- Codeforces Round #360 (Div. 2)——C. NP-Hard Problem(BFS染色判二分图)
C. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【CodeForces - 598D】Igor In the Museum(bfs)
Igor In the Museum Descriptions 给你一个n*m的方格图表示一个博物馆的分布图.每个方格上用'*'表示墙,用'.'表示空位.每一个空格和相邻的墙之间都有一幅画.(相邻指的 ...
- BFS(染色) LA 3977 Summits
题目传送门 题意:题意坑爹.问符合条件的的山顶个数 分析:降序排序后从每个点出发,假设为山顶,如果四周的点的高度>h - d那么可以走,如果走到已经走过的点且染色信息(山顶高度)不匹配那么就不是 ...
- Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)
题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...
- UVALive - 3977 Summits (BFS染色)
题目大意:坑爹的题目.题意那么难理解. 讲的就是,假设该点是山顶的话(高度为h).那么以该点为中心,往外辐射.走高度大于h-d的点,到达不了还有一个比它高的点 这就提示了,高度要从大到小排序,依次以高 ...
- CF796D Police Stations BFS+染色
题意:给定一棵树,树上有一些点是警察局,要求所有点到最近的警察局的距离不大于 $d$,求最多能删几条边 ? 题解: 考虑什么时候一条边可以被断开:这条边的两个端点被两个不同的警察局覆盖掉. 我们要设计 ...
随机推荐
- ATP检测 BAPI BAPI_MATERIAL_AVAILABILITY
*****ATP检测 DATA: END_RLT LIKE EBAN-LFDAT, NOT_AVAILABLE, AVAILABLE( ...
- nova计算服务
1. nova介绍 Nova 是 OpenStack 最核心的服务,负责维护和管理云环境的计算资源.OpenStack 作为 IaaS 的云操作系统,虚拟机生命周期管理也就是通过 Nova 来实现的. ...
- 磁盘的分区和挂载(mount)
一.挂载问题的引入 我们大多数人用惯了windos系统,对linux系统中磁盘的管理就先入为主,不太好理解挂载这一动作.在linux系统中添加一块新磁盘后,要进行分区.格式化(分配文件系统).挂载.当 ...
- 【嵌入式开发】用 VLC 显示 树莓派摄像头 H264 裸流
首先树莓派连上网络,并和电脑在同一网段. 树莓派的IP是: 192.168.3.13 电脑的IP是: 192.168.3.6 1.在树莓派上采集 H264裸流,并用UDP发送到电脑. pi@Neil- ...
- python 并发编程 多线程 互斥锁
互斥锁 并行变成串行,牺牲效率 保证数据安全,实现局部串行 保护不同的数据,应该加不同的锁 现在一个进程 可以有多个线程 所有线程都共享进程的地址空间 实现数据共享 共享带来问题就会出现竞争 竞争就会 ...
- bi的tableau
参考: 官网: https://help.tableau.com/current/server-linux/zh-cn/get_started_server.htm 可视化分析最佳做法: 实用指南 h ...
- Spring(三)--Spring bean的生命周期
Spring bean的生命周期 ApplicationContext Bean生命周期流程 1.需要的实体类 ackage com.xdf.bean; import org.springframew ...
- P1115 最大子段和(简单DP)
题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入格式 第一行是一个正整数NN,表示了序列的长度. 第二行包含NN个绝对值不大于1000010000的整数A_iAi,描述了这段序 ...
- git reset –mixed –soft –hard命令解释。
直接看官方的解释. 其中HEAD代表版本库,index代表暂存区,另外还有一个我们增删改代码的工作区.所以官方解释翻译过来就是: --hard : 回退版本库,暂存区,工作区.(因此我们修改过的代码就 ...
- 从入门到自闭之python初识
Day 01 整型: 对比: 在python 2 版本中有整型,长整型long 在python 3 版本中全部都是整型 用于计算和比较 整型和布尔值的转换 二进制转换成十进制: print (int ...