Notice
Recent Posts
Recent Comments
Link
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

BUILD_SSO

Bruteforce: ๋ธŒ๋ฃจํŠธํฌ์Šค/์™„์ „ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (+๊ตฌํ˜„) ๋ณธ๋ฌธ

Problem Solving/Algorithm

Bruteforce: ๋ธŒ๋ฃจํŠธํฌ์Šค/์™„์ „ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (+๊ตฌํ˜„)

sohyeonnn 2023. 3. 22. 15:03

๐Ÿ”ต๊ตฌํ˜„(์‹œ๋ฌผ๋ ˆ์ด์…˜๊ณผ ์™„์ „ํƒ์ƒ‰(brute force))

#๊ตฌํ˜„
๋จธ๋ฆฌ์†์—์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์†Œ์Šค์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •

- ๊ตฌํ˜„ ์œ ํ˜•์˜ ๋ฌธ์ œ๋ž€?
=> ํ’€์ด๋ฅผ ๋– ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ์€ ์‰ฝ์ง€๋งŒ ์†Œ์Šค์ฝ”๋“œ๋กœ ์˜ฎ๊ธฐ๊ธฐ ์–ด๋ ค์šด ๋ฌธ์ œ๋ฅผ ๋œปํ•จ

- ์ผ๋ฐ˜์ ์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์—์„œ์˜ 2์ฐจ์› ๊ณต๊ฐ„์€ 'ํ–‰๋ ฌ(matrix)'์˜ ์˜๋ฏธ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
=> ํ–‰๋ ฌ์—์„œ๋Š” ์™ผ์ชฝ์ƒ๋‹จ์ด (0, 0) ์ง€์ ์ด๋‹ค.

- ์‹œ๋ฌผ๋ ˆ์ด์…˜ ๋ฐ ์™„์ „ํƒ์ƒ‰ ๋ฌธ์ œ์—์„œ๋Š” 2์ฐจ์› ๊ณต๊ฐ„์—์„œ์˜ '๋ฐฉํ–ฅ ๋ฒกํ„ฐ'๊ฐ€ ์ž์ฃผ ํ™œ์šฉ๋ค๋‹ค.
=> ์„ธ๋กœ์ถ•; x(์œ„ ๋ฐฉํ–ฅ;- / ์•„๋ž˜ ๋ฐฉํ–ฅ; +), ๊ฐ€๋กœ์ถ•; y(์ขŒ; - / ์šฐ; +)
=> dx, dy = [0, 0, -1, 1], [-1, 1, 0, 0]

 

๐Ÿ”ต๋ธŒ๋ฃจํŠธํฌ์Šค(brute force)

๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์กฐํ•ฉ์„ ๋‹ค ํƒ์ƒ‰ํ•œ๋‹ค.(๋‹ต์ด ๋‚˜์˜ฌ๋•Œ๊นŒ์ง€)
-๋‹ต์„ ๋ฌด์กฐ๊ฑด ์ฐพ์„์ˆ˜๋Š” ์žˆ์œผ๋‚˜, ๋‹ต์„ ์ฐพ๋Š”๋ฐ, ๋น„์šฉ์ด ๋„ˆ๋ฌด ํฌ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Œ.
    => ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํฌ๋‹ค.

- ์ผ๋ฐ˜์ ์œผ๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n!) or O(2^n)
๋น„๊ต๋Œ€์ƒ๋งŒํผ for๋ฌธ์„ ์ž‘์„ฑํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๊ฐ€๋…์„ฑ๋„ ๋–จ์–ด์ง„๋‹ค.



์ƒํ•˜์ขŒ์šฐ

n = int(input())

plan = input().split()

x, y = 1, 1     #ํ˜„์žฌ์œ„์น˜

#๋ฐฉํ–ฅ๋ฒกํ„ฐ
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
move_type = ['L', 'R', 'U', 'D']

#๊ณ„ํš์„œ์— ์ ํ˜€์žˆ๋Š” ์ด๋™ ๊ณ„ํš์„ํ•˜๋‚˜์”ฉ ํ™•์ธ
for i in plan:

    #์ด๋™ ํ›„ ์ขŒํ‘œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.
    for j in range(len(move_type)):     #์ขŒ์šฐ์ƒํ•˜๋งŒํผ ๋ฐ˜๋ณต
        if i == move_type[j]:
            nx = x + dx[j]
            ny = y + dy[j]

    #๊ณต๊ฐ„์„ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ ๋ฌด์‹œ
    if nx < 1 or ny < 1 or nx > n or ny > n:
        continue

    #๊ณต๊ฐ„์„ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ด๋™์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
    x, y = nx, ny

print(x, y)



์‹œ๊ฐ

  โžก ์™„์ „ํƒ์ƒ‰

h = int(input())

count = 0

