徐州网络赛B-BE,GE or NE【记忆化搜索】【博弈论】
In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named "Sena" are playing a video game. The game system of this video game is quite unique: in the process of playing this game, you need to constantly face the choice, each time you choose the game will provide 1−3 options, the player can only choose one of them. Each option has an effect on a "score" parameter in the game. Some options will increase the score, some options will reduce the score, and some options will change the score to a value multiplied by −1 .
That is, if there are three options in a selection, the score will be increased by 1, decreased by 1, or multiplied by −1. The score before the selection is 8. Then selecting option 1 will make the score become 9, and selecting option 2 will make the score 7 and select option 3 to make the score −8. Note that the score has an upper limit of 100 and a lower limit of −100. If the score is 99 at this time, an option that makes the score +2 is selected. After that, the score will change to 100 and vice versa .
After all the choices have been made, the score will affect the ending of the game. If the score is greater than or equal to a certain value k, it will enter a good ending; if it is less than or equal to a certain value l, it will enter the bad ending; if both conditions are not satisfied, it will enter the normal ending. Now, Koutarou and Sena want to play the good endings and the bad endings respectively. They refused to give up each other and finally decided to use the "one person to make a choice" way to play the game, Koutarou first choose. Now assume that they all know the initial score, the impact of each option, and the k, l values, and decide to choose in the way that works best for them. (That is, they will try their best to play the ending they want. If it's impossible, they would rather normal ending than the ending their rival wants.)
Koutarou and Sena are playing very happy, but I believe you have seen through the final ending. Now give you the initial score, the k value, the l value, and the effect of each option on the score. Can you answer the final ending of the game?
Input
The first line contains four integers n,m,k,l(1≤n≤1000, −100≤m≤100 , −100≤l<k≤100), represents the number of choices, the initial score, the minimum score required to enter a good ending, and the highest score required to enter a bad ending, respectively.
Each of the next n lines contains three integers a,b,c(a≥0 , b≥0 ,c=0 or c=1),indicates the options that appear in this selection,in which a=0 means there is no option to increase the score in this selection, a>0 means there is an option in this selection to increase the score by a ; b=0 means there is no option to decrease the score in this selection, b>0 means there is an option in this selection to decrease the score by b; c=0 means there is no option to multiply the score by −1 in this selection , c=1 means there is exactly an option in this selection to multiply the score by −1. It is guaranteed that a,b,c are not equal to 0 at the same time.
Output
One line contains the final ending of the game. If it will enter a good ending,print "Good Ending"
(without quotes); if it will enter a bad ending,print "Bad Ending"
(without quotes);otherwise print "Normal Ending"
(without quotes).
样例输入1
3 -8 5 -5 3 1 1 2 0 1 0 2 1
样例输出1
Good Ending
样例输入2
3 0 10 3 0 0 1 0 10 1 0 2 1
样例输出2
Bad Ending
题目来源
题意:
两个人玩游戏 初始分值是m 进行n次操作
每次操作有三种选项 a,b,c
a不为0表示 可以选择加a;b不为0表示 可以选择减b; c不为0表示 可以选择乘-1
分值的上下界是-100和100
一个人希望分值最后大于k 一个人希望分值最后小于l 求最后分值会落在哪个范围
思路:
其实不太会记忆化搜索 本来以为就是dp 但是写了半天写不出来
其实记忆化搜索就是 dfs递归 + dp记录
dfs过程中一旦发现dp数组已经有值了就直接返回 这样对dfs进行了剪枝
用dp[id][x]表示在分值为x时进行第id次操作的结果 1表示先手必胜 0表示平手 -1表示先手必败
根据递归返回的结果 加上当前的局数来判断当前结果
1 #include <iostream> 2 #include <algorithm> 3 #include <stdio.h> 4 #include <vector> 5 #include <cmath> 6 #include <cstring> 7 #include <set> 8 #include <map> 9 10 #define inf 0x3f3f3f3f 11 using namespace std; 12 13 typedef long long LL; 14 15 int n, m, l, k; 16 const int maxn = 1005; 17 int dp[maxn][205]; 18 struct node{ 19 int a, b, c; 20 }op[maxn]; 21 22 int solve(int id, int x) 23 { 24 int win, lose, done, tmp; 25 if(dp[id][x] <= 1){ 26 return dp[id][x]; 27 } 28 if(id == n + 1){ 29 if(x >= k) return 1; 30 if(x <= l) return -1; 31 return 0; 32 } 33 34 win = lose = done = 0; 35 if(id % 2){ 36 if(op[id].a != 0){ 37 tmp = solve(id + 1, min(x + op[id].a, 200)); 38 if(tmp == 1){ 39 win = 1; 40 } 41 if(tmp == 0){ 42 done = 1; 43 } 44 if(tmp == -1){ 45 lose = 1; 46 } 47 } 48 if(op[id].b != 0){ 49 tmp = solve(id + 1, max(0, x - op[id].b)); 50 if(tmp == 1){ 51 win = 1; 52 } 53 if(tmp == 0){ 54 done = 1; 55 } 56 if(tmp == -1){ 57 lose = 1; 58 } 59 } 60 if(op[id].c != 0){ 61 tmp = solve(id + 1, 200 - x); 62 if(tmp == 1){ 63 win = 1; 64 } 65 if(tmp == 0){ 66 done = 1; 67 } 68 if(tmp == -1){ 69 lose = 1; 70 } 71 } 72 if(win == 1){ 73 return dp[id][x] = 1; 74 } 75 else if(done == 1){ 76 return dp[id][x] = 0; 77 } 78 else{ 79 return dp[id][x] = -1; 80 } 81 } 82 else{ 83 if(op[id].a != 0){ 84 tmp = solve(id + 1, min(x + op[id].a, 200)); 85 if(tmp == 1){ 86 lose = 1; 87 } 88 if(tmp == 0){ 89 done = 1; 90 } 91 if(tmp == -1){ 92 win = 1; 93 } 94 } 95 if(op[id].b != 0){ 96 tmp = solve(id + 1, max(0, x - op[id].b)); 97 if(tmp == 1){ 98 lose = 1; 99 } 100 if(tmp == 0){ 101 done = 1; 102 } 103 if(tmp == -1){ 104 win = 1; 105 } 106 } 107 if(op[id].c != 0){ 108 tmp = solve(id + 1, 200 - x); 109 if(tmp == 1){ 110 lose = 1; 111 } 112 if(tmp == 0){ 113 done = 1; 114 } 115 if(tmp == -1){ 116 win = 1; 117 } 118 } 119 if(win == 1){ 120 return dp[id][x] = -1; 121 } 122 else if(done == 1){ 123 return dp[id][x] = 0; 124 } 125 else{ 126 return dp[id][x] = 1; 127 } 128 } 129 130 } 131 132 int main() 133 { 134 while(scanf("%d%d%d%d", &n, &m, &k, &l) != EOF){ 135 m += 100; 136 l += 100; 137 k += 100; 138 for(int i = 1; i <= n; i++){ 139 scanf("%d%d%d", &op[i].a, &op[i].b, &op[i].c); 140 } 141 142 //memset(dp, 62, sizeof(dp)); 143 //cout<<dp[0]<<endl; 144 memset(dp, inf, sizeof(dp)); 145 int ans = solve(1, m); 146 if(ans == 1){ 147 printf("Good Ending\n"); 148 } 149 else if(ans == -1){ 150 printf("Bad Ending\n"); 151 } 152 else{ 153 printf("Normal Ending\n"); 154 } 155 } 156 return 0; 157 }
In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named "Sena" are playing a video game. The game system of this video game is quite unique: in the process of playing this game, you need to constantly face the choice, each time you choose the game will provide 1−3 options, the player can only choose one of them. Each option has an effect on a "score" parameter in the game. Some options will increase the score, some options will reduce the score, and some options will change the score to a value multiplied by −1 .
That is, if there are three options in a selection, the score will be increased by 1, decreased by 1, or multiplied by −1. The score before the selection is 8. Then selecting option 1 will make the score become 9, and selecting option 2 will make the score 7 and select option 3 to make the score −8. Note that the score has an upper limit of 100 and a lower limit of −100. If the score is 99 at this time, an option that makes the score +2 is selected. After that, the score will change to 100 and vice versa .
After all the choices have been made, the score will affect the ending of the game. If the score is greater than or equal to a certain value k, it will enter a good ending; if it is less than or equal to a certain value l, it will enter the bad ending; if both conditions are not satisfied, it will enter the normal ending. Now, Koutarou and Sena want to play the good endings and the bad endings respectively. They refused to give up each other and finally decided to use the "one person to make a choice" way to play the game, Koutarou first choose. Now assume that they all know the initial score, the impact of each option, and the k, l values, and decide to choose in the way that works best for them. (That is, they will try their best to play the ending they want. If it's impossible, they would rather normal ending than the ending their rival wants.)
Koutarou and Sena are playing very happy, but I believe you have seen through the final ending. Now give you the initial score, the k value, the l value, and the effect of each option on the score. Can you answer the final ending of the game?
Input
The first line contains four integers n,m,k,l(1≤n≤1000, −100≤m≤100 , −100≤l<k≤100), represents the number of choices, the initial score, the minimum score required to enter a good ending, and the highest score required to enter a bad ending, respectively.
Each of the next n lines contains three integers a,b,c(a≥0 , b≥0 ,c=0 or c=1),indicates the options that appear in this selection,in which a=0 means there is no option to increase the score in this selection, a>0 means there is an option in this selection to increase the score by a ; b=0 means there is no option to decrease the score in this selection, b>0 means there is an option in this selection to decrease the score by b; c=0 means there is no option to multiply the score by −1 in this selection , c=1 means there is exactly an option in this selection to multiply the score by −1. It is guaranteed that a,b,c are not equal to 0 at the same time.
Output
One line contains the final ending of the game. If it will enter a good ending,print "Good Ending"
(without quotes); if it will enter a bad ending,print "Bad Ending"
(without quotes);otherwise print "Normal Ending"
(without quotes).
样例输入1
3 -8 5 -5 3 1 1 2 0 1 0 2 1
样例输出1
Good Ending
样例输入2
3 0 10 3 0 0 1 0 10 1 0 2 1
样例输出2
Bad Ending