[dp+Game] Winning Strategy of Chess Board

Keywords: PHP Mac iOS

Links: https://ac.nowcoder.com/acm/problem/21797
Source: Niuke.com

Time limit: C/C++ 1 second, 2 seconds for other languages
Space limitations: C/C++ 32768K, other languages 65536K
64bit IO Format: %lld

Title Description

There is a two-dimensional chessboard with rows r and columns c. Each position on the board has the following four situations
'E': means export, there may be more than one
'T': There is only one, representing the starting point
'#': Indicates an obstacle
'.': denotes open space

Niu Niu and Niu Mei play a game on such a board. They have a card with an integer k written on it. It was first placed at the starting point. Now Niu and Niu Mei take turns and Niu Niu Mei operate first.
The currently operated bull will choose one of the directions up, down, left or right to move the card. Each step, the number on the card is subtracted by 1
You can only walk to the open space, or to the exit. When you walk to the exit, the game will end. When the number of cards turns to zero, the game will end. The cattle that cannot move any more will lose the game.

If Niu Niu and Niu Mei both use the best strategy, ask who will win

Enter a description:

The first line inputs three integers r,c,k
Next r lines read k characters per line to represent the board

1 ≤ r,c ≤ 50, 1 ≤ k ≤ 100

Output description:

Output "niuniu" if bulls have a winning strategy
Otherwise output "niumei"
Example 1

input

copy
2 3 3
T.#
#.E

output

copy
niuniu
Example 2

input

copy
3 3 99
E#E
#T#
E#E

output

copy
niumei
Example 3

input

copy
4 5 13
#E...
#...E
E.T#.
..#..

output

copy
niuniu

Remarks:

Subtask 1:mac(r,c) <= 10
Subtask 2:max(r,c) <= 20
Subtask 3: Unlimited

Title: A two-dimensional chessboard in column c of row r with obstacles # and open space'.', multiple exits''E'and a starting point''T'. Starting from the starting point, you can walk k steps. If k becomes 0, you can turn the k of the card to 0. If you can reach any end E in k steps or k steps, you can move the card to the end point, which wins the bull.Now Niu Niu is the first to ask if Niu Niu can win
Idea: If you can reach a winning state from the start, you must win first
Now start searching around the starting point
Look at this point first If the last bull moved the card to the current point and made the k of the card zero or reached the end point, the bull now loses and returns a zero
If this point does not deliver the above conditions, then start from this point and look at the points around it. If you can walk in the board with the remaining steps, and you can go to one point to make the next cow lose, then this point is the point where the cow will win. Otherwise, if you start from this point, you cannot walk to the next point to make the next cow win.The point at which you lose, then this is the point at which you can make this steak

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=1e2+1;
 4 char mp[51][51];
 5 int dp[51][51][amn],r,c,k,dic[5][3]={{0,1},{0,-1},{1,0},{-1,0}},dx,dy,si,sj;
 6 int dfs(int x,int y,int k){
 7     if(k<=0||mp[x][y]=='E')return dp[x][y][k]=0;///If the last bull moved the card to the current point to make the card k To zero or to the end, the cow is now lost and returns a zero
 8     if(dp[x][y][k]!=-1)return dp[x][y][k];      ///If this point has been passed, return to this state      
 9     for(int i=0;i<4;i++){
10         dx=x+dic[i][0];
11         dy=y+dic[i][1];
12         if(dx>=1&&dx<=r&&dy>=1&&dy<=c&&mp[dx][dy]!='#'&&k>0&&!dfs(dx,dy,k-1))    ///If you start from this point, can walk in the board with the remaining steps, and can go to one point to make the next bull lose, then this is the point that will make the bull win
13             return dp[x][y][k]=1;
14     }
15     return dp[x][y][k]=0;                       ///If you start from this point and cannot go to the next point to make the next steak, then this point is the point where the steak can be made
16 }
17 int main(){
18     ios::sync_with_stdio(0);
19     cin>>r>>c>>k;
20     for(int i=1;i<=r;i++){
21         for(int j=1;j<=c;j++){
22             cin>>mp[i][j];
23             if(mp[i][j]=='T'){si=i,sj=j;}
24         }
25     }
26     memset(dp,-1,sizeof dp);
27     if(dfs(si,sj,k)==1)printf("niuniu\n");  ///If you can reach a winning state from the start, you must win first
28     else printf("niumei\n");
29 }
30 /***
31 A two-dimensional chessboard in column c of row r, with obstacles'#'and open space'.', multiple exits''E' and a starting point''T'. You can walk k from the starting point. If k changes to 0, the bull who turns k to 0 wins. If you reach any end point E in k or k steps, the bull who moves the card to the end point wins. NowNiu Niu Begins by Asking if Niu Niu can win
32 If you can reach a winning state from the start, you must win first
33 Now start searching around the starting point
34 Look at this point first If the last bull moved the card to the current point and made the k of the card zero or reached the end point, the bull now loses and returns a zero
35 If this point does not deliver the above conditions, then start from this point and look at the points around it. If you can walk in the board with the remaining steps, and you can go to one point to make the next cow lose, then this point is the point where the cow will win. Otherwise, if you start from this point, you cannot walk to the next point to make the next cow win.The point at which you lose, then this is the point at which you can make this steak
36 ***/

 

Posted by Chris16962 on Sat, 03 Aug 2019 19:57:56 -0700