当前位置:网站首页>2022牛客多校(六)M. Z-Game on grid
2022牛客多校(六)M. Z-Game on grid
2022-08-09 11:45:00 【abcpony】
一道博弈论题目
链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
题目描述
Alice and Bob are playing a game on an n×mn\times mn×m grid where each cell has either 'A', 'B' or '.' written on it. They take turns moving a chess piece on the grid and Alice moves first.
Initially the piece is on cell (1,1)(1,1)(1,1). In each player's turn, he or she can move the piece one cell right or one cell down. That is, if the piece is on cell (x,y)(x,y)(x,y) before the turn, the player can move it to (x+1,y)(x+1,y)(x+1,y) or (x,y+1)(x,y+1)(x,y+1), as long as it doesn't go beyond the grid.
At any time, if the piece is on a cell with 'A', Alice wins and the game ends. If the piece is on a cell with 'B', Bob wins and the game ends. If the piece reaches cell (n,m)(n,m)(n,m) without the game ending, then it is a draw.
Since Alice cannot decide what acts Bob will take, she would like to know if she can be in control of the situation. Given the grid they're playing on, can you tell her if she can always find a way to win, draw or lose the game no matter what acts Bob takes?
输入描述:
In the first line an integer T (1≤T≤50)T\space (1 \le T \le 50)T (1≤T≤50), representing the number of test cases. For each test case, the first line contains two integers N,M (1≤N,M≤500)N,M\space (1\le N,M \le 500)N,M (1≤N,M≤500), representing the grid's size. Each of the next NNN lines for the case contains MMM characters (either 'A', 'B' or '.'), describing the grid.
输出描述:
For each test case, output three words 'yes' or 'no' in one line, representing if Alice can find a way to win, draw or lose the game respectively (without quotes).
示例1
输入
2 3 3 ..B ..B BB. 1 3 ...
输出
no no yes no yes no
下面这是官方题解(有少许表述不严谨的地方,无伤大雅,结合代码来看)


代码(直接拿标准题解的,但是加了一些帮助理解的注释)如下:
#include<bits/stdc++.h>
#define M 505
using namespace std;
char str[M][M];
int n,m;
char dp[M][M];
//总的来看这个问题还是要写dfs好写
int dfs(int x,int y,int f){
if(str[x][y]=='A')return 1;//遇到字母了就是要停止的了,如要赢的就是到此处可以找到1了,若是要输或者平局则是遇到0了它后面的路子不能搞了
if(str[x][y]=='B')return 4;
if(x==n&&y==m)return dp[x][y]=2;//能走到这里的就说明存在不经过字母的一条路径(还不一定就能保证平局)(上面先判断字母,再判断是否n,m)
//平局情况比较特殊,它要求走到最后(赢或输是遇到字母即可),因此任何途中的(不与右下角相邻的)'.'都不能确定为1,因此要走到最后位置再回溯确定各个'.'从而确定先手是否可以到达平局
char &t=dp[x][y];//注意这里是引用,数组可以实现记忆化搜索,本来dp数组是两层的,但是其实先手只用到f=0的,后手只用到f=1的,所以没必要双层
if(t==-1){
if(f==0){//0表先手,先手只需或
t=0;
if(x+1<=n)t|=dfs(x+1,y,f^1);//搞两层就是为了区分先后手!!!
if(y+1<=m)t|=dfs(x,y+1,f^1);
}else{//后手则和后面与,对于正常情况,就是x<n&&y<m的明显合理,对于处于边上的也是,对于x==n&&y==m的情况则已经在上面就判断了
t=7;
if(x+1<=n)t&=dfs(x+1,y,f^1);
if(y+1<=m)t&=dfs(x,y+1,f^1);
}
}
return t;
}
void solve(){
cin>>n>>m;
for(int i=1;i<=n;i++)scanf("%s",str[i]+1);
memset(dp,-1,sizeof(dp));
int t=dfs(1,1,0);
printf("%s", t&1?"yes":"no");
printf(" %s",t&2?"yes":"no");
printf(" %s\n",t&4?"yes":"no");
}
int main(){
int T;
cin>>T;
while(T--) solve();
return 0;
}边栏推荐
- 【Robustness of VQA-1】——2019-EMNLP-Don’t Take the Easy Way Out
- F280049库函数API编程、直接寄存器控制编程和混合编程方法
- buck型三相PFC
- Installation of gdb 10.2
- PAT章节
- log4net使用指南(winform版,sqlserver记录)
- _main C:/ti/ccs1011/ccs/tools/compiler/ti-cgt-c2000_20.2.1.LTS/lib/rts2800_fpu32.lib<ar在线升级跳转疑问
- 学长告诉我,大厂MySQL都是通过SSH连接的
- PAT1013 并查集 DFS(查找联通分量的个数)
- TI的片上固化好的boot ROM(上电引导加载程序)退出后的去向
猜你喜欢

问题来了:4GB物理内存的机器上申请8G内存能成功吗?

Chinese valentine's day?Programmers don't exist

mysql + redis + flask + flask-sqlalchemy + flask-session 配置及项目打包移植部署

在北京参加UI设计培训到底怎么样?

enum in c language

信息系统项目管理师必背核心考点(六十三)项目组合管理的主要过程&DIPP分析
![[现代控制理论]4_PhasePortrait爱情故事动态系统分析](/img/cd/dc1266addc58c3cd3e087f168bebf9.png)
[现代控制理论]4_PhasePortrait爱情故事动态系统分析

IDEA 关闭/开启引用提示Usages

软件测试——金融测试类面试题,看完直接去面试了

Fapi_StatusType Fapi_issueProgrammingCommand使用注意事项
随机推荐
The use of signal function (signal) in C language
Web console control edit box
字节秋招二面把我干懵了,问我SYN报文什么情况下会被丢弃?
Shell之常用小工具(sort、uniq、tr、cut)
PAT1002
结构体变量的首地址获取注意事项
从零开始Blazor Server(9)--修改Layout
【VQA survey】视觉问答中的语言学问题
MySQL中的锁
Recommend a free 50-hour AI computing platform
How tall is the B+ tree of the MySQL index?
redis缓存如何保证数据一致性
学长告诉我,大厂MySQL都是通过SSH连接的
软件测试——金融测试类面试题,看完直接去面试了
LeetCode 1413.逐步求和得到正数的最小值
buck型三相PFC
标准C语言学习总结14
【面试高频题】可逐步优化的链表高频题
[工程数学]1_特征值与特征向量
Shell正则表达式,三剑客之grep命令