๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿบ ๋งค์ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

Google Code Jam 2020 Qualification Round ํ›„๊ธฐ

์ง€๋‚œ ์ผ์š”์ผ์— 27์‹œ๊ฐ„๋™์•ˆ ๊ตฌ๊ธ€์ฝ”๋“œ์žผ 2020 Qualification Round๊ฐ€ ์žˆ์—ˆ๊ณ  ํ†ต๊ณผํ–ˆ๋‹ค. ์ ์ˆ˜์ฒด๊ณ„๋„ ๋ชจ๋ฅด๊ณ  ์ฐธ๊ฐ€ํ•˜๋ฉด ๋‹ค ํ†ต๊ณผํ•˜๋Š” ์—ฐ์Šต๋ผ์šด๋“œ์ด๊ฑฐ๋‹ˆํ•˜๊ณ  ์ฐธ๊ฐ€ํ–ˆ์—ˆ๋Š”๋ฐ Qualification Round์—์„œ ์ด 30์  ์ด์ƒ ๋ฐ›์•„์•ผํ•œ๋‹ค๋Š” ๊ทœ์น™์ด ์žˆ์—ˆ๋‹ค. ๊ทธ๋ž˜๋„ 5๋ฌธ์ œ ์ค‘ ํ’€๋งŒํ•œ ๋‚œ์ด๋„์˜ ๋ฌธ์ œ 3๊ฐœ๋งŒ ํ’€๋ฉด ํ†ต๊ณผ ๊ฐ€๋Šฅํ•œ ์ ์ˆ˜๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ  ์‹œ๊ฐ„์ œํ•œ๋„ ๋„‰๋„‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ถฉ๋ถ„ํžˆ ์‹œ๊ฐ„์„ ํˆฌ์žํ•˜๊ณ  ์ฐธ๊ฐ€ํ–ˆ๋‹ค๋ฉด ๊ฑฐ์ง„ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

Qualification Round์—์„œ ํ‘ผ ๋ฌธ์ œ๋“ค์„ ๊ฐ„๋žตํ•˜๊ฒŒ ์ •๋ฆฌํ•ด๋ณด๊ฒ ๋‹ค.

1. Vestigium

๊ฐ ํ–‰๊ณผ ์—ด์— ์ค‘๋ณต๋œ ์ˆซ์ž์—†์ด 1~N์‚ฌ์ด์˜ ์›์†Œ๊ฐ€ ํ•œ๋ฒˆ์”ฉ ๋“ฑ์žฅํ•˜๋Š” N x N ์ •์‚ฌ๊ฐํ˜•์„ Latin Square๋ผ๊ณ  ํ•œ๋‹ค. ์ฃผ์–ด์ง„ ์ •์‚ฌ๊ฐํ˜•์ด Latin Square๊ฐ€ ๋งž๋Š”์ง€ ํ™•์ธํ•˜๋ผ.

 

์ž…๋ ฅ๊ฐ’:

  • ์ฃผ์–ด์ง„ N x N ์‚ฌ์ด์ฆˆ์˜ ์ •์‚ฌ๊ฐํ˜•

์ถœ๋ ฅ๊ฐ’:

  • k = trace = ์™ผ์ชฝ ์œ„์—์„œ ์‹œ์ž‘ํ•ด์„œ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜์—์„œ ๋๋‚˜๋Š” ์ค‘์•™ ๋Œ€๊ฐ์„ ์— ์œ„์น˜ํ•œ ๊ฐ’๋“ค์˜ ํ•ฉ
  • r = ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ์กด์žฌํ•˜๋Š” ํ–‰์˜ ๊ฐฏ์ˆ˜
  • c = ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ์กด์žฌํ•˜๋Š” ์—ด์˜ ๊ฐฏ์ˆ˜

๋ฌธ์ œํ’€์ด:

  • ๊ฐ ํ–‰๊ณผ ์—ด์— ์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ๋Š”์ง€๋Š” Hash Set์œผ๋กœ ํ™•์ธ
  • arr[0][0], arr[1][1]๊ณผ ๊ฐ™์ด ํ–‰ ์ธ๋ฑ์Šค์™€ ์—ด ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ™์„ ๋•Œ๊ฐ€ trace๊ฐ’์„ ๊ตฌ์„ฑํ•˜๋Š” ๋Œ€๊ฐ์„ ์— ํฌํ•จ๋œ ์œ„์น˜์ด๋‹ค.
