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.
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.
Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.
5 6 3
******
*..*.*
******
*....*
******
2 2
2 5
4 3
6
4
10
4 4 1
****
*..*
*.**
****
3 2
8
题意:在一个n*m的画展馆里,“ . ”表示空地,“ * ”表示墙,每一个空地旁边的墙面上都有一幅画。给出一个空地点的坐标,问这个点所在的空地区域旁边的墙上一共有多少副画。
题解:bfs搜啦,先假设每一个空地旁边都是四面墙,画数加4,在搜索过程中,该空地上下左右出现一个空地,就表示墙数减少1,即画数减一。上下左右出界了,也相当于墙。 重要的是k次查询,每一次都bfs会超时,所有我们把bfs过的区域记录下来,下次再出现该区域的点,直接输出结果。 在搜索中,我初始化mark[][]数组为0,cnt=0。在每次搜索过程中我们把走过的空地变为mark[i][j]=++cnt。最后用num[cnt]记录下这块区域的画数。
呜呜呜~~~~(>_<)~~~~,初始化mark[][]数组是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\n",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\n",num[mark[x][y]]); } } return 0; }