Dynamic programming-polygon game

polygon_vertex=[5,-7,4,2]
polygon_side=[‘+‘,‘+‘,‘*‘,‘*‘]

multi_list = [[[0 for col in range(5)] for row in range(5)] for i in range(5)]

def get_min_of_four(val_1, val_2, val_3, val_4):
 minium = val_1
 if minium> val_2:
  minium = val_2
 if minium > val_3:
  minium = val_3
 if minium > val_4:
     minium = val_4
 return minium

def get_max_of_four(val_1, val_2, val_3, val_4):
 maximum = val_1
 if maximum < val_2:
  maximum = val_2
 if maximum < val_3:
  maximum = val_3
 if maximum < val_4:
     maximum = val_4
 return maximum

def get_min_and_max(multi_list, vertex, split, length):
 first_seg_min = multi_list[vertex][split][0]
 first_seg_max = multi_list[vertex][split][1]
 second_seg_vertex = (vertex+split)%4
 second_seg_min = multi_list[second_seg_vertex][length-split][0]
 second_seg_max = multi_list[second_seg_vertex][length-split][1]
 index = (vertex+split-1)%4
 oper = polygon_side[index]
 if oper == ‘+‘:
  return first_seg_min+ second_seg_min, first_seg_max + second_seg_max
 else:
  val_1 = first_seg_min * second_seg_min
  val_2 = first_seg_min * second_seg_max
  val_3 = first_seg_max * second_seg_min
  val_4 = first_seg_max * second_seg_max
  return get_min_of_four(val_1, val_2, val_3, val_4), get_max_of_four(val_1, val_2, val_3, val_4)

def calc_max_val(multi_list):
 for i in range(0,4):
  multi_list[i][1][0] = polygon_vertex[i]
  multi_list[i][1][1] = polygon_vertex[i]
 for length in range(2,5):
  for vertex in range(0,4):
   minimum, maximum = get_min_and_max(multi_list, vertex, 1, length)
   for split in range(2,length):
    cur_minimum, cur_maximum = get_min_and_max(multi_list, vertex, split, length)
    if cur_minimum < minimum:
     minimum = cur_minimum
    if cur_maximum > maximum:
     maximum = cur_maximum
   multi_list[vertex][length][0] = minimum
   multi_list[vertex][length][1] = maximum
   print vertex, minimum, maximum, length

calc_max_val(multi_list)

def get_maximum(multi_list):
 max_index=0
 maximum = multi_list[0][4][1]
 for i in range(0,4):
  # maximum = multi_list[i][4][1]
  print i, multi_list[i][4][1]
  if multi_list[i][4][1] > maximum:
   maximum = multi_list[i][4][1]
   max_index = i
 return maximum, max_index

print get_maximum(multi_list)

时间: 2024-10-10 14:54:14

Dynamic programming-polygon game的相关文章

Dynamic Programming

We began our study of algorithmic techniques with greedy algorithms, which in some sense form the most natural approach to algorithm design. Faced with a new computational problem, we've seen that it's not hard to propose multiple possible greedy alg

Dynamic Programming | Set 3 (Longest Increasing Subsequence)

在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Optimal Substructure Property) 中我们已经讨论了重叠子问题和最优子结构性质,现在我们来看一个可以使用动态规划来解决的问题:最长上升子序列(Longest Increasing Subsequence(LIS)). 最长上升子序列问题,致力于在一个给定的序列中找到一个最长的子序列

Dynamic Programming | Set 4 (Longest Common Subsequence)

首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", "bdf", "aeg", '"acefg"等都是"abcdefg"的子序列.因此,一个长度为n的序列拥有2^n中可能的子序列(序列中的每一个元素只有选或者不选两种可能,因此是2^n). Example: LCS for inp

HDU 4972 A simple dynamic programming problem(推理)

HDU 4972 A simple dynamic programming problem 题目链接 推理,会发现只有前一个和当前一个分数为(1, 2)或(2, 1)的时候,会有两种加分方法,其他情况最多就一种情况,所以只要统计(1, 2),(2, 1)的个数,最后判断分差是否为0,如果不为0,那么可能是正或负,那就是两倍 代码: #include <cstdio> #include <cstring> const int N = 100005; int t, n, a[N]; i

hdu 4972 A simple dynamic programming problem(高效)

题目链接:hdu 4972 A simple dynamic programming problem 题目大意:两支球队进行篮球比赛,每进一次球后更新比分牌,比分牌的计数方法是记录两队比分差的绝对值,每次进球的分可能是1,2,3分.给定比赛中的计分情况,问说最后比分有多少种情况. 解题思路:分类讨论: 相邻计分为1-2或者2-1的时候,会对应有两种的的分情况 相邻计分之差大于3或者说相等并且不等于1的话,为非法输入 其他情况下,不会造成新的比分情况产生 对于最后一次比分差为0的情况,就没有谁赢谁

2017 UESTC Training for Dynamic Programming

2017 UESTC Training for Dynamic Programming A    思维, 或 dp, 很有意思 方法1: 构造法:蛇形安排赛程表算法复杂度:O(N^2)将1-N排成两竖列,每一轮同一行的为对手保持1的位置不变,其他位置按顺(逆)时方向依次旋转1    6          1    2          1    3          1    4          1    5      2    5          3    6          4   

动态规划 Dynamic Programming

March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处. 前言 本文翻译自TopCoder上的一篇文章: Dynamic Programming: From novice to advanced ,并非严格逐字逐句翻译,其中加入了自己的

HDU-4972 A simple dynamic programming problem

http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 307    Accepted Submission(s): 117 Problem D

hdu 4972 A simple dynamic programming problem (转化 乱搞 思维题) 2014多校10

题目链接 题意:给定一个数组记录两队之间分差,只记分差,不记谁高谁低,问最终有多少种比分的可能性 分析: 类似cf的题目,比赛的时候都没想出来,简直笨到极点..... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <vector> 7 #include &

hdu 4223 Dynamic Programming?

Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solving complex problems by breaking them down