HDU 4101 Ali and Baba (思路好题)
与其说这是个博弈,倒不如说是个搜索。这题思路不错,感觉很难把情况考虑周全。
在地图外围填充一圈0,两次BFS,第一次从-1点出发,把从-1到达的0点以及包围0的那一圈石头标记出来。如下图:
1 1 1 1 1
1 1
1 3 5 1 1
- 1 1
第二次BFS,从外围(0,0)点出发,找出外面与标记石头的交界层:
6 7
1 1 1 1 1 1 1
0 0 0 0 0
0 3 5 1 1
0 -1 0 1
0 0 0 1
1 1 1 1 1 1
实际上起决定性作用的只有红色的那一圈和那一圈外的石头HP和,里面的石头HP不影响结果。
之前我的BFS只能处理这种情况(如下),就是里面的石头跟外面的那一圈石头是不相连的,却无法处理上面那两种情况(把绿色的3,5,1,4也加上了),所以一直WA……
-
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue> using namespace std; const int dx[] = { -, , , };
const int dy[] = { , , -, }; const int MAXN = ; struct Point
{
int x, y;
Point( int x = , int y = ):x(x), y(y) {}
}; bool vis[MAXN][MAXN];
int G[MAXN][MAXN];
bool flag[MAXN][MAXN];
int M, N;
Point st;
int sum;
int ok; void BFS()
{
ok = ;
queue<Point> Q;
Q.push( st );
memset( vis, false, sizeof(vis) );
memset( flag, false, sizeof(flag) );
vis[st.x][st.y] = true;
flag[st.x][st.y] = true;
while ( !Q.empty() )
{
Point cur = Q.front();
Q.pop();
if ( cur.x == || cur.x == N || cur.y == || cur.y == M ) ok = ;
for ( int i = ; i < ; ++i )
{
int xx = cur.x + dx[i];
int yy = cur.y + dy[i];
if ( xx >= && xx <= N && yy >= && yy <= M )
{
if ( !vis[xx][yy] )
{
vis[xx][yy] = true;
flag[xx][yy] = true;
if ( G[xx][yy] == )
Q.push( Point( xx, yy ) );
}
}
}
}
return;
} void BFS2( Point stt )
{
queue<Point> Q;
memset( vis, false, sizeof(vis) );
Q.push( stt );
vis[][] = true; while ( !Q.empty() )
{
Point p = Q.front();
Q.pop();
vis[ p.x ][ p.y ] = true;
sum += G[p.x][p.y];
//printf( "%d %d %d\n", p.x, p.y, G[p.x][p.y] );
for ( int i = ; i < ; ++i )
{
int xx = p.x + dx[i];
int yy = p.y + dy[i];
if ( xx >= && xx <= N+ && yy >= && yy <= M+ && !vis[xx][yy] )
{
vis[xx][yy] = true;
if ( !flag[xx][yy] ) Q.push( Point( xx, yy ) );
else sum += G[xx][yy] - ;
}
}
}
return;
} int main()
{
while ( scanf( "%d%d", &N, &M ) == )
{
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] == - ) st = Point( i, j );
} BFS();
if ( ok )
{
puts("Ali Win");
continue;
}
sum = ;
BFS2( Point(, ) );
//printf( "sum=%d\n", sum );
if ( sum % == ) puts("Baba Win");
else puts("Ali Win");
}
return ;
}
HDU 4101 Ali and Baba (思路好题)的更多相关文章
- HDU 4101 Ali and Baba
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4101 一看之下以为是博弈,后来分析才知道只是搜索题. 首先,我们需要从值为-1的位置由内向外搜索一次, ...
- HDU 2096 小明A+B --- 水题
HDU 2096 /* HDU 2096 小明A+B --- 水题 */ #include <cstdio> int main() { #ifdef _LOCAL freopen(&quo ...
- HDU 1248 寒冰王座(全然背包:入门题)
HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...
- hdu 4647 - Another Graph Game(思路题)
摘自题解: 若没有边权,则对点权从大到小排序即可.. 考虑边,将边权拆成两半加到它所关联的两个点的点权中即可. ..因为当两个人分别选择不同的点时,这一权值将互相抵消. 代码如下: #include ...
- HDU 1271 整数对(思路题)
假设删除第k位,把整数A表示成如下形式: A = a * 10^(k+1) + b * 10 ^k + c; 则: B = a * 10^k + c; N = A + B = (11*a+b)*10^ ...
- hdu 5480(维护前缀和+思路题)
Conturbatio Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 5071(2014鞍山现场赛B题,大模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 思路:模拟题,没啥可说的,移动的时候需要注意top的变化. #include <iostr ...
- HDU 4193 Non-negative Partial Sums(想法题,单调队列)
HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...
- HDU 5122 K.Bro Sorting(模拟——思维题详解)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...
随机推荐
- checkboxlist 如何配置数据源?
<f:CheckBoxList runat="server" ColumnNumber="4" ColumnVertical="true&quo ...
- jquery 表单事件
.blur() 当元素失去焦点的时候触发事件. .blur(handler(eventObject)) handler(eventObject) 每当事件触发时候执行的函数. .blur([event ...
- go get超时解决办法
go get gopkg.in/yaml.v2超时,发现被墙了,解决办法如下: 1.安装golang.org/x/net $ mkdir -p $GOPATH/src/golang.org/x/ $ ...
- JavaScript - 库 jQuery
测试 JavaScript 框架库 - jQuery 引用JQuery 如需测试JavaScript库,您需要在网页中引用它. 为了引用某个库,请使用<script>标签,其src属性设置 ...
- python正则表达式01--贪心算法和非贪心算法findall()
import re st = 'asdfasxxixxdafqewxxlovexxsadawexxyouxxas' # . #点匹配除换行符外的任意字符 a0 = re.findall('xx.',s ...
- 小白日记1:kali环境Wpscan渗透Wordpress
一.什么是Wpscan?什么是Wordpres? 1.Wpscan WPScan是一款针对wordpress的安全扫描软件:可以扫描出wordpress的版本,主题,插件,后台用户以及爆破后台用户密码 ...
- 12,nginx+uWSGI+django+virtualenv+supervisor发布web服务器
导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收请求,处理请求,响应请求) 基于wsgi运行的框架 ...
- 创建、导入、导出、复制以及粘贴 WMI 筛选器
TechNet 库 Deployment Forefront Identity and Access Management 基础结构优化 浏览器 Microsoft Dynamics Products ...
- 底部菜单栏之Fragment的详细介绍和使用方法
详情请看:http://blog.csdn.net/loongggdroid/article/details/9366413
- 《Cracking the Coding Interview》——第18章:难题——题目11
2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...