原题链接: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® API Code Pack for Microsoft® .NET Framework

    相比之前的操作系统,Window 7(or Vista)提供了很多新特性,我们在应用实现中可以利用这些特性来提升用户体验. 这些特性主要包括以下几个方面: Shell Enhancements Dir ...

  2. iOS-KVC和KVO精炼讲解(干货)

    一.KVO介绍 KVO就是观察者模式,说白了就是你关心的一个值改变了,你就会得到通知.你就可以在你想处理的地方处理这个值. 二.KVO的使用 一般分为三步: 注册监听 使用方法: /** * 添加KV ...

  3. GForms开发平台

    1. 开发平台概述 1.1. 产品概述 GForms开发平台让开发人员甚至非技术人员在短短几分钟内创建全功能的展现服务,让开发团队更加适应客户和市场的需求,从而提高客户服务和速度实现收益. GForm ...

  4. Data Mining Resources

    韩家炜 http://www.cs.uiuc.edu/~hanj/ 著名数据挖掘书籍,<数据挖掘概念和技术>作者,在DM界久负盛名.他的个人主页里面有很多他的papers,都非常经典:还有 ...

  5. python中文json串创建与解析

    下面代码,举例说明了json如何创建和解析含有中文的json串: #coding=gbk import os import sys reload(sys) sys.setdefaultencoding ...

  6. 去除wordpress由代发

    在服务器上安装好wordpress后,通过程序发送邮件却显示...由<www@hostname>代发,解决办法很简单:进入程序文件夹wp-includes修改pluggable.php文件 ...

  7. 如何在java类中读取Properties配置文件

    在com.example包下有一个test.properties文件和测试类PropertyReadTest.java. test.properties 文件内容: author=zeige  tea ...

  8. wpf窗体中复合控件焦点控制

    1.         自定义控件 在UserControl标记中 <UserControl KeyboardNavigation.ControlTabNavigation="Local ...

  9. js 函数闭包内部返回函数体调用方法难点解答

    今天在网上,看到一篇关于js函数难点的文章,js函数的一些难点.在那上面提了一下,关于js函数返回另一个函数的问题,并附上了一道面试题: var add = function(x){ var sum ...

  10. 无法安装程序包“MIcrosoft.Owin.Security 2.0.2”。您正在尝试将此程序包安装到某个将“.NETFramework,Version=v4.0”作为目标的项目中。

    在VS2010 MVC4项目中,安装NuGet程序包Microsoft.AspNet.SignalR时出现以下错误: 原因是安装的版本是Microsoft.AspNet.SignalR 2.0.2,要 ...