Codeforces 598D:Igor In the Museum
1 second
256 megabytes
standard input
standard output
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.
First line of the input contains three integers n, m 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.
Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.
5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3
6
4
10
4 4 1
****
*..*
*.**
****
3 2
8
题意就是.和*相交的那部分有画,问每一个.连通块能看到多少幅画。
简单深搜,这题的一个亮点在于归类,将一个连通块的点用一个id标记一下,mark一下。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
using namespace std; int n, m, k, id;
int classify[1000005];
int result[1005][1005];
int vis[1005][1005];
char val[1005][1005]; int dfs(int x, int y)
{
vis[x][y] = id;//标记一个连通块内的点
if (val[x + 1][y] == '*')
{
result[x][y]++;
}
if (val[x - 1][y] == '*')
{
result[x][y]++;
}
if (val[x][y + 1] == '*')
{
result[x][y]++;
}
if (val[x][y - 1] == '*')
{
result[x][y]++;
}
if (vis[x - 1][y] == 0 && val[x - 1][y] == '.')
{
result[x][y] = dfs(x - 1, y) + result[x][y];
}
if (vis[x + 1][y] == 0 && val[x + 1][y] == '.')
{
result[x][y] = dfs(x + 1, y) + result[x][y];
}
if (vis[x][y - 1] == 0 && val[x][y - 1] == '.')
{
result[x][y] = dfs(x, y - 1) + result[x][y];
}
if (vis[x][y + 1] == 0 && val[x][y + 1] == '.')
{
result[x][y] = dfs(x, y + 1) + result[x][y];
}
return result[x][y];
} int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int i, j;
int temp1, temp2;
scanf("%d%d%d", &n, &m, &k); for (i = 1; i <= n; i++)
cin >> val[i] + 1;
id = 1;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
if (val[i][j] == '.'&&vis[i][j] == 0)
{
classify[id]= dfs(i, j);
id++;
}
}
}
for (i = 1; i <= k; i++)
{
scanf("%d%d", &temp1, &temp2);
printf("%d\n", classify[vis[temp1][temp2]]);
}
//system("pause");
return 0;
}
Codeforces 598D:Igor In the Museum的更多相关文章
- 【CodeForces - 598D】Igor In the Museum(bfs)
Igor In the Museum Descriptions 给你一个n*m的方格图表示一个博物馆的分布图.每个方格上用'*'表示墙,用'.'表示空位.每一个空格和相邻的墙之间都有一幅画.(相邻指的 ...
- 【Codeforces 598D】Igor In the Museum
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 同一个联通块里面答案都一样. 把每个联通块的答案都算出来 然后赋值就好 [代码] #include <bits/stdc++.h> ...
- 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 ...
- [BFS]Codeforces Igor In the Museum
Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Igor In the Museum(搜搜搜151515151515******************************************************1515151515151515151515)
D. Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standar ...
- codeforces 598D Igor In the Museum
题目链接:http://codeforces.com/problemset/problem/598/D 题目分类:dfs 题目分析:处理的时候一次处理一片而不是一个,不然会超时 代码: #includ ...
- Codeforces 598D (ccpc-wannafly camp day1) Igor In the Museum
http://codeforces.com/problemset/problem/598/D 分析:BFS,同一连通区域的周长一样,但查询过多会导致TLE,所以要将连通区域的答案储存,下次查询到该连通 ...
- Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)
题目链接:http://codeforces.com/problemset/problem/598/D 题意是 给你一张行为n宽为m的图 k个询问点 ,求每个寻问点所在的封闭的一个上下左右连接的块所能 ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
随机推荐
- pandas read excel or csv
import pandas as pd """pandas doc:df.dtypes 查看数据每column 数据类型 id int64x0 float64df.rei ...
- 06-Docker-Image管理操作
目录 06-Docker-Image管理操作 参考 镜像命名规范 镜像管理命令 1. 拉取推送 2. 查看操作 3. 本地删除 4. 创建标签 5. 导出导入 06-Docker-Image管理操作 ...
- rsync安装与配置使用 数据同步方案(centos6.5)
rsync + crond ==定时数据同步 sersync(inotify) + rsync ==实时数据同步,利用rsync实现 ##应用场景 ..1 主备服务器之间同步数据定时 = ...
- WCF技术归纳
本人在2013年就做过一个WCF的项目,但最近又开始看相关的文章,才发现当年的认识实在太浅显,这里我把WCF的几个重要知识点总结以下. ABC概念 WCF服务的构成如下图 如你所见,Host即为宿主, ...
- jmh 微基准测试
选择依据:对某段代码的性能测试. 1.运行方法 mvn clean install java -jar target/benchmarks.jar JMHSample_02 -f 1 2.maven ...
- python opencv:像素操作
图片的像素 像素:组成图片的单位 RGB:颜色由 RGB三种颜色组成 颜色深度:对于8bit的颜色深度来说,它可以表示的颜色范围是 0 ~ 255,对于RGB图片来说,8位颜色深度可以表示 (2^8) ...
- Bugku-CTF加密篇之简单加密(e6Z9i~]8R~U~QHE{RnY{QXg~QnQ{^XVlRXlp^XI5Q6Q6SKY8jUAA)
简单加密 e6Z9i~]8R~U~QHE{RnY{QXg~QnQ{^XVlRXlp^XI5Q6Q6SKY8jUAA
- 【转】弹出USB大容量存储设备时出问题的解决方法
原文链接 如下图所示,这个问题,相信很多人都有遇到过,而且经常难以解决,试了很多方法都无效.到最后,只能抱着侥幸的心理直接拔出,如果运气好,可能没有事,如果运气不好,你的U盘或者移动硬盘就要从此报废了 ...
- JS获取光标在input 或 texterea 中下标位置
<textarea placeholder="请输入表达式" id="methodInput" ></textarea> 获取位置: v ...
- python字典操作方法详解
前言 字典是一种通过名字或者关键字引用的得数据结构,key 类型需要时被哈希,其键可以是数字.字符串.元组,这种结构类型也称之为映射.字典类型是Python中唯一內建的映射类型. 注意,浮点数比较很不 ...