原题链接: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. 北大ACM(POJ1014-Dividing)

    Question:http://poj.org/problem?id=1014 问题点:抽屉原理.dfs.多重背包. Memory: 248K Time: 16MS Language: C++ Res ...

  2. wsus安装与部署——下

    转载请注明原出处 write by xiaoyang 一.            测试 1.         使用客户机或者在域环境下编辑GPO打开组策略 2.         配置自动更新 3.   ...

  3. jQuery异步分页插件

    学校软件工程让写课程设计(其实就是自选语言做个项目),感觉都是重复的东西就没有很认真的去写内容,更加注意写一些之前没有用过的东西. 因为一直都使用TP框架来写PHP,TP又自带分页类,想到这里就想试试 ...

  4. 页面传值总结Block

    // AppDelegate.m // 页面传值总结 // // Created by qianfeng on 15/6/13. // Copyright (c) 2015年 qianfeng. Al ...

  5. Mac下github的使用

    新建github账户   新建Repository,如下图:   建立连接github的秘钥 打开mac的shell cd ~ mkdir .ssh cd .ssh ssh-keygen -t rsa ...

  6. spring3中新增的@value注解

    在spring 3.0中,可以通过使用@value,对一些如xxx.properties文件 中的文件,进行键值对的注入,例子如下: 1 首先在applicationContext.xml中加入:   ...

  7. hdu 2602 Bone Collector 背包入门题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题目分析:0-1背包  注意dp数组的清空, 二维转化为一维后的公式变化 /*Bone Coll ...

  8. HashSet 读后感

    HashSet实现Set,是一个不能重复元素的集合,内部使用HashMap实现.因此具有HashMap的特性,如不保证元素插入的顺序,线程不安全,允许null.HashSet的元素就是内部HashMa ...

  9. IAR产生可烧录的镜像文件

    Technorati 标签: IAR 烧录镜像 在IAR中,产生能够使用烧录器烧写的hex文件的方法: 1. 只生成可烧写的hex文件:     1.1   在Project Option中的Link ...

  10. Android的Handler与Activity线程同步

    假设这里有同一个Runnable对象r. 可能采用的方法有: 第一种: handler.post(r); 实际上这种方法并没有调用线程someThread的start方法,而是直接调用了Runaabl ...