D. Igor In the Museum
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

First line of the input contains three integers nm 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.

Output

Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.

Sample test(s)
input
5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3
output
6
4
10
input
4 4 1
****
*..*
*.**
****
3 2
output
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的更多相关文章

  1. 【CodeForces - 598D】Igor In the Museum(bfs)

    Igor In the Museum Descriptions 给你一个n*m的方格图表示一个博物馆的分布图.每个方格上用'*'表示墙,用'.'表示空位.每一个空格和相邻的墙之间都有一幅画.(相邻指的 ...

  2. 【Codeforces 598D】Igor In the Museum

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 同一个联通块里面答案都一样. 把每个联通块的答案都算出来 然后赋值就好 [代码] #include <bits/stdc++.h> ...

  3. 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 ...

  4. [BFS]Codeforces Igor In the Museum

     Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. 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 ...

  6. codeforces 598D Igor In the Museum

    题目链接:http://codeforces.com/problemset/problem/598/D 题目分类:dfs 题目分析:处理的时候一次处理一片而不是一个,不然会超时 代码: #includ ...

  7. Codeforces 598D (ccpc-wannafly camp day1) Igor In the Museum

    http://codeforces.com/problemset/problem/598/D 分析:BFS,同一连通区域的周长一样,但查询过多会导致TLE,所以要将连通区域的答案储存,下次查询到该连通 ...

  8. Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)

    题目链接:http://codeforces.com/problemset/problem/598/D 题意是 给你一张行为n宽为m的图 k个询问点 ,求每个寻问点所在的封闭的一个上下左右连接的块所能 ...

  9. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

随机推荐

  1. Docker容器CPU限制选项测试

    目录 Docker容器CPU限制选项测试 参考 实验环境 --cpu-shares选项 测试 结论 --cpus选项 测试 结论 --cpuset-cpus选项 测试 结论 Docker容器CPU限制 ...

  2. 微服务、分库分表、分布式事务管理、APM链路跟踪性能分析演示项目

    好多年没发博,最近有时间整理些东西,分享给大家. 所有内容都在github项目liuzhibin-cn/my-demo中,基于SpringBoot,演示Dubbo微服务 + Mycat, Shardi ...

  3. 浅谈SQL注入漏洞以及防范策略

    --HeShiwei 2014-5-15 什么是SQL注入 SQL注入,指的是用户通过向登录框输入恶意字符,利用代码的字符串拼接漏洞进行网站注入攻击,最终导致整个网站用户表信息泄露的攻击方式.黑客就是 ...

  4. 水平居中显示CSS

    HTML代码部分 <div class="center" > <img style="margin:0 auto ;" :src=item.i ...

  5. android studio 导入主题设置,代码风格(附带eclipse 主题代码样式)

    在这里我最想说的,android studio默认主题样式,太low.不适合长时间写代码,看代码颜色不好识别,相对于背景的代码样式,我都不想吐槽了.还是网上下载主题代码样式导入样式.在这里我推荐 Su ...

  6. 解决:mui 的 选项卡 + 下拉刷新 功能,在其中嵌入 iframe 后,在 iphone 的情况下,iframe 的内容不能滚动,只显示第一屏内容。

    我所遇到的情况是,使用 mui 的 选项卡 + 下拉刷新 功能时,其中有2个页面是嵌入了别的网站的页面,而别个几个是通过 ajax 加载本网站的数据.然后 在其中嵌入 iframe 后,在 iphon ...

  7. lnmp1.5安装fileinfo扩展

    cd /usr/local/src cd lnmp1.-full/src tar xvf php-.tar.bz2 cd php-/ext/fileinfo phpize ./configure -- ...

  8. HA: Infinity Stones-Write-up

    下载地址:点我 哔哩哔哩:点我 主题还是关于复仇者联盟的,这次是无限宝石的. 信息收集 虚拟机的IP为:192.168.116.137 ➜ ~ nmap -sn 192.168.116.1/24 St ...

  9. 0X01应用程序黑客技术

    前言 该文章主要是讲解了常见的应用程序黑客技术基本概念,包括消息钩取,API钩取,DLL注入,代码注入 天象独行 0X01:消息钩取 原理:在我们通过键盘,鼠标等输入信息过程中,Windows会通过钩 ...

  10. 二分查找及几种变体的Python实现

    1. 在不重复的有序数组中,查找等于给定值的元素 循环法 def search(lst, target): n = len(lst) if n == 0: return -1 low = 0 high ...