T = int(input())

for t in range(1, T + 1):
    N = int(input())
    board = []
    for n in range(1, N + 1):
        row = input().split(' ')
        board.append([int(x) for x in row])
    k = 0
    r = 0
    c = 0
    checkCol = [{'.'} for _ in range(0, N)]
    for i in range(0, N):
        checkRow = {'.'}
        for j in range(0, N):
            if i == j:
                k += board[i][j]
            checkRow.add(board[i][j])
            checkCol[j].add(board[i][j])
            if i == N - 1 and len(checkCol[j]) != N + 1:
                c += 1
        if len(checkRow) != N + 1:
            r += 1
    print("Case #{}: {} {} {}".format(t, k, r, c))

 

Node.js ํ™˜๊ฒฝ์—์„œ ์ž…์ถœ๋ ฅ์„ ๋ฐ›๋Š” ๊ฒƒ์„ ์ง์ ‘ ์งœ๋ณด๋Š”๊ฒŒ ๋‚ฏ์„ค์–ด์„œ ๋Œ€ํšŒ๋•Œ ํ”ผํ–ˆ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ๋„ ๋‹ค์‹œ ํ’€์–ด๋ดค๋‹ค. JS๋ฅผ ๋” ์—ฐ์Šตํ•  ํ•„์š”๊ฐ€ ์žˆ์–ด์„œ JS๋กœ ํ‘ธ๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„ ์•„์‰ฝ์ง€๋งŒ, ํŒŒ์ด์ฌ ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฒˆ ์ฝ”๋“œ์žผ์€ ํŒŒ์ด์ฌ์œผ๋กœ ํ’€์–ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค. ๋ณ„๊ฐœ๋กœ ์•„๋ž˜ ์ฝ”๋“œ์— ์“ฐ์ธ JS๋กœ ๋ชจ๋“ˆ importํ•˜๋Š” ๋ฒ•, ์ž…์ถœ๋ ฅ ๋ชจ๋“ˆ์ธ readline, async await, IIFE๋Š” ์ด์ฐธ์— ํ•œ ๋ฒˆ ์ •๋ฆฌํ•ด๋ด์•ผ๊ฒ ๋‹ค. (์ •๋ฆฌํ•˜๋ฉด ๋งํฌ ์ถ”๊ฐ€ํ•  ์˜ˆ์ •)

 

const solution = board => {
    let k = 0, r = 0, c = 0;
    const cols = [];
    for (let i = 0; i < board.length; i++) {
        const rows = new Set();
        for (let j = 0; j < board.length; j++) {
            if (i === 0) {
                cols.push(new Set());
            }
            if (i === j) {
                k += board[i][j];
            }
            rows.add(board[i][j]);
            cols[j].add(board[i][j]);
            if (i + 1 === board.length && cols[j].size !== board.length) {
                c++;
            }
        }
        if (rows.size !== board.length) {
            r++;
        }
    }
    return [k, r, c];
}

const { createInterface } = require('readline');

(async () => {
    const rl = createInterface({
        input: process.stdin,
        output: process.stdout
    });
    
    let T = 0, t = 1, N = 0, board = [];
    
    rl.on('line', line => {
        if (T === 0) {
            T = parseInt(line);
            return;
        } else {
            if (N === 0) {
                N = parseInt(line);
                board = []
                return;
            } else {
                board.push(line.split(' ').map(e => parseInt(e)));
                if (board.length === N) {
                    const [k, r, c] = solution(board);
                    console.log(`Case #${t++}: ${k} ${r} ${c}`);
                    N = 0;
                }
            }
        }
        
    })

    await rl.close;
})();

 

2. Nesting Depth

์ž…๋ ฅ๊ฐ’:

  • "010" "123"๊ณผ ๊ฐ™์ด 1-9์‚ฌ์ด์˜ ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด S
  • 1 <= S.length <= 100

