uva 11311 EXCLUSIVELY EDIBLE (DP)
来源:互联网 发布:淘宝购物评级怎么看 编辑:程序博客网 时间:2024/06/01 21:06
Problem E
EXCLUSIVELY EDIBLE
Hansel and Gretel like cakes, but especially the so called �grid cake" served in Wolfgang Puck's restaurants. It is made ofmn pieces of different cakes, resembling a 2D m-by-n grid when looked at from above (hence the name).
The only thing that Hansel and Gretel do not like about grid cakes is that each of them has to contain a piece of the Scrumptious Caramel Topping cake. (Image of a three-by-four grid cake with brown Scrumptious Caramel Topping cake is shown) It turns out that the only reason Wolfgang Puck has the Scrumptious Caramel Topping cake in his recipe book is because he inherited it from his late great-great-grandmother.
Neither Hansel nor Gretel want to have the "bad" piece in their portion of the cake, so they came up with the following way to decide who gets the bad piece: first Hansel cuts a piece of the cake along the grid lines, then Gretel does the same and they keep alternating until there is only the Scrumptious Caramel Topping cake piece left and one of them is forced to take it.
For example, with a two-by-three grid cake, the illustrations below show the following steps:
A sequence of cuts to determine whether Hansel or Gretel is getting the bad piece.
Hansel and Gretel have eaten many grid cakes together and have played this game so many times that they know who will take the bad piece before starting. In fact, if they observe Hansel will take the bad piece, then Gretel knows a strategy to ensure Hansel takes the bad piece. Hansel also knows this strategy.
Given the original cake and position of the Scrumptious Caramel Topping cake piece in the grid, who will take the bad piece?
Input
The first line of the input file contains a number t (1 ≤ t ≤ 100), the number of test cases. Thent lines follow, each containing m n r c (separated by spaces) wherem and n (2 ≤ m, n ≤ 48) are the width and the length of the cake and(r,c) is the zero-based position of the Scrumptious Caramel Topping cake piece in the grid cake (0 ≤r ≤ m-1, 0 ≤ c ≤ n-1).
Output
For each test case print the name of the person that gets the bad piece assuming that Hansel makes the first cut and that Hansel and Gretel always cut the cake at an optimal location (trying not to get the Scrumptious Caramel Topping cake piece). Note that "cut" here refers to a straight line cut (along a grid line) that separates the cake into two pieces.
Sample Input
22 3 0 211 11 5 5
Output for the Sample Input
GretelHansel
#include <iostream>#include <cstdio>using namespace std;#define ll intconst int maxn = 50;ll dp[2][maxn][maxn][maxn][maxn];int m , n , r , c;void initial(){for(int i = 0;i < maxn;i++){for(int j = 0;j < maxn;j++){for(int k = 0;k < maxn;k++){for(int l = 0;l < maxn;l++){dp[0][i][j][k][l] = -1;dp[1][i][j][k][l] = -1;}}}}}int DP(int n1 , int n2 , int n3 , int n4 , int k){if(n1 == 0 && n2 == 0 && n3 == 0 && n4 == 0) return !k;if(dp[k][n1][n2][n3][n4] != -1) return dp[k][n1][n2][n3][n4];if(n1 != 0){for(int i = 1;i <= n1;i++){if(DP(n1-i , n2 , n3 , n4 , !k) == k){return dp[k][n1][n2][n3][n4] = k;}}}if(n2 != 0){for(int i = 1;i <= n2;i++){if(DP(n1 , n2-i , n3 , n4 , !k) == k){return dp[k][n1][n2][n3][n4] = k;}}}if(n3 != 0){for(int i = 1;i <= n3;i++){if(DP(n1 , n2 , n3-i , n4 , !k) == k){return dp[k][n1][n2][n3][n4] = k;}}}if(n4 != 0){for(int i = 1;i <= n4;i++){if(DP(n1 , n2 , n3 , n4-i , !k) == k){return dp[k][n1][n2][n3][n4] = k;}}}return dp[k][n1][n2][n3][n4] = !k;}void computing(){scanf("%d%d%d%d" , &n , &m , &r , &c);int n1 = r , n2 = n-r-1 , n3 = c , n4 = m-c-1;int ans = DP(n1 , n2 , n3 , n4, 1);if(ans){printf("Gretel\n");}else{printf("Hansel\n");}}int main(){initial();int t;cin >> t;while(t--){computing();}return 0;}
- uva 11311 EXCLUSIVELY EDIBLE (DP)
- Edible
- UVA DP 入门专题
- uva 1424 简单DP
- uva 10192 DP
- UVA dp题目汇总
- UVa 4256 - Salesmen dp
- UVa 10271 - Chopsticks dp
- UVA 1424 - Salesmen (dp)
- uva 672 Gangsters( dp )
- UVA 10271 Chopsticks(dp)
- uva 10271 Chopsticks(dp)
- UVA 11584 DP水题
- UVA 1424 DP水题
- UVA 10534 DP+LIS
- UVA 11552 DP水题
- UVA 1456 DP水题
- Uva 624 背包DP
- HDU 1016(dfs)
- 最全的 Twitter Bootstrap 开发资源清单
- RadioGroup用法
- 电脑知识大全之电脑内存常见问题处理方法
- js 读取url 参数
- uva 11311 EXCLUSIVELY EDIBLE (DP)
- 电脑知识大全之如何使用qq截图
- 建立顺序表
- 修改beacon帧结构(2)帧的接收与帧信息提取过程【Linux内核-OpenWRT】
- CUDA程序优化小记(四)
- 电脑知识大全之批量清除WPS/Word文档中的回车符
- 设置solr/home的三种方式
- 反编译那些事儿(一)
- KVC/KVO原理详解及编程指南