(CodeForces - 598D)Igor In the Museum
2017-09-07 08:17
344 查看
(CodeForces - 598D)Igor In the Museum
time limit per test:1 secondmemory 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
64
10
Input
4 4 1****
*..*
*.**
****
3 2
Output
8题目大意:在一个n*m矩形细胞块中,”.”代表空细胞,“*”代表不可逾越的细胞,若“.”和“*”相邻,则说他们可以构成一幅画,现在又k次询问,每次给出一个人的起点(x,y)保证为空细胞的位置,他可以上下左右移动,只能咋空细胞中走动,问他能看到几幅画。
思路:如果对于每一次询问都去dfs显然会超时。这里可以预处理每一个“.”可以看到几幅画,对于(x,y)可以用time[x][y]数组记录它是第几次dfs,用ans数组记录第几次dfs的结果,所以对于询问(x,y)只需要直接访问ans[time[x][y]]。
#include<cstdio> #include<cstring> using namespace std; const int maxn=1005; const int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; char a[maxn][maxn]; bool vis[maxn][maxn]; int ans[maxn*maxn],time[maxn][maxn]; int cnt,n,m,tot; bool check(int x,int y) { return (x>=1&&x<=n&&y>=1&&y<=m); } void dfs(int x,int y) { vis[x][y]=1; time[x][y]=tot; for(int i=0;i<4;i++) { int tmpx=x+dir[i][0]; int tmpy=y+dir[i][1]; if(check(tmpx,tmpy)&&!vis[tmpx][tmpy]) { if(a[tmpx][tmpy]=='*') cnt++; else dfs(tmpx,tmpy); } } } int main() { int k; scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++) scanf("%s",a[i]+1); memset(vis,0,sizeof(vis)); tot=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) bd2b if(a[i][j]=='.'&&!vis[i][j]) { cnt=0; tot++; dfs(i,j); ans[tot]=cnt; } while(k--) { int x,y; scanf("%d%d",&x,&y); printf("%d\n",ans[time[x][y]]); } return 0; }
相关文章推荐
- Codeforces 598D:Igor In the Museum
- codeforces_598D. Igor In the Museum(dfs)
- CodeForces 598 D. Igor In the Museum(dfs)
- Codeforces 598D:Igor In the Museum
- 【CodeForces】598D - Igor In the Museum
- CodeForces 598D-Igor In the Museum【DFS】
- CF 598D. Igor In the Museum【DFS】
- Educational Codeforces Round 1 D. Igor In the Museum BFS,带权并查集
- codeforces 598D - Igor In the Museum
- Educational Codeforces Round 1 D. Igor In the Museum
- Educational Codeforces Round 1 D. Igor In the Museum(BFS)
- Educational Codeforces Round 1 D. Igor In the Museum
- Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)
- Educational Codeforces Round 1 D.Igor In the Museum(DFS)
- Igor In the Museum CodeForces - 598D
- D. Igor In the Museum(简单DFS)
- 【Educational Codeforces Round 1D】【DFS 联通块打标记法】Igor In the Museum 联通块内墙的面数
- Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集
- Igor In the Museum(搜搜搜151515151515******************************************************1515151515151515151515)
- BFS 宽搜 B题 - Igor In the Museum