原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4101

  一看之下以为是博弈,后来分析才知道只是搜索题。

  首先,我们需要从值为-1的位置由内向外搜索一次,标记出包围-1并且值不为0的最近一圈区域(这个临界区域记为A,那么A区域一旦在某个缺口打破,那么就可以定胜负了),如果发现此次搜索出了边界,那么肯定是Ali赢,否则进行以下步骤:从外围开始由外向内搜索,如果遇到非A区域的石头,总计和ans加上当前位置的HP,如果遇到的是A区域的石头,总计和ans加上当前位置HP-1(必须要留下一个HP,否则对方就赢了),最后求得的ans也就是Ali能够实施的合法步骤数,根据ans的奇偶性便能得出答案。

 #include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int maxn = + ;
typedef pair<int,int> pii;
int g[maxn][maxn];
bool h[maxn][maxn];
int dr[][] = {{, }, {-, }, {, }, {, -}};
bool vis[maxn][maxn]; int N, M;
int dx, dy; struct node
{
int x, y;
}; bool bfs1()
{
memset(h, false, sizeof h);
queue<node> que;
node t;
t.x = dx, t.y = dy;
h[dy][dx] = true;
que.push(t);
while(!que.empty())
{
node cur = que.front();
que.pop();
int cx = cur.x;
int cy = cur.y;
for(int i = ; i < ; i++)
{
int nx = cx + dr[i][];
int ny = cy + dr[i][];
if(nx > && nx <= M && ny > && ny <= N)
{
if(!h[ny][nx])
{
h[ny][nx] = true;
if(g[ny][nx] == )
{
t.x = nx, t.y = ny;
que.push(t);
}
}
}
else
return true;
}
}
return false;
} int bfs2()
{
memset(vis, false, sizeof vis);
int ans = ;
queue<node> que;
node s, t;
s.x = , s.y = ;
vis[][] = true;
que.push(s);
while(!que.empty())
{
node cur = que.front();
que.pop();
int cx = cur.x;
int cy = cur.y;
for(int i = ; i < ; i++)
{
int nx = cx + dr[i][];
int ny = cy + dr[i][];
if(nx >= && nx <= M + && ny >= && ny <= N + )
{
if(!vis[ny][nx])
{
vis[ny][nx] = true;
if(!h[ny][nx])
{
ans += g[ny][nx];
t.x = nx, t.y = ny;
que.push(t);
}
else if(h[ny][nx] && g[ny][nx] > )
{
ans += g[ny][nx] - ;
}
}
}
}
}
return ans;
} int main()
{
while(scanf("%d %d", &N, &M) != EOF)
{
memset(g, , sizeof g);
for(int i = ; i <= N; i++)
{
for(int j = ; j <= M; j++)
{
scanf("%d", &g[i][j]);
if(g[i][j] == -)
dx = j, dy = i;
}
}
if(bfs1())
{
puts("Ali Win");
continue;
}
int ans = bfs2();
if(ans & )
puts("Ali Win");
else
puts("Baba Win");
}
return ;
}

HDU 4101 Ali and Baba的更多相关文章

  1. HDU 4101 Ali and Baba (思路好题)

    与其说这是个博弈,倒不如说是个搜索.这题思路不错,感觉很难把情况考虑周全. 在地图外围填充一圈0,两次BFS,第一次从-1点出发,把从-1到达的0点以及包围0的那一圈石头标记出来.如下图: 1 1 1 ...

  2. hdu 4101

    比赛的时候先是受以前一个圣神海的题目 用了两遍DFS 第一遍标记出围墙  第二遍求围墙外和每块围墙降为1所需的攻击次数  结果爆栈  改为BFS后AC DFS的加了一句这个 #pragma comme ...

  3. 阿里校招内推C++岗位编程题第一题 空格最少的字符串

    给定一个字符串S和有效单词的字典D,请确定可以插入到S中的最小空格数,使得最终的字符串完全由D中的有效单词组成.并输出解. 如果没有解则应该输出n/a 例如: 输入: S = “ilikealibab ...

  4. BFS-hdu-4101-Ali and Baba

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4101 题目大意: 给一个矩阵,0表示空的可走,-1宝藏的位置(只有一个),其余的正整数表示该位置石头 ...

  5. HDU 5919 Sequence II(主席树+逆序思想)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

  6. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  7. HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  8. HDU 4622 Reincarnation Hash解法详解

    今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...

  9. HDU 6278 主席树(区间第k大)+二分

    Just h-index Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)To ...

随机推荐

  1. ubuntu tty 永久修改中文环境为英文

    以下代码只针对当前用户tty1有效, 对我来说足够了 vim ~/.bashrc 加入如下代码 if [ "$(tty)" = "/dev/tty1" ]; t ...

  2. JS源码(条件的判定,循环,数组,函数,对象)整理摘录

    --- title: JS学习笔记-从条件判断语句到对象创建 date: 2016-04-28 21:31:13 tags: [javascript,front-end] ---JS学习笔记——整理自 ...

  3. sort()的多种用法

    sort()  方法用于对数组的元素进行排序. 一.默认情况 在默认情况下, sort() 方法按升序排列数组项.为了实现排序, sort() 方法会调用每个数组项的 toString() 转型方法, ...

  4. 9 款赏心悦目的 HTML5/CSS3 特效

    1.HTML5 WebGL实验,超酷的HTML5 Canvas波浪墙 这是一款HTML5 Canvas实验项目,也是波浪特效,只是这不是真正的水波,而是利用柱体高度的变化实现的波浪墙效果. 在线演示 ...

  5. 常用的HTML 标签二

    <marquee></marquee> 滚动的文字,也称"走马灯" 语法格式 <marquee 属性="属性值">内容< ...

  6. C++向main函数传递参数的方法(实例已上传至github)

    通常情况下,我们定义的main函数都只有空形参列表: int main(){...} 然而,有时我们确实需要给mian传递实参,一种常见的情况是用户设置一组选项来确定函数所要执行的操作.例如,假定ma ...

  7. Linux驱动编程--基于I2C子系统的I2C驱动

    代码中,我添加了很多注释,应该不难理解,有错误大家可以指出来,我再改正 #include <linux/kernel.h> #include <linux/module.h> ...

  8. wordpress使用video.js与七牛云存储实现无广告视频分享应用

    video.js是一款极受欢迎的基于HTML5的开源WEB视频播放器,其充分利用了HTML5的视频支持特性,可以实现全平台的无视频插件播放功能,对于现在流行的手机.PAD等移动智能终端有极佳的应用体验 ...

  9. laravel--为什么属性在模型中没有定义,却取出来了值,这些属性哪里来的

    看laravel模型中的这段代码, public function getLimitUsersAttribute() { return $this->user_limit - $this-> ...

  10. php实现input输入框失去焦点自动保存输入框的数据

    最近做一个输入框失去焦点时自动保存数据的功能,当然就是jQuery选择器选择input,blur时,ajax提交数据给php文件,php文件保存一下数据咯.主要是要注意一下中文的问题,所以中间需要转一 ...