当前位置:网站首页>2022 Niu Ke Duo School (6) M. Z-Game on grid
2022 Niu Ke Duo School (6) M. Z-Game on grid
2022-08-09 12:15:00 【abcpony】
A game theory topic
Link: Login—Professional IT Written TestInterview preparation platform_Niuke.com
Source: Niuke.com
Title description
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 thegrid 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 orone 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 controlof 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?
Enter description:
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.
Output description:
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).
Example 1
Enter
23 3..B..BBB.1 3...
Output
no no yesno yes no
The following is the official solution to the problem (there are some inaccurate expressions, it is harmless, combined with the code)
The code (solved directly from the standard problem, but with some comments to help understanding) is as follows:
#include#define M 505using namespace std;char str[M][M];int n,m;char dp[M][M];//In general, this problem still needs to be written in dfsint dfs(int x,int y,int f){if(str[x][y]=='A')return 1;//If you encounter a letter, you will stop. If you want to win, you can find 1 here. If you want to lose or draw, it isWhen you encounter 0, the path behind it can't be done.if(str[x][y]=='B')return 4;if(x==n&&y==m)return dp[x][y]=2;//If you can get here, it means that there is a path that does not pass through letters (it may not guarantee a draw) (the above is judged firstletters, and then determine whether n,m)//The case of a draw is special, it requires to go to the end (win or lose by encountering letters), so any '.' on the way (not adjacent to the lower right corner) cannot be determined to be 1, so it is necessary to go toThe last position is backtracked to determine each '.' to determine whether the first mover can reach a drawchar &t=dp[x][y];//Note that this is a reference. The array can realize memory search. Originally, the dp array has two layers, but in fact, only f=0 is used in the first hand, and only f is used in the second hand.=1, so there is no need for double layersif(t==-1){if(f==0){//0 means the first move, the first move only needs ort=0;if(x+1<=n)t|=dfs(x+1,y,f^1);//The purpose of doing two layers is to distinguish between the hands!!!if(y+1<=m)t|=dfs(x,y+1,f^1);}else{//The back hand and the back and, for the normal situation, it is obviously reasonable for x>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;}
边栏推荐
猜你喜欢
Ways to prevent data fraud
redis库没法引入
【Data augmentation in NLP】——1
Semaphore SIGCHLD use, how to make the parent that the child performs over, how to make the distinction between multiple child processes. The end
Shell之常用小工具(sort、uniq、tr、cut)
电解电容漏电流及均压
[现代控制理论]4_PhasePortrait爱情故事动态系统分析
虚拟机安装出现的问题汇总
LeetCode热题(11.合并两个有序链表)
Fapi_StatusType Fapi_issueProgrammingCommand使用注意事项
随机推荐
标准C语言学习总结14
ClickHouse物化视图(八)
redis的线程模型
proto3-2 syntax
The use of gdb tui
[Interview high-frequency questions] Linked list high-frequency questions that can be gradually optimized
web course design
[C language] creation and use of dynamic arrays
matlab simulink的scope 示波器光标如何移动记录
索引index
[Essence] Analysis of the special case of C language structure: structure pointer / basic data type pointer, pointing to other structures
IDEA 关闭/开启引用提示Usages
Shell之常用小工具(sort、uniq、tr、cut)
ACM longest non-descent subsequence problem
Programmer's Exclusive Romance - Use 3D Engine to Realize Fireworks in 5 Minutes
fidder为什么不会抓包的问题
【VQA survey】视觉问答中的语言学问题
PAT1010
buck型三相PFC
This application has no explicit mapping for /error, so you are seeing this as a fallback