题目传送门

 /*
BFS:先把1的入队,每个1和它相邻的组合后看看能不能使0变1,若有则添加入队,change函数返回改变了多少个0
注意:结果还要加上原来占领的
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
using namespace std; const int MAXN = 5e2 + ;
const int INF = 0x3f3f3f3f;
int a[MAXN][MAXN];
int dx[] = {-, -, , };
int dy[] = {-, , -, };
int n, m;
queue<pair<int, int> > Q; int change(int x, int y)
{ int cnt = ;
for (int i=; i<; ++i)
{
int tx = x + dx[i]; int ty = y + dy[i];
if (tx < || tx > n) continue;
if (ty < || ty > m) continue;
if (a[tx][ty] == )
{
if (i == || i == )
{
if (a[tx][ty+] == ) {a[tx][ty+] = ; ++cnt; Q.push (make_pair (tx, ty+));}
if (a[x][y-] == ) {a[x][y-] = ; ++cnt; Q.push (make_pair (x, y-));}
}
else if (i == || i == )
{
if (a[tx][ty-] == ) {a[tx][ty-] = ; ++cnt; Q.push (make_pair (tx, ty-));}
if (a[x][y+] == ) {a[x][y+] = ; ++cnt; Q.push (make_pair (x, y+));}
}
}
} return cnt;
} int BFS(void)
{
int ans = ;
while (!Q.empty ())
{
int x = Q.front ().first; int y = Q.front ().second; Q.pop ();
ans += change (x, y);
} return ans;
} int main(void) //2015百度之星初赛2 HDOJ 5254 棋盘占领
{
int t, cas = ; scanf ("%d", &t);
while (t--)
{
while (!Q.empty ()) Q.pop ();
scanf ("%d%d", &n, &m);
memset (a, , sizeof (a)); int g; scanf ("%d", &g);
int cnt = ;
while (g--)
{
int x, y; scanf ("%d%d", &x, &y);
if (a[x][y] == )
{
cnt++; a[x][y] = ; Q.push (make_pair (x, y));
}
} printf ("Case #%d:\n", ++cas);
printf ("%d\n", BFS () + cnt);
} return ;
} /*
4
2 2
2
1 1
2 2
3 3
3
1 1
2 3
3 2
2 4
5
1 1
1 1
1 2
1 3
1 4
2 4
2
1 1
2 4
*/

最新文章

  1. ELK+Kafka集群日志分析系统
  2. JavaScript学习1
  3. 判断一个url地址是不是404状态(用curl函数)
  4. hdu4940 Destroy Transportation system(2014多校联合第七场)
  5. [转]SqlPlus安装配置
  6. 将数字映射到字母上 .xml
  7. python 递归遍历文件夹
  8. 3.依赖倒置原则(Dependence Inversion Principle)
  9. [置顶] 【Git入门之十四】Git GUI
  10. Android MVP框架模式
  11. 自定义DTD(myeclipser的XML提示功能)
  12. hdu2206IP的计算
  13. 【bird-java】bird-java概述
  14. Python数据分析(一): ipython 技巧!
  15. CodeForces #549 Div.2 ELynyrd Skynyrd 倍增算法
  16. 学习笔记78—三大统计相关系数:Pearson、Spearman秩相关系数、kendall等级相关系数
  17. python_超级基础
  18. UVa 10561 Treblecross (SG函数)
  19. HTTTP及TCP的超时以及KEEP-ALIVE机制小结
  20. 复习一下xml(c)

热门文章

  1. Seesion和Cookie详解2
  2. HashMap随机取值和迭代器取值的对比
  3. xamarin.android searchview的一些用法
  4. HttpSession and Hibernate session
  5. virtualbox创建centos7虚拟机
  6. Deep Learning 36:python中的一些函数
  7. 关于android的DB操作
  8. caioj1462: 【EXKMP】回文串
  9. sed替换变量
  10. hadoop datanode启动失败(All directories in dfs.data.dir are invalid)