【Educational Codeforces Round 1D】【DFS 联通块打标记法】Igor In the Museum 联通块内墙的面数

原创 2015年11月19日 11:14:18
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 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.

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

#include<stdio.h> 
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=1010,M=0,Z=1e9+7,ms63=1061109567;
const int dy[4]={-1,0,0,1};
const int dx[4]={0,-1,1,0};
int casenum,casei;
char a[N][N];
int vis[N][N];
int rec[N*N];
int ans[N][N];
int n,m,q,tim,num;
void dfs(int y,int x)
{
	vis[y][x]=tim;
	for(int i=0;i<4;i++)
	{
		int yy=y+dy[i];
		int xx=x+dx[i];
		if(a[yy][xx]=='*')++num;
		else if(!vis[yy][xx])dfs(yy,xx);
	}
}
int main()
{
	while(~scanf("%d%d%d",&n,&m,&q))
	{
		MS(vis,0);
		tim=0;
		for(int i=1;i<=n;i++)scanf("%s",a[i]+1);
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)if(a[i][j]=='.')
			{
				if(vis[i][j]==0)
				{
					++tim;
					num=0;
					dfs(i,j);
					rec[tim]=num;
				}
				ans[i][j]=rec[vis[i][j]];
			}
		}
		int y,x;
		while(q--)
		{
			scanf("%d%d",&y,&x);
			printf("%d\n",ans[y][x]);
		}
	}
	return 0;
}
/*
【题意】
给你一个n(1000)*m(1000)的地图,有k(1e5)个询问,地图上的'*'代表墙,'.'代表空地。
一个墙最多有4个面。*与.相邻的就是其一个面。
对于每个询问,给出一个范围内的坐标(y,x),问你,从这个开始向四周走,能撞到多少面墙。

【类型】
简单dfs

【分析】
直接从每个'.'开始dfs,dfs到的'.'都打上同样的标记,表示其为是相同联通块,
相同连通块的答案是相同的。
同时对墙的每一面用bool数组打标记,被第一次搜到的话就用全局变量计数++
这个全局变量用来更新这个联通块的答案。
这样这道题就做完啦。

然后我们发现,
因为每个'.'最多走一次,
所以其实每面墙也只最多计数1次,它最多只会对一个联通块的答案贡献1.

【时间复杂度&&优化】
O(4nm)

*/


版权声明:题解中哪里写错请一定要指出来QwQ 转载还请注明下出处哦,谢谢^_^

相关文章推荐

CodeForces 598D-Igor In the Museum【DFS】

Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as...

Codeforces 598D Igor In the Museum(概率DP)

@(K ACMer) 题意: 在x轴上有n颗高度为h的树,现在你将有12\frac{1}{2}的概率,砍最左边的树,同样的概率砍最右边的树.然后对于每个树它有pp的概率会向左边倒,(1−p)...

CodeForces 598D Igor In the Museum 暴力

题目大意:计算给定点所在区域中( . , * )对数。 暴力就好啦。这是D题。。 #include #include #define FOR(i,j,k) for(i=j;i...

D. Igor In the Museum(简单DFS)

先预处理出每一个联通块的答案,然后查询时就可以O(1)出答案; /* *********************************************** Author :x...

Codeforces 598D Igor In the Museum 离线处理+bfs

Codeforces 598D Igor In the Museum bfs+离线处理
  • PhyCode
  • PhyCode
  • 2016年04月03日 10:54
  • 225

CodeForces - 598D Igor In the Museum

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【预处理Bfs】

D. Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes in...

Educational Codeforces Round 7 E. Ants in Leaves(DFS+贪心)

题目链接:点击打开链接 题意:给出一棵n个结点的树, 每个叶子结点上有一只蚂蚁, 每秒每只蚂蚁可以向相邻结点走一步, 同一时刻同一结点上只能有最多一只蚂蚁(根结点除外),根结点为1, 求所有蚂蚁都移...

Educational Codeforces Round 18 -- D. Paths in a Complete Binary Tree(二叉树模拟)

题意: 给你一棵完全二叉树, 要求从某个结点出发, 开始行走,可以向上走,向左走,向右走, 模拟这个过程。 思路: 观察那个二叉树的形状, 直接二分走就可以找到每一个结点。 我们先找到那个结点...

【Educational Codeforces Round 6E】【线段树 dfs序】New Year Tree 子树颜色修改子树颜色数

E. New Year Tree time limit per test 3 seconds memory limit per test 256 megabytes ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Educational Codeforces Round 1D】【DFS 联通块打标记法】Igor In the Museum 联通块内墙的面数
举报原因:
原因补充:

(最多只允许输入30个字)