题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5254
解题思路:暴力枚举即可,没什么好讲的。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 505;
int n,m,g,map[maxn][maxn];
bool check(int i,int j)
{
int tmp1 = 0,tmp2 = 0,tmp3 = 0,tmp4 = 0;
if(i > 1 && map[i-1][j] == 1)
tmp1 = 1;
if(i < n && map[i+1][j] == 1)
tmp2 = 1;
if(j > 1 && map[i][j-1] == 1)
tmp3 = 1;
if(j < m && map[i][j+1] == 1)
tmp4 = 1;
if(tmp1 == 1 && tmp3 == 1) return true;
if(tmp1 == 1 && tmp4 == 1) return true;
if(tmp2 == 1 && tmp3 == 1) return true;
if(tmp2 == 1 && tmp4 == 1) return true;
return false;
}
int main()
{
int t,x,y,cas = 1;
scanf("%d",&t);
while(t--)
{
memset(map,0,sizeof(map));
scanf("%d%d",&n,&m);
scanf("%d",&g);
while(g--)
{
scanf("%d%d",&x,&y);
map[x][y] = 1;
}
while(true)
{
bool ok = true;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(map[i][j] == 0 && check(i,j))
{
map[i][j] = 1;
ok = false;
}
if(ok) break;
}
int ans = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(map[i][j])
ans++;
printf("Case #%d:\n%d\n",cas++,ans);
}
return 0;
}