题解 | 24点游戏算法

## 这题好难,先糊弄过去

package main

import (
	"fmt"
)

func cal(p0, p1, op float64) float64 {
	switch op {
	case 0:
		return p0 + p1
	case 1:
		return p0 - p1
	case 2:
		return p0 * p1
	case 3:
		return p0 / p1
	}

	return -1
}

func main() {
	var p0, p1, p2, p3 float64
	_, _ = fmt.Scan(&p0, &p1, &p2, &p3)
	// var p0, p1, p2, p3 float64 = 3, 9, 3, 4

	for _, params := range permute([]float64{p0, p1, p2, p3}, 0, 3) {
		for op0 := 0.0; op0 < 4; op0++ {
			for op1 := 0.0; op1 < 4; op1++ {
				for op2 := 0.0; op2 < 4; op2++ {
					// A (B C) D
					{
						result0 := cal(params[1], params[2], op0)
						result1 := cal(params[0], result0, op1)
						result := cal(result1, params[3], op2)

						if 24-result < 0.0001 && 24-result > -0.0001 {
							// fmt.Printf("params: %#v, ops: %v,%v,%v\n", params, op0, op1, op2)
							fmt.Println("true")
							return
						}
					}

					// A B (C D)
					{
						result0 := cal(params[0], params[1], op1)
						result1 := cal(params[2], params[3], op0)
						result := cal(result0, result1, op2)

						if 24-result < 0.0001 && 24-result > -0.0001 {
							// fmt.Printf("params: %#v, ops: %v,%v,%v\n", params, op0, op1, op2)
							fmt.Println("true")
							return
						}
					}

					// A B (C D)
					{
						result0 := cal(params[1], params[2], op0)
						result1 := cal(result0, params[3], op1)
						result := cal(params[0], result1, op2)

						if 24-result < 0.0001 && 24-result > -0.0001 {
							// fmt.Printf("params: %#v, ops: %v,%v,%v\n", params, op0, op1, op2)
							fmt.Println("true")
							return
						}
					}

					// A (B C D)
					{
						result0 := cal(params[1], params[2], op0)
						result1 := cal(result0, params[3], op1)
						result := cal(params[0], result1, op2)

						if 24-result < 0.0001 && 24-result > -0.0001 {
							// fmt.Printf("params: %#v, ops: %v,%v,%v\n", params, op0, op1, op2)
							fmt.Println("true")
							return
						}
					}

					// 6 / (1 - (3 / 4)) = 24
					{
						result0 := cal(params[2], params[3], op0)
						result1 := cal(params[1], result0, op1)
						result := cal(params[0], result1, op2)

						if 24-result < 0.0001 && 24-result > -0.0001 {
							// fmt.Printf("params: %#v, ops: %v,%v,%v\n", params, op0, op1, op2)
							fmt.Println("true")
							return
						}
					}
				}
			}
		}
	}

	fmt.Println("false")
}

func swap(arr []float64, i, j int) {
	tmp := arr[i]
	arr[i] = arr[j]
	arr[j] = tmp
}

func permute(params []float64, startIdx, endIdx int) [][]float64 {
	if startIdx == endIdx {
		// fmt.Printf("equal: %#v, i: %v\n", params, startIdx)
		return [][]float64{params}
	}

	result := [][]float64{}
	for i := startIdx; i <= endIdx; i++ {
		// fmt.Printf("params0: %#v, i: %v\n", params, i)
		swap(params, startIdx, i)
		copyedParams := append([]float64{}, params...)
		// fmt.Printf("params1: %#v\n", params)
		result = append(result, permute(copyedParams, startIdx+1, endIdx)...)
		swap(params, startIdx, i)
		// fmt.Printf("params2: %#v\n", params)
	}

	// fmt.Printf("result: %#v, i: %v\n", result, startIdx)

	return result
}

全部评论

相关推荐

12-25 17:57
Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务