Codeforces 598D Igor In the Museum(bfs)

4062 단어
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.
Examples
input
5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3

output
6
4
10

input
4 4 1
****
*..*
*.**
****
3 2

output
8

제목: n*m의 그림 전시관에서 "."공지빈 지점의 좌표를 제시하고 이 점이 있는 공터 구역 옆의 벽에 모두 몇 폭의 그림이 있는지 물어보세요.
문제: bfs가 검색했다. 먼저 모든 공터 옆에 사면벽이 있다고 가정하고 그림의 수를 4로 한다. 검색 과정에서 이 공터의 상하좌우에 공터가 나타나면 벽의 수가 1로 줄어든다. 즉, 그림의 수가 1로 줄어든다.상하좌우가 경계를 벗어난 것도 벽에 해당한다.중요한 것은 k회 조회입니다. 매번 bfs는 시간을 초과합니다. 우리가 bfs가 지나간 모든 구역을 기록하고 다음에 이 구역의 점이 나타나면 결과를 직접 출력합니다.검색에서, 나는mark[][]의 그룹을 0, cnt=0으로 초기화했다.매번 검색 과정에서 우리는 지나간 공터를mark[i][j]=++cnt로 바꿉니다.마지막으로num[cnt]로 이 구역의 그림 수를 기록합니다.
우우~~~(> <)~~~, 마크 초기화[][] 수조는 SB가 bfs 함수에 일반적으로 썼는데 8번이나 줄을 그어도 TLE가 되지 않았다.
코드는 다음과 같습니다.
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 1010
using namespace std;
char map[maxn][maxn];
int n,m,ans,cnt;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int mark[maxn][maxn];
int num[maxn*maxn];
struct node
{
	int x,y;
}temp,a;

void bfs(int x,int y)
{
	int i,j;
	queue<node>q;
	a.x=x;
	a.y=y;
	q.push(a);
	mark[x][y]=cnt;
	ans=0;
	while(!q.empty())
	{
		a=q.front();
		q.pop();
		ans+=4;
		for(i=0;i<4;++i)
		{
			temp.x=a.x+dir[i][0];
			temp.y=a.y+dir[i][1];
			if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m)
			{
				if(map[temp.x][temp.y]=='.')
				{
					ans--;
					if(mark[temp.x][temp.y]==0)
					{
						q.push(temp);
						mark[temp.x][temp.y]=cnt;
					}
				}	
			}
			else
				ans--;
		}
	}
	num[cnt]=ans;
	printf("%d
",ans); } int main() { int i,j,k,x,y; while(scanf("%d%d%d",&n,&m,&k)!=EOF) { for(i=0;i<n;++i) scanf("%s",map[i]); memset(num,-1,sizeof(num)); memset(mark,0,sizeof(mark)); cnt=0; while(k--) { scanf("%d%d",&x,&y); x--; y--; if(mark[x][y]==0) { cnt++; bfs(x,y); } else printf("%d
",num[mark[x][y]]); } } return 0; }

좋은 웹페이지 즐겨찾기