hdu 4101 - Ali and Baba(搜索)
来源:互联网 发布:array vb 编辑:程序博客网 时间:2024/05/22 03:21
看起来想博弈的题目,说白了,就是一道搜索的题目。
思路:先从里往外搜,把宝藏的保护壳标记出来,如果保护壳是开口的,那么Ali第一次就能赢得比赛, 如果是闭口的,那么第一次打开保护壳的人输,因此,我们再次从外围向里搜索,把那些保护壳外的都统计出来,正好在壳上的,就统计x-1,
开始的时候用的两遍dfs,果断的爆栈,然后换了bfs才过的,
还要注意存数据的数组要初始化的,,,
代码如下:
#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <cstdlib>#include <stack>#include <queue>#include <vector>#include <algorithm>#include <set>#include <map>#define M 310#define INF 0x7fffffff#define eps 1e-8#define LL long longusing namespace std;struct Point{ int x, y;};int s[M][M];bool vis[M][M], flag[M][M];int n, m, cnt;int dx[] = {1,0,-1,0};int dy[] = {0,1,0,-1};int bfs(int x, int y){ Point q[M*M]; int rear = 1, pop = 0, _x, _y, _xx, _yy; vis[x][y] = 1; q[0] = (Point){x,y}; while(rear>pop) { _x = q[pop].x; _y = q[pop].y; if(_x==1||_x==n||_y==1||_y==m) return 1; for(int i = 0; i < 4; ++i) { _xx = _x+dx[i]; _yy = _y+dy[i]; if(vis[_xx][_yy]==0) { vis[_xx][_yy] = 1; if(s[_xx][_yy]==0) q[rear++] = (Point){_xx, _yy}; } } ++pop; } return 0;}void bfs2(int x, int y){ Point q[M*M]; int rear = 1, pop = 0; flag[x][y] = 1; q[0] = (Point){x, y}; int _x, _y, _xx, _yy; while(rear>pop) { _x = q[pop].x; _y = q[pop].y; if(vis[_x][_y]==1) { cnt+=(s[_x][_y]-1); ++pop; continue; } else cnt+=s[_x][_y]; for(int i = 0; i < 4; ++i) { _xx = _x+dx[i]; _yy = _y+dy[i]; if(_xx>=0&&_xx<=n+1&&_yy>=0&&_yy<=m+1&&flag[_xx][_yy]==0) { flag[_xx][_yy] = 1; q[rear++] = (Point){_xx, _yy}; } } ++pop; }}int main(){ int x, y; while(~scanf("%d%d", &n, &m)) { memset(s,0,sizeof(s)); for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) { scanf("%d", &s[i][j]); if(s[i][j]==-1) x = i, y = j; } cnt = 0; vis[x][y] = 1; memset(vis, 0,sizeof(vis)); memset(flag,0,sizeof(flag)); if(bfs(x, y)) printf("Ali Win\n"); else { bfs2(0,0); if(cnt&1) printf("Ali Win\n"); else printf("Baba Win\n"); } } return 0;}
- hdu 4101 - Ali and Baba(搜索)
- HDU:4101 Ali and Baba(搜索+博弈)
- HDU 4101 Ali and Baba
- HDU 4101 Ali and Baba
- HDU 4101 Ali and Baba
- HDU 4101 Ali and Baba
- BFS-hdu-4101-Ali and Baba
- hdu 4101 Ali and Baba(bfs,dfs)
- HDU 4101Ali and Baba (bfs)
- hdu 4101 Ali and Baba (bfs+严密思维)
- HDU 4101 Ali and Baba 博弈, 连通块
- hdu 4101 Ali and Baba【BFS好题】
- [题解] HDU 4101 Ali and Baba (BFS)
- !HDU 4101 Ali and Baba-博弈-(bfs&dfs扫描二维点)
- HDU4101:Ali and Baba(BFS)
- HDU4101 Ali and Baba (bfs+dfs+博弈)
- Ali and Snoopy(利用广度搜索走迷宫)
- Ali
- 轻松学习RSA加密算法原理
- 18款在线代码片段测试工具
- java值传递
- 关于GRE的思考
- android面试题之五
- hdu 4101 - Ali and Baba(搜索)
- Java多线程程序设计
- 在AWK中输出特殊字符-单引号
- struts2中一个很实用的标签
- 数据结构与算法分析:第一章:Finding the kth largest number and The four basic rules of recursion
- C#_delegate - 用委托实现事件,Display和Log类都使用Clock对象
- (void) (&_x == &_y)
- Ruby中,&:(ampersand-colon,"与号-冒号")的用法
- C#:System.Array简单使用