for i in range(h+1):    #์‹œ
    for m in range(60):     #๋ถ„
        for s in range(60):     #์ดˆ
            time = str(i) + str(m) + str(s)     #์‹œ๊ฐ
            if '3' in time:     #์‹œ๊ฐ์•ˆ์— 3์ด ํฌํ•จ๋œ๋‹ค๋ฉด,
                count += 1      #์นด์šดํŠธ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ด
print(count)



์™•์‹ค์˜ ๋‚˜์ดํŠธ

โžก ๊ตฌํ˜„, ์™„์ „ํƒ์ƒ‰
โžก *ํ–‰๋ ฌ์˜ ์‚ฌ์ด์ฆˆ, (x, y)๋ฅผ ๊ตฌํ˜„ํ•˜๋ คํ–ˆ์œผ๋‚˜ ์˜ณ์ง€๋ชปํ•œ ๋ฐฉ๋ฒ•

data = input()
row = int(data[0])
column = int(ord(data[1])-int(ord('a'))+1)

#dx, dy๋ฅผ ์‚ฌ์šฉํ•˜์ง€์•Š๊ณ  ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
night_dir = [(-2, -1), (-2, 1), (2, -1), (2, 1), (1, 2), (-1, 2), (1, -2), (-1, -2)]

count = 0

for night in night_dir:
    next_row = row + night[0]
    next_column = column + night[1]

    #8*8์งœ๋ฆฌ ํŒ ์•ˆ์— ์žˆ๋‹ค๋ฉด
    if next_row >= 1 and next_row <= 8 and next_column >= 1 and next_column <= 8:

        #๊ฒฐ๊ณผ๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
        count +=1

print(count)



๋ฌธ์ž์—ด ์žฌ์ •๋ ฌ

โžก *sort()-sorted()์ฐจ์ด์  / joinํ•จ์ˆ˜ / isalpha()๋ฉ”์„œ๋“œ

s = input()

s1 = list(s)

number = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

hap = 0     #์ˆซ์ž๋ผ๋ฆฌ ๋”ํ•ด์ฃผ๊ธฐ์œ„ํ•จ
result = []     #๋นˆ๋ฆฌ์ŠคํŠธ์ƒ์„ฑ

for i in s1:        #๋ฆฌ์ŠคํŠธ์•ˆ์˜ ์›์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•œ๋‹ค
    if i in number:     #i๊ฐ€ number์— ์žˆ๋Š” str์ด๋ผ๋ฉด,
        hap += int(i)       #hap์— intํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•œ i๋“ค์„ ๋”ํ•ด์ค€๋‹ค
    else:
        result.append(i)    #number์— ํ•ด๋‹นํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด result ๋ฆฌ์ŠคํŠธ์— ์ˆœ์„œ๋Œ€๋กœ appendํ•œ๋‹ค.
result = sorted(result)     #์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
if hap > 0:                 #๋งŒ์•ฝ hap์ด 0๋ณด๋‹ค ํฌ๋‹ค๋ฉด,(์ˆซ์ž๊ฐ€ ์กด์žฌํ–ˆ๋‹ค๋ฉด)
    result.append(str(hap)) #number์— ํ•ด๋‹นํ•˜๋Š” i๋ฅผ ๋”ํ•ด์ค€ hap์„ strํ˜•ํƒœ๋กœ appendํ•œ๋‹ค.

print("".join(result))      #๊ณต๋ฐฑ์—†์ด ๋ถ™์—ฌ์„œ ์ถœ๋ ฅ

 

#isalpha() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•œ ํ’€์ด ๋ฐฉ๋ฒ•
s = input()

s1 = list(s)

hap = 0     #์ˆซ์ž๋ผ๋ฆฌ ๋”ํ•ด์ฃผ๊ธฐ์œ„ํ•จ
result = []     #๋นˆ๋ฆฌ์ŠคํŠธ์ƒ์„ฑ

for i in s1:        #๋ฆฌ์ŠคํŠธ์•ˆ์˜ ์›์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•œ๋‹ค
    if i.isalpha():     #i๊ฐ€ alphaํ˜•ํƒœ์ธ์ง€ ํ™•์ธํ•œ๋‹ค. ๋งž๋‹ค๋ฉด,
        result.append(i)
    else:
        hap += int(i)

result = sorted(result)     #์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.


if hap > 0:                 #๋งŒ์•ฝ hap์ด 0๋ณด๋‹ค ํฌ๋‹ค๋ฉด,(์ˆซ์ž๊ฐ€ ์กด์žฌํ–ˆ๋‹ค๋ฉด)
    result.append(str(hap)) #number์— ํ•ด๋‹นํ•˜๋Š” i๋ฅผ ๋”ํ•ด์ค€ hap์„ strํ˜•ํƒœ๋กœ appendํ•œ๋‹ค.

print("".join(result))

 

*์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค 2021 ์œ ํŠœ๋ธŒ ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

Comments