hdu6699Block Breaker

Problem Description
Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m square blocks of size 1×1. Due to the friction with the frame and each other, the blocks are stable and will not drop.

However, the blocks can be knocked down. When a block is knocked down, other remaining blocks may also drop since the friction provided by other remaining blocks may not sustain them anymore. Formally, a block will drop if it is knocked or not stable, which means that at least one of the left block and the right block has been dropped and at least one of the front block and the back block has been dropped. Especially, the frame can be regarded as a huge stable block, which means that if one block's left is the frame, only when its right block has been dropped and at least one of the front block and the back block has been dropped can it drop. The rest situations are similar.

Now you, the block breaker, want to knock down the blocks. Formally, you will do it q times. In each time, you may choose a position (xi,yi). If there remains a block at the chosen position, you will knock it down; otherwise, nothing will happen. Moreover, after knocking down the block, you will wait until no unstable blocks are going to drop and then do the next operation.

For example, please look at the following illustration, the frame is of size 2×2 and the block (1,1) and (1,2) have been dropped. If we are going to knock the block (2,2), not only itself but also the block (2,1) will drop in this knocking operation.

You want to know how many blocks will drop in total in each knocking operation. Specifically, if nothing happens in one operation, the answer should be regarded as 0.

Input
The first line contains one positive integer T (1≤T≤10), denoting the number of test cases.

For each test case:

The first line contains three positive integers n,m and q (1≤n,m≤2000,1≤q≤100000), denoting the sizes in two dimensions of the frame and the number of knocking operations.

Each of the following q lines contains two positive integers xi and yi (1≤xi≤n,1≤yi≤m), describing a knocking operation.

Output
For each test case, output q lines, each of which contains a non-negative integer, denoting the number of dropped blocks in the corresponding knocking operation.

Sample Input
2
2 2 3
1 1
1 2
2 2
4 4 6
1 1
1 2
2 1
2 2
4 4
3 3

Sample Output
1
1
2
1
1
2
0
1
11

递归求解,不过注意一定要使用scanf,printf;如果使用cin,cout一定会超时的,刚开始我就是这样显示超时。
AC代码:

include

include

using namespace std;
int a[2005][2005],n,m,q,s;
int d[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int panduan(int x,int y){
if((a[x-1][y]==-1||a[x+1][y]==-1)&&(a[x][y+1]==-1||a[x][y-1]==-1)) return 1;
else return 0;
}
void digui(int x,int y){
for(int i=0;i<4;i++){
if(x+d[i][0]>=1&&x+d[i][0]<=n&&y+d[i][1]>=1&&y+d[i][1]<=m&&a[x+d[i][0]][y+d[i][1]]!=-1&&panduan(x+d[i][0],y+d[i][1])){
a[x+d[i][0]][y+d[i][1]]=-1,s++,digui(x+d[i][0],y+d[i][1]);
}
}
return ;
}
int main(){
int t;scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&q);
memset(a,0,sizeof(a));
while(q--){
s=0;
int x,y;
scanf("%d%d",&x,&y);
if(a[x][y]!=-1){
a[x][y]=-1;
s++;
digui(x,y);
}
printf("%d\n",s);
}
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/sunjianzhao/p/11392902.html
今日推荐