Block Breaker(hdu第十场多校签到题)

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
dfs裸题。
敲击一个方块,看有多少方块会击落。击落的条件是左右至少有一个击落并且上下至少一个被击落。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e3+100;
bool vis[maxx][maxx];
int d[][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m,q,ans;

inline void dfs(int x,int y)
{
	if(vis[x][y]) return ; 
	vis[x][y]=1;
	for(int i=0;i<4;i++)
	{
		int tx=x+d[i][0];
		int ty=y+d[i][1];
		if(tx<1||tx>n||ty<1||ty>m||vis[tx][ty]) continue;
		if(vis[tx][ty-1]&&((tx>1&&vis[tx-1][ty])||(tx<n&&vis[tx+1][ty]))&&ty>1) ans++,dfs(tx,ty);
		else if(vis[tx][ty+1]&&((tx>1&&vis[tx-1][ty])||(tx<n&&vis[tx+1][ty]))&&ty<m) ans++,dfs(tx,ty);
		else if(vis[tx+1][ty]&&((ty>1&&vis[tx][ty-1])||(ty<m&&vis[tx][ty+1]))&&tx<n) ans++,dfs(tx,ty);
		else if(vis[tx-1][ty]&&((ty>1&&vis[tx][ty-1])||(ty<m&&vis[tx][ty+1]))&&tx>1) ans++,dfs(tx,ty);
	}
}
int main()
{
	int t,x,y;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d",&n,&m,&q);
		memset(vis,0,sizeof(vis));
		while(q--)
		{
			ans=0;
			scanf("%d%d",&x,&y);
			if(vis[x][y]==0)
			{
				ans++;
				dfs(x,y);
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值