技巧
dp
思路
之前用了贪心的做法是不对的;存在后效性。。。
正确做法
dp[ x1] [ y1 ] [ x2 ] [ y2] 表示第一个人走到(x1, y1)同时第二个人走到(x2,y2)所获得的最大值
到达dp[ x1] [ y1 ] [ x2 ] [ y2]有4中情况
dp[x1 - 1][y1][x2 - 1][y2] dp[x1][y1 - 1][x2][y2 - 1] dp[x1 - 1][y1][x2][y2 - 1] dp[x1][y1 - 1][x2 - 1][y2]
如果 x1 = x2 & y1 = y2 说明这两人走到了同一个点 只能加一次格子上的值 否则可以+两次
实现
package main import ( . "fmt" "os" "io" "bufio" ) func NC16759(_r io.Reader, _w io.Writer) { in, out := bufio.NewReader(_r), bufio.NewWriter(_w) defer out.Flush() var n int Fscan(in, &n) grid := [11][11]int{} for { var a,b,c int Fscan(in, &a, &b, &c) if a == 0 && b ==0 && c == 0 { break } grid[a][b] = c } dp := [11][11][11][11]int{} for x1 := 1; x1 <= n; x1++ { for y1 := 1; y1 <= n; y1++ { for x2 := 1; x2 <= n; x2++ { for y2 := 1; y2 <= n; y2++ { max := Max(dp[x1 - 1][y1][x2 - 1][y2],dp[x1][y1 - 1][x2][y2 - 1], dp[x1 - 1][y1][x2][y2 - 1],dp[x1][y1 - 1][x2 - 1][y2]) if x1 == x2 && y1 == y2 { dp[x1][y1][x2][y2] = max + grid[x1][y1] }else { dp[x1][y1][x2][y2] = max + grid[x1][y1] + grid[x2][y2] } } } } } Fprintln(out, dp[n][n][n][n]) } func Max(nums ... int) int { max := -0x3f for i := 0; i < len(nums); i++ { if nums[i] > max { max = nums[i] } } return max } func main() { NC16759(os.Stdin, os.Stdout) }