题目传送门

 /*
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
*/

BFS 2015百度之星初赛2 HDOJ 5254 棋盘占领的更多相关文章

  1. 数学 2015百度之星初赛2 HDOJ 5255 魔法因子

    题目传送门 /* 数学:不会写,学习一下这种解题方式:) 思路:设符合条件的数的最高位是h,最低位是l,中间不变的部分为mid,由题意可得到下面的公式(这里对X乘上1e6用a表示,b表示1e6) (h ...

  2. LIS 2015百度之星初赛2 HDOJ 5256 序列变换

    题目传送门 题意:中文题面 分析:LIS(非严格):首先我想到了LIS,然而总觉得有点不对:每个数先减去它的下标,防止下面的情况发生:(转载)加入序列是1,2,2,2,3,这样求上升子序列是3,也就是 ...

  3. Kruskal 2015百度之星初赛2 HDOJ 5253 连接的管道

    题目传送门 /* 最小生成树(Kruskal):以权值为头,带入两个端点,自然的排序;感觉结构体的并查集很好看 注意:题目老头要的是两个农田的高度差,中文水平不好,题意理解成和平均值的高度差! */ ...

  4. 二分搜索 2015百度之星初赛1 HDOJ 5248 序列变换

    题目传送门 /* 二分搜索:在0-1e6的范围找到最小的max (ai - bi),也就是使得p + 1 <= a[i] + c or a[i] - c 比赛时以为是贪心,榨干智商也想不出来:( ...

  5. 二分查找 2015百度之星初赛1 HDOJ 5246 超级赛亚ACMer

    题目传送门 /* 二分找到不大于m的最大的数,记做p,只要a[p] + k <= a[p+1]就继续 注意:特判一下当没有比m小的数的情况:) */ #include <cstdio> ...

  6. 2015百度之星初赛2 1005 序列变换(LIS变形)

    LIS(非严格):首先我想到了LIS.然而总认为有点不正确:每一个数先减去它的下标.防止以下的情况发生:(转载) 3 增加序列是1,2,2,2,3,这样求上升子序列是3.也就是要改动2个,可是中间的两 ...

  7. 2016百度之星 初赛2A ABEF

    只做了1001 1002 1005 1006.剩下2题可能以后补? http://acm.hdu.edu.cn/search.php?field=problem&key=2016%22%B0% ...

  8. HDU 5690:2016"百度之星" - 初赛 All X

    原文链接:https://www.dreamwings.cn/hdu5690/2657.html All X Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  9. 2016"百度之星" - 初赛(Astar Round2A)HDU 5695 拓扑排序+优先队列

    Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

随机推荐

  1. 新建web项目时css注意事项

    初始化css ,如设置body的margin,padding值,button:hover的pointer手型,li dd的list-style,a的下划线等. 最好将常用的初始化css文件整合在一起, ...

  2. 关于animate的一些属性

    animate() 方法执行 CSS 属性集的自定义动画.该方法通过CSS样式将元素从一个状态改变为另一个状态.CSS属性值是逐渐改变的,这样就可以创建动画效果.只有数字值可创建动画(比如 " ...

  3. Lesson one of python

    Test1:Use the powershell to output the contents print "Hello World!" print "Hello Aga ...

  4. 【转载】Android Studio简单设置

    界面设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面.Settings --> Appearance --> Theme ,选择 Darcula 主题即可 ...

  5. fuse的mount机制 2 -系统调用mount

    经过上一篇的分析,目前已经知道mount函数最终进入到mount.c 中的 int fuse_kern_mount(const char *mountpoint, struct fuse_args * ...

  6. Viewpager animation duration setting

    private void animatePagerTransition(final boolean forward) { ValueAnimator animator = ValueAnimator. ...

  7. Ubuntu 16.04 如何使用Samba服务器

    对于Windows与Ubuntu之间的数据传输,我们习惯于使用FTP工具,不过还是有学员问到samba服务器搭建和使用的问题,这便是本文的来由. Ubuntu版本:ARM裸机1期加强版配套的Ubunt ...

  8. View Controller Programming Guide for iOS---(一)---About View Controllers

    About View Controllers View controllers are a vital link between an app’s data and its visual appear ...

  9. hdoj1078【DP·记忆化搜索】

    还是满水的一道题目吧...这个一看肯定要搜索了..然后又是这么DP,那就是记忆化搜索了...走K步,下一步要比他多...很好写啊/// #include<iostream> #includ ...

  10. Codeforces Round #357 (Div. 2)C. Heap Operations

    用单调队列(从小到大),模拟一下就好了,主要是getMin比较麻烦,算了,都是模拟....也没什么好说的.. #include<cstdio> #include<map> #i ...