码迷,mamicode.com
首页 > 其他好文 > 详细

[dfs] HDU 2019 Multi-University Training Contest 10 - Block Breaker

时间:2019-08-21 19:02:06      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:选择   开始   输出   lease   bin   src   panel   provided   bre   

Block Breaker

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


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 (1T10), denoting the number of test cases.

For each test case:

The first line contains three positive integers n,m and q (1n,m2000,1q100000), 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 (1xin,1yim), 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

 

题意:

有n*m个块装在一个框内,以左上角为原点,竖直向下为x正半轴,水平向右为y正半轴建立坐标系,快和块之间和块和框架之间会有摩擦力,一个块如果上下或左右有方块或框架,则他们之间可以被固定住,否则就会落下
有q次操作,每次操作会将(x,y)位置的块敲掉,问每次操作最多能掉下多少方块,如果操作的位置没有方块则输出0

思路:

可以模拟,用dfs或bfs都行,这里选择dfs,从操作位置开始搜索,如果搜索超出了边界或搜到了空位置就返回,否则就把当前位置置为空且答案+1,
从当前位置向周围检查,如果超出边界就跳过(因为我把框架也当成方块,但这个方块必须被固定而不能落下,所以检查到边界时跳过)否则如果检查到的方块不为空且他上下没有对应方块且左右没有对应方块就搜索这个检查到的位置

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=2e3+5;
 4 int n,m,q,ans,dx,dy,td[5][3]={{1,0},{-1,0},{0,1},{0,-1}};
 5 bool idx[amn][amn];
 6 bool jg(int x,int y){
 7     return idx[x-1][y]&&idx[x+1][y]||idx[x][y-1]&&idx[x][y+1];      ///如果上下有对应方块或左右有对应方块则返回1,否则返回0
 8 }
 9 void dfs(int x,int y){
10     if(!idx[x][y]||x<1||x>n||y<1||y>m)return ;
11     idx[x][y]=0;
12     ans++;
13 //    cout<<x<<‘ ‘<<y<<endl;
14     for(int i=0;i<4;i++){
15         dx=x+td[i][0];
16         dy=y+td[i][1];
17         if(dx<1||dx>n||dy<1||dy>m)continue;
18         if(!jg(dx,dy)&&idx[dx][dy]){
19             dfs(dx,dy);
20         }
21     }
22 }
23 int main(){
24     int T,xi,yi;
25     scanf("%d",&T);
26     while(T--){
27         scanf("%d%d%d",&n,&m,&q);
28         for(int i=0;i<=n+1;i++)                 ///把边界也处理为方块,不过边界方块是固定的不能掉落
29             for(int j=0;j<=m+1;j++)
30                 idx[i][j]=1;
31         while(q--){
32             scanf("%d%d",&xi,&yi);
33             ans=0;
34             dfs(xi,yi);
35             printf("%d\n",ans);
36         }
37     }
38 }
39 /**
40 有n*m个块装在一个框内,以左上角为原点,竖直向下为x正半轴,水平向右为y正半轴建立坐标系,快和块之间和块和框架之间会有摩擦力,一个块如果上下或左右有方块或框架,则他们之间可以被固定住,否则就会落下
41 有q次操作,每次操作会将(x,y)位置的块敲掉,问每次操作最多能掉下多少方块,如果操作的位置没有方块则输出0
42 可以模拟,用dfs或bfs都行,这里选择dfs,从操作位置开始搜索,如果搜索超出了边界或搜到了空位置就返回,否则就把当前位置置为空且答案+1,
43 从当前位置向周围检查,如果超出边界就跳过(因为我把框架也当成方块,但这个方块必须被固定而不能落下,所以检查到边界时跳过)否则如果检查到的方块不为空且他上下没有对应方块且左右没有对应方块就搜索这个检查到的位置
44 **/

 

[dfs] HDU 2019 Multi-University Training Contest 10 - Block Breaker

标签:选择   开始   输出   lease   bin   src   panel   provided   bre   

原文地址:https://www.cnblogs.com/Railgun000/p/11390498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!