Checkerboard winning strategy (Game + search)

link:

https://ac.nowcoder.com/acm/problem/21797
Source: Cattle-off network

Title Description

There is a two-dimensional chessboard, board has r rows c columns, checkerboard each location has the following four cases
'E': denotes an outlet, there may be multiple
'T': there is only one representing the starting point
'#': that obstacle
'': that space

beef and cattle sister play games on such a board, they have a written integer k cards, initially placed in the position of the starting point, now beef and cattle sister began to take turns operating, beef first operation
cow current operation will choose one of the vertical and horizontal direction card, every step, subtract one number on the card
can only come to the open space, or walked exports went to exports, the game is over, the card number becomes zero when the game will end, and can no longer move the cows will lose the game

if both beef and cattle sister with the best strategy, may I ask who will win

Enter a description:

The first line of the input three integers r, c, k 
next line reads r rows each character represents the board k

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

Output Description:

If there is a winning strategy beef output "niuniu" Otherwise, output "niumei"

Specific ideas:

Game + Search, violence gets the job done (this problem too can join in the sample 83)

AC Code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 # define ll long long
 4 # define inf 0x3f3f3f3f
 5 # define LL_inf (1ll<<60)
 6 const int maxn = 200;
 7 char str[55][55];
 8 int r,c,k;
 9 struct node
10 {
11     int x,y,step;
12     node() {}
13     node(int xx,int yy,int zz)
14     {
15         x=xx,y=yy,step=zz;
16     }
17 };
18 int f[2][4]= {{1,-1,0,0},{0,0,1,-1}};
19 bool judge(int x,int y)
20 {
21     return x>=1&&x<=r&&y>=1! && Y <= C && STR [X] [Y] = ' # ' ;
 22 is  }
 23 is  int DP [ 55 ] [ 55 ] [MAXN];
 24  BOOL DFS ( int ST, int ED, int K)
 25  {
 26 is      IF (STR [ST] [ED] == ' E ' ) return DP [ST] [ED] [K] = 0 ; // if is 'E' when the flip is losing time for the state, the upper hand is necessary to wins state
 27      IF (K <= 0 )
 28          return  0 ;
 29      IF ! (DP [ST] [ED] [K] = - . 1)
30         return dp[st][ed][k];
31     for(int i=0; i<4; i++)
32     {
33         int tx=st+f[0][i];
34         int ty=ed+f[1][i];
35         if(judge(tx,ty)&&k-1>=0)
36         {
37             if(dfs(tx,ty,k-1)==0)
38                 return  dp[st][ed][k]=1;
39         }
40     }
41     return dp[st][ed][k]=0;
42 }
43 int main()
44 {
45     memset(dp,-1,sizeof(dp));
46     int st,ed;
47     scanf("%d %d %d",&r,&c,&k);
48     for(int i=1; i<=r; i++)
49     {
50         scanf("%s",str[i]+1);
51         for(int j=1; j<=c; j++)
52         {
53             if(str[i][j]=='T')
54             {
55                 st=i,ed=j;
56             }
57         }
58     }
59     int ans=dfs(st,ed,k);
60     printf("%s\n",ans==1?"niuniu":"niumei");
61     return 0;
62 }

 

Guess you like

Origin www.cnblogs.com/letlifestop/p/10980721.html