์ถœ๋ ฅ๊ฐ’:

  • ์ž…๋ ฅ๊ฐ’์— ์ตœ์†Œํ•œ์˜ ๊ด„ํ˜ธ๋ฅผ ์‚ฝ์ž…ํ•œ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅ.
  • ๊ฐ ์ˆซ์ž๋Š” ๊ทธ ์ˆซ์ž์˜ ํฌ๊ธฐ๋งŒํผ์˜ ๊ด„ํ˜ธ์— ๊ฐ์‹ธ์ ธ์•ผํ•œ๋‹ค.
  • 1์€ (1) 1 depth๋งŒํผ 2๋Š” ((2)) 2 depth ๋งŒํผ.
  • 12000์˜ ๊ฒฝ์šฐ (1(2))000 ์ด๋Ÿฐ์‹์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฌธ์ œํ’€์ด:

  • ํ˜„์žฌ ์ˆซ์ž๋ณด๋‹ค ์ด์ „ ์ˆซ์ž๋ณด๋‹ค ์ž‘์œผ๋ฉด ํ˜„์žฌ ์ˆซ์ž ์ง์ „์— ๋‘ ์ˆ˜์˜ ์ฐจ์ด๋งŒํผ ๊ด„ํ˜ธ๋ฅผ ๋‹ซ์•„์ค˜์•ผํ•œ๋‹ค.
  • ํ˜„์žฌ ์ˆซ์ž๋ณด๋‹ค ์ด์ „ ์ˆซ์ž๊ฐ€ ํฌ๋ฉด ํ˜„์žฌ ์ˆซ์ž ์ง์ „์— ๋‘ ์ˆ˜์˜ ์ฐจ์ด๋งŒํผ ๊ด„ํ˜ธ๋ฅผ ์—ด์–ด์ค˜์•ผํ•œ๋‹ค.
  • ๋งˆ์ง€๋ง‰๊ฐ’์ด 0์ด ์•„๋‹ˆ๋ผ๋ฉด ์ž…๋ ฅ๊ฐ’์„ ๋‹ค ์ˆœํšŒํ•œ ํ›„์— ์•„์ง ๋‹ซํžˆ์ง€ ์•Š์€ ๊ด„ํ˜ธ๊ฐ€ ์žˆ๋‹ค. ์ด๋ฅผ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผํ•œ๋‹ค. 
T = int(input())
for t in range(1, T + 1):
    str1 = input()
    res = []
    isOpen = 0
    for i in range(0, len(str1)):
        x = int(str1[i])
        if isOpen < x:
            res.append('(' * (x - isOpen))
        if x < isOpen:
            res.append(')' * (isOpen - x))
        isOpen = x
        res.append(str1[i])
    res.append(')' * isOpen)
    print("Case #{}: {}".format(t, ''.join(res)))

 

3. Parenting Partnering

์ž…๋ ฅ๊ฐ’:

  • n๊ฐœ์˜ ์ผ์ •๋“ค์ด [์‹œ์ž‘์‹œ๊ฐ„(int), ๋์‹œ๊ฐ„(int)] ๋‹ด๊ธด ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง„๋‹ค.
  • ์ผ์ •๋“ค์€ ์ •๋ ฌ๋˜์ง€ ์•Š์•˜๋‹ค.

์ถœ๋ ฅ๊ฐ’:

  • Cameron๊ณผ Jamie๊ฐ€ ์œ„ ์ผ์ •๋“ค์„ ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ์ง€ ๋ถˆ๊ฐ€๋Šฅํ•œ์ง€ ํ”„๋ฆฐํŠธ.
  • ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด Cameron์˜ C๋ฅผ Jamie์˜ J๋ฅผ ๋”ฐ์„œ ์ฃผ์–ด์ง„ ์ผ์ •๋“ค์— ๋”ฐ๋ผ ๋‚˜์—ดํ•œ ๋ฌธ์ž์—ด์„ ํ”„๋ฆฐํŠธ. 

