本题给定一个nm的区域,每一个点(坐标都有一个11的方块)没敲击这个区域,这一个方块就会掉落。(连锁反应)其他方块掉落的条件是该方块的上方或者下方已经有其中一块方块掉落并且左边和右边有其中一个方块掉落。输入敲打位置的坐标,需要你输出敲打该位置时会掉落的方块数量。另外如果敲打的位置没有方块则掉落方块的数量为0。
【基础DFS】
附上自打ac代码:
#include<bits/stdc++.h>
using namespace std;
int a[2005][2005];
int ans=0;
int n,m,q;
void dfs(int x,int y)
{
if((x+1)<=n)
if(a[x+1][y]==1)
{
if(((y-1)>=1&&a[x+1][y-1]==0)||(a[x+1][y+1]==0&&(y+1)<=m))
{
ans++;
a[x+1][y]=0;
dfs(x+1,y);
}
}
if((y+1)<=m)
if(a[x][y+1]==1)
{
if(((x-1)>=1&&a[x-1][y+1]==0)||(a[x+1][y+1]==0&&(x+1)<=n))
{
ans++;
a[x][y+1]=0;
dfs(x,y+1);
}
}
if((x-1)>=1)
if(a[x-1][y]==1)
{
if((a[x-1][y-1]==0&&(y-1)>=1)||(a[x-1][y+1]==0&&(y+1)<=m))
{
ans++;
a[x-1][y]=0;
dfs(x-1,y);
}
}
if((y-1)>=1)
if(a[x][y-1]==1)
{
if((a[x+1][y-1]==0&&(x+1)<=n)||(a[x-1][y-1]==0&&(x-1)>=1))
{
ans++;
a[x][y-1]=0;
dfs(x,y-1);
}
}
return;
}
int main()
{
std::ios::sync_with_stdio(false);
int T;
while(cin >> T)
{
while(T--)
{
cin >> n >> m >> q ;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
a[i][j]=1;
}
}
while(q--)
{
int x,y;
cin >> x >> y ;
ans=0;
if(a[x][y]!=0)
{
a[x][y]=0;
ans++;
}
dfs(x,y);
cout << ans << endl ;
}
}
}
return 0;
}