HDU 4101 Ali and Baba
原题链接: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的更多相关文章
- HDU 4101 Ali and Baba (思路好题)
与其说这是个博弈,倒不如说是个搜索.这题思路不错,感觉很难把情况考虑周全. 在地图外围填充一圈0,两次BFS,第一次从-1点出发,把从-1到达的0点以及包围0的那一圈石头标记出来.如下图: 1 1 1 ...
- hdu 4101
比赛的时候先是受以前一个圣神海的题目 用了两遍DFS 第一遍标记出围墙 第二遍求围墙外和每块围墙降为1所需的攻击次数 结果爆栈 改为BFS后AC DFS的加了一句这个 #pragma comme ...
- 阿里校招内推C++岗位编程题第一题 空格最少的字符串
给定一个字符串S和有效单词的字典D,请确定可以插入到S中的最小空格数,使得最终的字符串完全由D中的有效单词组成.并输出解. 如果没有解则应该输出n/a 例如: 输入: S = “ilikealibab ...
- BFS-hdu-4101-Ali and Baba
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4101 题目大意: 给一个矩阵,0表示空的可走,-1宝藏的位置(只有一个),其余的正整数表示该位置石头 ...
- HDU 5919 Sequence II(主席树+逆序思想)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
- hdu 5919 主席树(区间不同数的个数 + 区间第k大)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- 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 ...
- HDU 4622 Reincarnation Hash解法详解
今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...
- HDU 6278 主席树(区间第k大)+二分
Just h-index Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)To ...
随机推荐
- mysql数据库用户和权限管理记录
一.MySQL用户的基本说明: 1.1 用户的基本结构MySQL的用户:用户名@主机 ■用户名:16个字符以内■主机:可以是主机名.IP地址.网络地址等主机名:www.111cn.net,localh ...
- 基于python yield机制的异步操作同步化编程模型
又一个milestone即将结束,有了些许的时间总结研发过程中的点滴心得,今天总结下如何在编写python代码时对异步操作进行同步化模拟,从而提高代码的可读性和可扩展性. 游戏引擎一般都采用分布式框架 ...
- 数据库设计(字段)中的char、varchar、text和nchar、nvarchar、ntext的区别
char.varchar.text和nchar.nvarchar.ntext的区别 1.CHAR.CHAR存储定长数据很方便,CHAR字段上的索引效率级高,比如定义char(10),那么不论你存储的数 ...
- 使用DbVisualizer 8 连接Oracle数据库
1. 网上下载一个驱动包ojdbc14.jar,放到oracle目录下:...\DbVisualizer-8.0.1\jdbc\oracle\ojdbc14.jar 2. 打开 DbVisualize ...
- USB时钟为什么是48MHz
在学习2440的USB配置时钟中,发现它的时钟需要设置成48MHz固定的,这个我就来兴趣了,为什么这里面USB的时钟一定要是48M呢?在网上找了众多文章,都是讲解如何配置2440的MPLL和U ...
- ARM寄存器的8种寻址方式01
一.立即数寻址 操作数由指令本身给出 MOV r0,#0x0F //是所有寻址方式里面速度最快的,但是受到合法立即数的限制 立即数要求以#和$开头 十六进制,#后跟0x:十进制,#后直接加:八进制,# ...
- Windows Phone 8 蓝牙编程
蓝牙是手机的近距离无限传输的技术,在之前的Windows Phone 7系统手机里面仅支持蓝牙耳机功能,并不支持蓝牙文件信息传输,那么在Windows Phone 8手机里面将全面支持蓝牙技术,并且提 ...
- 【风马一族_Android】手机与电脑通过adb进行连接
1:打开电脑的命令行 cmd 2:adb devices 查看与电脑连接的手机或模拟器的名称 3:准备要安装的apk.记住手机的名称 4:adb –s <模拟器名称> install & ...
- [转]bat中的特殊字符,以及需要在bat中当做字符如何处理
bat中的特殊字符,以及需要在bat中当做字符如何处理 批处理.Bat 中特殊符号的实际作用,Windows 批处理中特殊符号的作用: @ \\隐藏命令的回显. ~ \\在for中表示使用增强的变量扩 ...
- Eclipse中使用javap运行配置详解
javap是sun提供的对class文件进行反编译的工具 1.配置Run---external tools---external tools configurations 选择Program 新建ja ...