๋ฌธ์ œํ’€์ด:

  • ์šฐ์„  ์ผ์ •๋“ค์„ ์‹œ์ž‘์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•œ๋‹ค. ์ด ๋•Œ ๋‚˜์ค‘์— ๋ณธ๋ž˜์˜ ์ˆœ์„œ๋กœ ๋‹ค์‹œ ๋ณต๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์ธ๋ฑ์Šค๋ฅผ ํ•จ๊ป˜ ์ €์žฅํ•ด์ค€๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ ์“ฐ์ธ ์ •๋ ฌ๋œ ์ผ์ •์€ [์‹œ์ž‘์‹œ๊ฐ„, ๋์‹œ๊ฐ„,  ์ธ๋ฑ์Šค] ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
  • C ๋˜๋Š” J์˜ ์ด์ „ ์ผ์ • ๋์‹œ๊ฐ„์ด ํ˜„์žฌ ์ผ์ •์˜ ์‹œ์ž‘์‹œ๊ฐ„์— ๋๋‚˜๊ฑฐ๋‚˜ ๋” ๋นจ๋ฆฌ ๋๋‚ฌ๋‹ค๋ฉด ํ˜„์žฌ ์ผ์ •์„ ์ƒˆ๋กœ ํ• ๋‹น ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
  • ๋‘˜ ๋‹ค ์•„์ง ์ด์ „ ์ผ์ •์ด ์•ˆ ๋๋‚ฌ๋‹ค๋ฉด ์ด๋Š” ๋ถˆ๊ฐ€๋Šฅํ•œ ์Šค์ผ€์ฅด์ด๋‹ค. 
def solution(sched, t):
    isJ = 0
    isC = 0
    res = []
    for i in range(N):
        if isJ and isJ <= sched[i][0]:
            isJ = 0
        if isC and isC <= sched[i][0]:
            isC = 0
        if isJ == 0:
            res.append(['J', sched[i][2]])
            isJ = sched[i][1]
            continue
        if isC == 0:
            res.append(['C', sched[i][2]])
            isC = sched[i][1]
            continue
        print("Case #{}: {}".format(t, 'IMPOSSIBLE'))
        break
    else:
        res = sorted(res, key = lambda x : x[1])
        print("Case #{}: {}".format(t, ''.join([r[0] for r in res])))



T = int(input())

for t in range(1, T + 1):
    N = int(input())
    sched = []
    for n in range(N):
        cur = [int(x) for x in input().split(' ')]
        cur.append(n)
        sched.append(cur)
    solution(sorted(sched, key = lambda x : x[0]), t)

    

 

ํƒ์š•๋ฒ•์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ์œ ๋ช…ํ•œ ๋ฌธ์ œ(2 worker scheduling)๋ผ๊ณ  ํ•œ๋‹ค. 

 

 

4. ESAb ATAd

์ด ๋ฌธ์ œ๋Š” ์•ˆ ํ’€๊ณ  ๋„˜์–ด๊ฐ”๋‹ค. ๋ณ€๋ช…์„ ํ•˜์ž๋ฉด ๋‹น์ผ์— ๋‹ค๋ฅธ ์‹œํ—˜๋„ ์žˆ์—ˆ๊ณ , ๊ทธ ์‹œํ—˜ ๋๋‚˜๊ณ  ๋‹ค์‹œ ์ฝ”๋“œ์žผ์„ ํ’€๋Ÿฌ์™”๋Š”๋ฐ ์ธํ„ฐ๋ž™ํ‹ฐ๋ธŒ ๋ฌธ์ œ์œ ํ˜•์„ ํŒŒ์•…ํ•˜๊ณ , ํ…Œ์ŠคํŒ… ํˆด๋„ ๋‹ค์šด๋ฐ›๊ณ  ์ดํ•ดํ•˜๊ณ  5๋ฌธ์ œ ์ค‘ ๊ฐ€์žฅ ๊ธด ์ง€๋ฌธ์„ ์ฝ๊ณ  ๋ฌธ์ œ ํŒŒ์•…ํ•ด์„œ ํ•ด๊ฒฐ ํ•  ์—„๋‘๊ฐ€ ์•ˆ๋‚ฌ๋‹ค. ํ•œํŽธ์œผ๋ก  ๋‚ด๊ฐ€ ๋…ํ•ด๊ฐ€ ํ•„์š”ํ•œ ๋ฌธ์ œ์— ์•ฝํ•œ ๊ฒƒ ๊ฐ™๋‹ค๋Š”๊ฑธ ์ƒˆ์‚ผ ๋‹ค์‹œ ๋Š๊ผˆ๋‹ค. ์˜๋ฌธ ๋…ํ•ด๊ฐ€ ๋ฌธ์ œ๊ฐ€ ์•„๋‹ˆ๋ผ ํ•œ๊ตญ์–ด๋กœ ๋œ ๋ฌธ์ œ๋„ ์“ธ๋ฐ์—†๋Š” ๋ง๊ณผ ์ค‘์š”ํ•œ ๋ง๋“ค์ด ์„ž์—ฌ์žˆ๋Š” ๋ฌธ์ œ๋“ค์„ ์ดํ•ดํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ํŽธ์ธ ๊ฒƒ ๊ฐ™๋‹ค. ํ‰์†Œ์—๋„ ์š”์ ์„ ๊ฐ„๋‹จํžˆ ๋“ฃ๋Š”๊ฑธ ์„ ํ˜ธํ•˜๊ณ  ์žฅํ™ฉํ•œ ๋‚ด์šฉ์˜ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ์—ฐ์Šต ์•ˆ ํ•œ ํƒ“๋„ ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ์˜ ์ถœ์ œ์˜๋„ ์ž์ฒด๊ฐ€ ์ƒ๋Œ€๊ฐ€ ๋‚œํ•ดํ•˜๊ณ  ํšก์„ค์ˆ˜์„คํ•œ ์„ค๋ช…์„ ๋“ฃ๊ณ  ๋น ๋ฅด๊ฒŒ ์š”์ ์„ ํŒŒ์•…ํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š๋ƒ๋Š” ์‹์˜ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ๋Šฅ๋ ฅ์„ ๋ณด๋ ค๋Š”๊ฑด๊ฐ€ ์‹ถ๊ธฐ๋„ํ•˜๋‹ค. ๊ทธ๋Ÿฐ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ๋Šฅ๋ ฅ์€ ์ค‘์š”ํ•˜๋‹ˆ๊นŒ ์—ฐ์Šตํ•ด์•ผ๊ฒ ๋‹ค. ๐Ÿ˜”

 

