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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

1248๋ฒˆ: ๋งž์ถฐ๋ด

๋ฌธ์ œ ๊ทœํ˜„์ด๋Š” ๋ฉ์ฒญํ•˜๋‹ค. ์™œ๋ƒํ•˜๋ฉด, 1~10๊นŒ์ง€ ์ˆ˜ ๋ฐ–์— ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์–ด๋Š ๋‚  ๊ทœํ˜„์ด ์˜†์„ ์ง€๋‚˜๊ฐ€๋˜ ํƒœ์„์ด๊ฐ€ ๊ทœํ˜„์ด๋ฅผ ๋ณด๊ณ  ์ด๋ ‡๊ฒŒ ์™ธ์ณค๋‹ค. "๋นต๋นต!!" ๊ทœํ˜„์ด๋Š” "์•„ํ•˜!" ํ•˜๋ฉด์„œ ์„ธ์ƒ์—๋Š” ๋นต์ด๋ž€

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 -10 ~ 10๋ฅผ ์„ ํƒํ•˜๋ฉด, ์„ ํƒ๋œ ์ˆซ์ž๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ S ๋ฐฐ์—ด์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š”๋‹ค. ํ•ด๋‹น ๋ฐฐ์—ด ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด, ๊ทธ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ˆœ์—ด ๋“ค๊ณผ ๋น„๊ตํ•˜์—ฌ ๋งŒ์กฑํ•˜๋Š” ์ˆœ์—ด์„ ์ฐพ์œผ๋ฉด ๋œ๋‹ค. ๋ฌธ์ œ์˜ ์˜ˆ์ œ ์ž…๋ ฅ์˜ S๋Š” -+0++++--+๋กœ ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

 

  ์ฒซ ํ–‰์˜ ์ •๋ณด(i == 0)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

  • j == 0
    • (0, 0) < 0
  • j == 1
    • (0, 0) + (0, 1) > 0
  • j == 2
    • (0, 0) + (0, 1) + (0, 2) == 0
  • j == 3
    • (0, 0) + (0, 1) + (0, 2) + (0, 3) > 0

  ๋‘ ๋ฒˆ์งธ ํ–‰์˜ ์ •๋ณด๋Š” i == 1 ๋ถ€ํ„ฐ ๊ฐ ์ž๋ฆฌ ์ˆซ์ž์˜ ํ•ฉ์ด + + + ์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋งŒ์กฑํ•˜๋Š” ์ˆœ์—ด์„ ์ž…๋ ฅ๊ฐ’์„ ํ†ตํ•ด ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค. ๋˜ํ•œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋‹ต์„ ์ฐพ์„ ๊ฒฝ์šฐ ์•„๋ฌด๊ฑฐ๋‚˜ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋˜๋ฏ€๋กœ, ํ•˜๋‚˜๋ผ๋„ ์ฐพ๊ฒŒ ๋œ๋‹ค๋ฉด ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ข…๋ฃŒํ•˜๋ฉด ๋œ๋‹ค.

 

์ฝ”๋“œ

def available(idx):
    sum_num = 0
    for i in range(idx, -1, -1):
        sum_num += check[i]
        if s[i][idx] == '+' and sum_num <= 0:
            return False
        if s[i][idx] == '-' and sum_num >= 0:
            return False
        if s[i][idx] == '0' and sum_num != 0:
            return False
    return True


def dfs(depth):
    if depth == n:
        print(*check)
        exit(0)
    for i in range(-10, 11):
        check[depth] = i
        if available(depth):
            dfs(depth + 1)
        check[depth] = -11


if __name__ == "__main__":
    n = int(input())
    s = [[0] * n for _ in range(n)]
    input_s = list(input())
    check, k = [-11] * n, 0
    for i in range(n):
        for j in range(i, n):
            s[i][j] = input_s[k]
            k += 1
    dfs(0)

 Python3๋กœ ์ œ์ถœํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ PyPy3๋กœ ์ œ์ถœํ•˜์—ฌ ํ†ต๊ณผํ•˜์˜€๋‹ค. ๋‹ค๋ฅธ ์–ธ์–ด๋Š” ํ•ด๋‹น ๋กœ์ง์œผ๋กœ ํ†ต๊ณผํ•˜๋Š” ๊ฒƒ์„ ๋ณด์•„.. ํŒŒ์ด์ฌ์ด ๋Š๋ฆฌ๋‹ค๋Š” ์ž๊ธฐ ํ•ฉ๋ฆฌํ™”๋ฅผ ํ–ˆ๋‹ค...๐Ÿ˜‚

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€