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$,求最多能删几条边 ? 题解: 考虑什么时候一条边可以被断开:这条边的两个端点被两个不同的警察局覆盖掉. 我们要设计 ...
随机推荐
- 2019-03-19 Fiori学习笔记 Fiori开发环境
Fiori 文档 :https://experience.sap.com/fiori-design-web/ Fiori 登录:https://cloudplatform.sap.com/index. ...
- Redis 入门 3.2.4 命令拾遗
Redis 入门 3.2 字符串类型 3.2.4 命令拾遗 1. 增加指定的整数 INCRBY key increment INCRBY命令与INCR命令基本一样,只不过前者可以通过increme ...
- 跨域常见解决方案jsonp,cors示例
方案 JSONP jsonp需要后端进行配置,并且前端需要动态生成script标签通过callback调用函数进行操作的跨域解决方案 不建议使用该方案: 前后端均需进行配置处理,增加了工作量 ...
- qs.js使用方法
https://github.com/ljharb/qs 占个空
- mysql如何下载历史版本?
进入官网 www.mysql.com
- 解决 Illegal DefaultValue null for parameter type integer 异常
该异常是由 swagger 引起的 swagger 版本 1.9.2 解决原因:重新导入 swagger-annotations 和 swagger-models 版本 为 1.5.21 pom.xm ...
- 云服务器以及linux操作系统打开防火墙,在墙上开一个小口
在服务器运行时,需要在某个端口上开一个小口,以供外部访问 执行命令/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT 8080为端口号,需要开的 ...
- MLS(移动最小二乘)
https://blog.csdn.net/weixin_41484240/article/details/81204113 https://blog.csdn.net/baidu_38127162/ ...
- 手把手教你用Python搭建自己的量化回测框架【均值回归策略】
手把手教你用Python搭建自己的量化回测框架[均值回归策略] 引言 大部分量化策略都可以归类为均值回归与动量策略.事实上,只有当股票价格是均值回归或趋势的,交易策略才能盈利.否则,价格是随机游走的, ...
- Codeforces 10D LCIS 求最长公共上升子序列及输出这个子序列 dp
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/34430283 题目链接:点击打开链接 题意 ...