๋Œ€ํšŒ๊ฐ€ ๋๋‚œ ํ›„ ๋‹ค์‹œ ์‚ดํŽด๋ณด๊ณ  ์žˆ๋Š”๋ฐ ์—ฌ์ „ํžˆ ์–ด๋ ต๊ฒŒ ๋Š๊ปด์ง„๋‹ค. ์ด๋ฒˆ์ฃผ ์•ˆ์— ์ดํ•ด๋ฅผ ๋งˆ์น˜๊ณ  ํ’€์ด๋ฅผ ์—…๋ฐ์ดํŠธํ•ด๋ณผ ์˜ˆ์ •์ด๋‹ค. 

 

 

5. Indicium

1๋ฒˆ ๋ฌธ์ œ Vestigium์—์„œ ๋‚˜์˜จ ์ฃผ์ œ์ธ Latin Square์— ๊ด€ํ•œ ๋ฌธ์ œ์ด๋‹ค. ์—ฐ๊ณ„๋œ ๋ฌธ์ œ์ธ๊ฐ€ ์‹ถ์–ด ์žฌ๋ฐŒ์–ด๋ณด์—ฌ์„œ ํ’€๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. Latin Square๋Š” 1~N์˜ ์ˆซ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋˜๊ณ  ๊ฐ ํ–‰์™€ ์—ด์— ์ค‘๋ณต๋œ ๊ฐ’์ด ์—†๋Š” N x N ์ •์‚ฌ๊ฐํ˜•์„ ๋งํ•œ๋‹ค.

 

์ž…๋ ฅ๊ฐ’:

  • N: ์ •์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ
  • K: ์™ผ์ชฝ ์œ„ - ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ๋Œ€๊ฐ์„ ์˜ ํ•ฉ์ธ trace ๊ฐ’

์ถœ๋ ฅ๊ฐ’:

  • trace์˜ ๊ฐ’์œผ๋กœ K๋ฅผ ๊ฐ–์€ N x N Latin Square์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ์ง€ ๋ถˆ๊ฐ€๋Šฅํ•œ์ง€ ์ถœ๋ ฅ
  • ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ํ•ด๋‹นํ•˜๋Š” Latin Square๋ฅผ ๋งŒ๋“ค์–ด ์ถœ๋ ฅ

Test set 1 (Visible Verdict) :

  • T = 44
  • 2 ≤ N ≤ 5

Test set 2 (Hidden Verdict):

  • 1 ≤ T ≤ 100
  • 2 ≤ N ≤ 50.

 

๋ฌธ์ œํ’€์ด:

 

