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

最新文章

  1. 深入理解CSS中的margin负值
  2. [Unity3D插件]2dToolKit系列三 碰撞检测功能的实现以及障碍物的随机摆放
  3. 如何加速MATLAB代码运行
  4. 使用Spring + Jedis集成Redis
  5. ios各种手势,很有意思
  6. Leetcode#79 Word Search
  7. Unity3d 项目管理 版本管理
  8. pytesser图片文本识别
  9. Andorid Binder进程间通信---总结
  10. Netty中的连接管理
  11. Jboss项目部署出现java.lang.UnsupportedClassVersionError 问题的解决方法
  12. Linux进程退出详解(do_exit)--Linux进程的管理与调度(十四)
  13. mybatis环境配置与入门例子
  14. verilog语法实例学习(13)
  15. windows下安装pycharm并连接Linux的python环境
  16. 阿里开源 iOS 协程开发框架 coobjc!--异步编程的问题与解决方案
  17. 使用Xshell和Xftp部署简单的项目
  18. LoadRunner内部结构
  19. Code First配合Entity Framework Power Tools Beta 4使用
  20. Cookie,Sesstion,Application 缓存。

热门文章

  1. ASP.NET Core搭建多层网站架构【15-扩展之使用Obfuscar混淆加密保护代码】
  2. December 28th, Week 52nd Saturday, 2019
  3. python opencv:保存图像
  4. Docker Learning Notes
  5. sqlserver数据库中char、varchar、text与nchar、nvarchar、ntext数据类型使用详解
  6. 给Linux系统运维新手的四点建议
  7. iOS ViewController 中代码书写规范
  8. 实验一&amp;#160;&amp;#160;GIT 代码版本管理
  9. 助力企业战疫提效保质,腾讯wetest远程办公工具包请查收!
  10. GUI编程与CLI编程