ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

9184๋ฒˆ: ์‹ ๋‚˜๋Š” ํ•จ์ˆ˜ ์‹คํ–‰

์ž…๋ ฅ์€ ์„ธ ์ •์ˆ˜ a, b, c๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ž…๋ ฅ์˜ ๋งˆ์ง€๋ง‰์€ -1 -1 -1๋กœ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์„ธ ์ •์ˆ˜๊ฐ€ ๋ชจ๋‘ -1์ธ ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์˜ ๋งˆ์ง€๋ง‰์„ ์ œ์™ธํ•˜๋ฉด ์—†๋‹ค.

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/9184
package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	dp [][][]int
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	for {
		var a, b, c int
		fmt.Fscanln(reader, &a, &b, &c)
		if a == -1 && b == -1 && c == -1 {
			break
		}
		dp = make([][][]int, 21)
		for i := 0; i < 21; i++ {
			dp[i] = make([][]int, 21)
			for j := 0; j < 21; j++ {
				dp[i][j] = make([]int, 21)
			}
		}
		fmt.Fprintf(writer, "w(%d, %d, %d) = %d\n", a, b, c, w(a, b, c))
	}
}

func w(a, b, c int) int {
	if a <= 0 || b <= 0 || c <= 0 {
		return 1
	}
	if a > 20 || b > 20 || c > 20 {
		return w(20, 20, 20)
	}
	if dp[a][b][c] != 0 {
		return dp[a][b][c]
	}
	if a < b && b < c {
		dp[a][b][c] = w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
	} else {
		dp[a][b][c] = w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
	}
	return dp[a][b][c]
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/9184
import sys

dp = [[[0 for i in range(21)] for j in range(21)] for k in range(21)]

def w(a, b, c):
    if a <= 0 or b <= 0 or c <= 0:
        return 1
    if a > 20 or b > 20 or c > 20:
        return w(20, 20, 20)
    if dp[a][b][c] != 0:
        return dp[a][b][c]
    if a < b and b < c:
        dp[a][b][c] = w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
    else:
        dp[a][b][c] = w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
    return dp[a][b][c]

if __name__ == "__main__":
    while True:
        a, b, c = list(map(int, sys.stdin.readline().split()))
        if a == -1 and b == -1 and c == -1:
            break
        print("w({}, {}, {}) = {}".format(a, b, c, w(a, b, c)))
728x90
๋Œ“๊ธ€