์ฒ˜์Œ์—๋Š” ์˜ˆ์ œ๋ฅผ ์†์œผ๋กœ ์ ์–ด๋ณด๋ฉฐ ์ผ์ •ํ•œ ๊ทœ์น™์„ ์ฐพ์•„๋ณด๊ณ  ์ ์šฉํ•ด๋ณด์•˜๋‹ค๊ฐ€ ์‹คํŒจํ–ˆ๋‹ค. 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ๊ฐ™์€ ํ–‰์„ ํ•˜๋‚˜์”ฉ shift & push ํ•ด์„œ board๋ฅผ ๋งŒ๋“ค๊ณ , ํ–‰์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ๋ณด๋“œ์˜ trace๊ฐ’๊ณผ ๊ทธ ๋ณด๋“œ๋ฅผ 180๋„ ๋Œ๋ ธ์„ ๋–„ trace๊ฐ’์„ ํ™•์ธํ•ด ๋ณด๋Š” ๋ฐฉ์‹์ด์—ˆ๋‹ค.

 

์†์œผ๋กœ ๊ทœ์น™์„ ์ฐพ๋Š”๊ฒŒ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹จ ์ƒ๊ฐ์ด๋“ค์–ด ๋ฐฉ๋ฒ•์„ ๋ฐ”๊พธ์—ˆ๋‹ค. 1~N๊ฐ’์˜ ํ•ฉ์œผ๋กœ K๋ฅผ ๋งŒ๋“ค์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์กฐํ•ฉ์„ ๋ฐฑํŠธ๋ž˜ํ‚น์œผ๋กœ ๊ตฌํ•˜๊ณ , ๋˜ ๋ฐฑํŠธ๋ž˜ํ‚น์œผ๋กœ board์— ๋นˆ์นธ๋“ค์„ ์ฑ„์›Œ๋‚˜๊ฐ”๋‹ค. ์ด ๋ฐฉ์‹์œผ๋กœ Test set 1์€ ํ†ต๊ณผํ–ˆ๋Š”๋ฐ Test set 2๋ฅผ ์‹คํŒจํ–ˆ๋‹ค. N์ด ์กฐ๊ธˆ ์ปค์ง€๋ฉด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ผํ‹ด์Šคํ€˜์–ด์˜ ์ˆ˜๊ฐ€ ๊ธฐํ•˜๊ธ‰์ˆ˜์ ์œผ๋กœ ์ปค์ ธ์„œ ๋‚ด๊ฐ€ ์ ‘๊ทผํ•œ ๋ฐฉ์‹์œผ๋กœ๋Š” ํ’€ ์ˆ˜ ์—†๋Š” ๊ฒƒ ๊ฐ™๋‹ค. (N = 11์˜๊ฒฝ์šฐ 776966836171770144107444346734230682311065600000์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋ผํ‹ด์Šคํ€˜์–ด๊ฐ€ ์žˆ๋‹ค๊ณ ํ•œ๋‹ค)

 

Test Case2๋„ ํ†ต๊ณผํ•˜๋Š” ์ฝ”๋“œ๋กœ ๊ณ ์ณ์„œ ์—…๋ฐ์ดํŠธ ํ•  ์˜ˆ์ •์ด๋‹ค.

def getBoard(line):
    
    isSloved = [False]
    board = []
    rows = []
    cols = []
    
    for i in range(N):
        board.append([])
        rows.append(set())
        rows[i].add(line[i])
        for j in range(N):
            if i == j:
                board[i].append(line[i])
                cols.append(set())
                cols[i].add(line[i])
            else:
                board[i].append(0)
    
    def isAvailable(r, c, v):
        if v in rows[r] or v in cols[c]:
            return False
        return True
    
    def place(r, c, v):
        board[r][c] = v
        rows[r].add(v)
        cols[c].add(v)
    
    def remove(r, c, v):
        board[r][c] = 0
        rows[r].remove(v)
        cols[c].remove(v)
    
    def go(r, c):
        if isSloved[0]:
            return
        if r + 1 == N and c + 1 == N:
            isSloved[0] = True
        else:
            if c + 1 == N:
                backtrack(r + 1, 0)
            else:
                backtrack(r, c + 1)
    
    def backtrack(r, c):
        if board[r][c] == 0:
            for x in range(1, N + 1):
                if isAvailable(r, c, x):
                    place(r, c, x)
                    go(r, c)
                    if not isSloved[0]:
                        remove(r, c, x)
        else:
            go(r, c)
    
    backtrack(0, 0)
    return [isSloved[0], board]
    

    
