原题链接: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. Windows删除大文件

    Temp是目录 或者是 文件很大很大很大很大 cmd rd /s /q Temp

  2. javascript中ajax post实例详解

    一,原生态的XMLHttpRequest 代码如下 复制代码 <script language="javascript">         function savei ...

  3. 【原创】Oracle函数中对于NO_DATA_FOUND异常处理的研究

    一直以来有一个困惑,一直没解决,昨天一哥们问我这个问题,决心弄清楚,终于得到了答案.先看下面这个函数: create or replace function fn_test(c_xm varchar) ...

  4. Cocos2d-JS坐标系

    在图形图像和游戏应用开发中坐标系是非常重要的,我们在Android和iOS等平台应用开发的时候使用的二维坐标系它的原点是在左上角的.而在Cocos2d-JS坐标系中它原点是在左下角的,而且Cocos2 ...

  5. Mac OS X中开启或关闭显示隐藏文件

    打开终端,输入:defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.app ...

  6. AMQ学习笔记 - 20. 使用Apache ActiveMQBrowser监控ActiveMQ

    概述 Apache ActiveMQBrowser可以用于查看AMQ中的消息.这里对其使用方法进行简单介绍. 使用介绍 1.下载并解压缩 下载地址:Apache ActiveMQBrowser,当前最 ...

  7. DBCP--""连接池创建"与"资源关闭"Util类

    import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.S ...

  8. CentOS学习笔记--程序管理

    程序管理 一个程序被加载到内存当中运行,那么在内存内的那个数据就被称为程序(process).程序是操作系统上非常重要的概念, 所有系统上面跑的数据都会以程序的型态存在.那么系统的程序有哪些状态?不同 ...

  9. RHEL7 Ansible

    [root@promote tt]# rpm -iUvh http://dl.Fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch ...

  10. php生成txt文件换行问题

    用双引号即"\r\n"换行,不能用单引号即'\r\n'.