def find(n, k, nums):
    if len(nums) == n:
        return getBoard(nums) if sum(nums) == k else [False, []]
    for x in range(1, n + 1):
        nums.append(x)
        res = find(n, k, nums)
        if res[0]:
            return res 
        nums.pop()
    return [False, []]

T = int(input())

for t in range(1, T + 1):
    N, K = [int(x) for x in input().split(' ')]
    res = find(N, K, [])
    if res[0]:
        print("Case #{}: POSSIBLE".format(t))
        for i in range(N):
            print(" ".join([str(x) for x in res[1][i]]))
    else:
        print("Case #{}: IMPOSSIBLE".format(t))

 

 

 

์ดํ›„์—๋Š” 1๋ผ์šด๋“œ, 2๋ผ์šด๋“œ, 3๋ผ์šด๋“œ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ง„ํ–‰๋˜๋ฉฐ ์ผ์ • ๋“ฑ์ˆ˜ ์ด์ƒ์„ ๋ฐ›์•„์•ผ ๊ทธ ๋‹ค์Œ ๋ผ์šด๋“œ๋กœ ์ง„์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. 1๋ผ์šด๋“œ์˜ ๊ฒฝ์šฐ 3๊ฐœ์˜ sub round (1A๋ผ์šด๋“œ, 1B๋ผ์šด๋“œ, 1C๋ผ์šด๋“œ) ์ค‘ ํ•œ ๋ฒˆ๋งŒ ์ผ์ • ๋“ฑ์ˆ˜๋ฅผ ๋ฐ›์œผ๋ฉด ๋œ๋‹ค. 

  • 1๋ผ์šด๋“œ 1500๋“ฑ ์ด๋‚ด (1500๋“ฑ*3๋ฒˆ ์ด๋ผ ๋Œ€๋žต 4500๋ช…)
  • 2๋ผ์šด๋“œ 1000๋“ฑ ์ด๋‚ด
  • 3๋ผ์šด๋“œ 25๋“ฑ ์ด๋‚ด

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰ ๋ผ์šด๋“œ์ธ Final Round๋Š” ํ•œ ๋ฒˆ ์ง„ํ–‰๋˜๋ฉฐ ๊ตฌ๊ธ€ ์บ ํผ์Šค์—์„œ ์ง„ํ–‰๋˜์–ด์™”๋‹ค. ์˜ฌํ•ด๋Š” ๋…์ผ Munich ๊ตฌ๊ธ€ ์˜คํ”ผ์Šค์—์„œ ์ง„ํ–‰๋  ์˜ˆ์ •์ด๋ผ๊ณ  ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ฝ”๋กœ๋‚˜ ์—ฌํŒŒ๋กœ ์–ด๋–ป๊ฒŒ ๋ ์ง€ ์ง€์ผœ๋ด์•ผ ์•Œ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

๋“ฑ์ˆ˜์— ๋”ฐ๋ผ ๋ถ€์ƒ๋„ ์žˆ๋‹ค.

  • 1๋“ฑ $15,000 USD
  • 2๋“ฑ $2,000 USD
  • 3๋“ฑ $1,000 USD
  • 4~25๋“ฑ $100 USD
  • 26~1000๋“ฑ ํ‹ฐ์…”์ธ 

Qualification ๋ผ์šด๋“œ์— ์ฐธ๊ฐ€ํ•  ๋•Œ ๋น ๋ฅด๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด๋‚˜๊ฐ€๋Š” ํƒ‘๋žญํฌ ์ฐธ๊ฐ€์ž๋“ค์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ํ‹ฐ์…”์ธ ๋ฅผ ๋ฐ›๋Š” ์ •๋„๋งŒ๋˜๋„ ์—„์ฒญ ์‹ค๋ ฅ์žˆ๋Š”๊ฑฐ๋ผ ์ƒ๊ฐํ•œ๋‹ค. ๋‚œ 2๋ผ์šด๋“œ์— ์ง„์ถœํ•˜๋Š”๊ฑธ ๋ชฉํ‘œ๋กœ ์žก์•„๋ณด๋ ค๊ณ ํ•œ๋‹ค.