Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

31-gjsk132 #151

Merged
merged 1 commit into from
May 1, 2024
Merged

31-gjsk132 #151

merged 1 commit into from
May 1, 2024

Conversation

gjsk132
Copy link
Member

@gjsk132 gjsk132 commented Mar 29, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์ €๋ฒˆ ๋ถ„ํ•  ์ •๋ณต ๋ณต์Šต ๊ฒธ ์ถ”์ฒœํ•ด์ฃผ์‹  ๋ฌธ์ œ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

๋ฐฑ์ค€ 1074 : Z

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

40๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๐ŸŽฏ ๋ฌธ์ œ ์ดํ•ด

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜• : ๋ถ„ํ•  ์ •๋ณต ( Divide and Conquer )

๋ฌธ์ œ ์š”์•ฝ
์•„๋ž˜ ์ด๋ฏธ์ง€๋ฅผ ํ†ตํ•ด ๋ฐฉ๋ฌธ ์ˆœ์„œ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ( Z ๋ชจ์–‘ )
์ฃผ์–ด์ง„ ์œ„์น˜๊ฐ€ ๋ช‡ ๋ฒˆ์งธ์— ๋ฐฉ๋ฌธํ•˜๋Š”์ง€๋ฅผ ์•Œ๋ฉด ๋˜๋Š” ๋ฌธ์ œ

๋ฌธ์ œ์— ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๊ธฐ๋งŒ ํ•ด๋„ ๋ถ„ํ•  ์ •๋ณต์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

ํ•œ ์ •์‚ฌ๊ฐํ˜•์„ 4๊ฐœ์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋ถ„๋ฆฌํ•ด์„œ Z ๋ชจ์–‘์œผ๋กœ ๋ฐฉ๋ฌธ

์žฌ๊ท€๋ฅผ ํ™œ์šฉํ•˜์—ฌ 4์‚ฌ๋ถ„๋ฉด์„ ๋ฐ˜๋ณตํ•ด์„œ ํƒ์ƒ‰ํ•ด์ค€๋‹ค.

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์ด 1๊ฐœ 1๊ฐœ ๋‹ค ํƒ์ƒ‰ํ•˜๊ฒŒ ๋˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค. ๐Ÿ˜ž

๊ทธ๋Ÿผ ์›ํ•˜๋Š” ์œ„์น˜๊ฐ€ ์žˆ๋Š” ๋ถ€๋ถ„๋งŒ ๊ณ ๋ฅด๋ฉด ์–ด๋–จ๊นŒ?
image

์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 4๋ถ„ํ• ํ•ด์„œ ์œ„์น˜๊ฐ€ ํฌํ•จ๋˜๋Š” ๋ถ€๋ถ„์„ ํ•œ๋ฒˆ ๋” ํƒ์ƒ‰ํ•ด์ฃผ๊ณ , ์ด์ „์— ํƒ์ƒ‰ํ•œ ๋ฒ”์œ„์˜ ๋ฉด์ ๋งŒ ๋”ํ•ด์ฃผ๋Š” ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด ๊ตณ์ด ํ•˜๋‚˜ํ•˜๋‚˜ ๋“ค์–ด๊ฐ€์ง€ ์•Š์•„๋„ ๋” ์ ์€ ์‹œ๊ฐ„์— ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

๐Ÿ”์ฝ”๋“œ ์„ค๋ช…

n : 2^n ์‚ฌ์ด์ฆˆ์˜ 2์ฐจ์› ๋ฐฐ์—ด์„ ์˜๋ฏธ
row, col : ์›ํ•˜๋Š” ๊ฐ’์˜ ์œ„์น˜
z_setting : z ๋ชจ์–‘์œผ๋กœ ์œ„์น˜๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ํ•จ์ˆ˜
cnt : ์›ํ•˜๋Š” ๊ฐ’์˜ ์œ„์น˜๋ฅผ ๋ช‡ ๋ฒˆ์งธ์— ํƒ์ƒ‰ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜


์ฃผ์–ด์ง„ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

n, row, col = map(int,input().split())

ํ•ต์‹ฌ ์ฝ”๋“œ
z_setting์€ size๋ฅผ 4๋ถ„ํ• ํ•˜์—ฌ z๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ, 4๋ฐฉ๋ฉด์—์„œ ์–ด๋””์— ์œ„์น˜ํ•˜๋Š”์ง€๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
์ด๋ฅผ ์žฌ๊ท€๋กœ ๋ฐ˜๋ณตํ•˜๋ฉด size๊ฐ€ 0์ด ๋  ๋•Œ๊นŒ์ง€ ํƒ์ƒ‰ํ•˜๊ฒŒ ๋˜๊ณ , ์ด์ „์— ๋ฐฉ๋ฌธํ•œ ์œ„์น˜์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์œ„์น˜๊ฐ’์„ ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

def z_setting(r, c, size):
    cnt = 0
    
    if size == 0:
        return 0

    ns = size//2

    if r <= row < r+ns and c <= col < c+ns:
        cnt += z_setting(r, c, ns)
    elif c <= col < c+ns:
        cnt += z_setting(r+ns, c, ns) + ns**2 * 2
    elif r <= row < r+ns:
        cnt += z_setting(r, c+ns, ns) + ns**2
    else:
        cnt += z_setting(r+ns, c+ns, ns) + ns**2 * 3
        
    return cnt

์ด ํ•จ์ˆ˜์˜ base case(๋ฉˆ์ถ”๋Š” ์‹œ์ )๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
size๊ฐ€ ๋” ์ด์ƒ ๋ถ„ํ• ํ•  ์ˆ˜ ์—†๋Š” 0์ด ๋œ๋‹ค๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.

cnt = 0

if size == 0:
    return 0

4๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, 2์‚ฌ๋ถ„๋ฉด > 1์‚ฌ๋ถ„๋ฉด > 3์‚ฌ๋ถ„๋ฉด > 4์‚ฌ๋ถ„๋ฉด ์ˆœ์„œ๋Œ€๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์›ํ•˜๋Š” ์œ„์น˜๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

ns = size//2

if r <= row < r+ns and c <= col < c+ns:
    cnt += z_setting(r, c, ns)
elif c <= col < c+ns:
    cnt += z_setting(r+ns, c, ns) + ns**2 * 2
elif r <= row < r+ns:
    cnt += z_setting(r, c+ns, ns) + ns**2
else:
    cnt += z_setting(r+ns, c+ns, ns) + ns**2 * 3

๋‹ค์Œ ์‚ฌ๋ถ„๋ฉด์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ๊ทธ ๋ฉด์ ๋งŒํผ ์ถ”๊ฐ€๊ฐ€ ๋˜์–ด์•ผํ•œ๋‹ค๋Š” ์ ์„ ์ƒ๊ฐํ•ด์„œ cnt์˜ ๊ฐ’์„ ๋”ํ•ด์ค€๋‹ค.
๊ฐ ์‚ฌ๋ถ„๋ฉด ์ค‘์—์„œ ์›ํ•˜๋Š” ์œ„์น˜๊ฐ€ ์žˆ๋Š” ์‚ฌ๋ถ„๋ฉด์€ z_setting์„ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ๊ฐ’์„ ๋”ํ•ด์ค€๋‹ค.

์ด๋ ‡๊ฒŒ ๊ตฌํ•ด์ง„ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

print(z_setting(0,0,2**n))
์ „์ฒด ์ฝ”๋“œ
input = open(0).readline

n, row, col = map(int,input().split())

def z_setting(r, c, size):
    cnt = 0
    
    if size == 0:
        return 0

    ns = size//2

    if r <= row < r+ns and c <= col < c+ns:
        cnt += z_setting(r, c, ns)
    elif c <= col < c+ns:
        cnt += z_setting(r+ns, c, ns) + ns**2 * 2
    elif r <= row < r+ns:
        cnt += z_setting(r, c+ns, ns) + ns**2
    else:
        cnt += z_setting(r+ns, c+ns, ns) + ns**2 * 3
        
    return cnt

print(z_setting(0,0,2**n))

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์‚ด๋ ค์คŒ๋ฉ”

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ œ ๊ฒฝ์šฐ์—๋Š” ํ•จ์ˆ˜ ์ž์ฒด์—์„œ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ , ์ „์—ญ์œผ๋กœ ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋‘ฌ์„œ ๊ธฐ๋กํ•˜๊ฒŒ ํ–ˆ์Šต๋‹ˆ๋‹ค.
์ถ”๊ฐ€๋กœ, ์ƒ๊ฐํ•ด๋ณด๋ฉด base case๋ฅผ ํ•˜๋‚˜ ๋” ๋‘˜ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์ขŒํ‘œ (x, y)๊ฐ€ ์ง€๊ธˆ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฒ”์œ„์— ์†ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํ•ด๋‹น ์‚ฌ๋ถ„๋ฉด ํฌ๊ธฐ๋ฅผ ์ฆ‰์‹œ returnํ•ด๋„ ๋˜๊ฒ ์ฃ ?
์ œ๋ชฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”_-001 (7)

import sys
input = sys.stdin.readline

N, r, c = map(int, input().split())

visited = 0
def mv(sz: int, x: int = 0, y: int = 0) -> bool:
    global visited
    
    if not (x <= r < x + sz and y <= c < y + sz):  # ํ˜„์žฌ ์‚ฌ๋ถ„๋ฉด์— (x, y) ์ขŒํ‘œ๊ฐ€ ์—†๋‹ค๋ฉด
        visited += sz * sz  # ์˜์—ญ ํฌ๊ธฐ๋งŒํผ ๋”ํ•ด์ฃผ๊ณ 
        return False  # ์ฆ‰์‹œ False ๋ฆฌํ„ด
    elif x == r and y == c:  # ํ˜„์žฌ ์ขŒํ‘œ๊ฐ€ ์ฐพ๋Š” ์ขŒํ‘œ์™€ ๊ฐ™๋‹ค๋ฉด
        return True  # True ๋ฆฌํ„ด
    else:  # ์•„๋‹ˆ๋ผ๋ฉด
        sz //= 2  # ํ•˜์œ„ 4๋ถ„๋ฉด์— ๋Œ€ํ•ด ํ•˜๋‚˜๋ผ๋„ True๋ฉด return True
        return mv(sz, x, y) or mv(sz, x, y + sz) or mv(sz, x + sz, y) or mv(sz, x + sz, y + sz)
        

mv(2 ** N)  # ์ตœ์ดˆ ํฌ๊ธฐ
print(visited)  # ๊ธฐ๋กํ•œ ํฌ๊ธฐ ์ถœ๋ ฅ

Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฆฌ๋ทฐ๊ฐ€ ๋„ˆ๋ฌด ๋Šฆ์–ด์„œ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค...๐Ÿ˜ญ
๋ฌธ์ œ ์ œ๋ชฉ์ด ์‹ ๊ธฐํ•ด์„œ ํ•œ๋ฒˆ ๋„์ „ํ•ด๋ดค๋‹ค๊ฐ€ ๊นจ์กŒ๋˜ ๋ฌธ์ œ๋„ค์š”...๐Ÿฅน
๋ถ„ํ• ์ •๋ณต๋„ ์•ฝ๊ฐ„ dp์™€ ๋น„์Šทํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ‘ธ๋Š” ๊ฒƒ ๊ฐ™์•„์š”!
ns๋กœ 4๋ถ„ํ• ํ•œ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•  ์ƒ๊ฐ์€ ๋ชปํ–ˆ์—ˆ๋Š”๋ฐ ํ™•์‹คํžˆ ์ค‘์š”ํ•œ ๊ธฐ์ค€ ์—ญํ• ์„ ํ•ด์ฃผ๋Š” ๊ฒƒ ๊ฐ™์•„์š”.
z_setting ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„์—์„œ cnt = 0์„ ๋ณด๊ณ  ์•ฝ๊ฐ„ ๊ฐธ์šฐ๋šฑํ–ˆ์—ˆ๋Š”๋ฐ์š”..!

image

์ด๋ ‡๊ฒŒ ๋ถ„์„ํ•ด๋ณด์•˜๋Š”๋ฐ ์ œ๊ฐ€ ์ž˜ ์ดํ•ดํ•œ ๊ฒƒ์ด ๋งž๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!!

@gjsk132
Copy link
Member Author

gjsk132 commented Apr 19, 2024

์ด๋ ‡๊ฒŒ ๋ถ„์„ํ•ด๋ณด์•˜๋Š”๋ฐ ์ œ๊ฐ€ ์ž˜ ์ดํ•ดํ•œ ๊ฒƒ์ด ๋งž๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!!

@xxubin04
๋„ค ๋งž์Šต๋‹ˆ๋‹ค!
์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, count๋ฅผ ์„ธ๋Š” ๋ณ€์ˆ˜๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ , ๊ทธ ๋‹ค์Œ ํ˜ธ์ถœ๋˜๋Š” ์žฌ๊ท€์˜ ๊ฒฐ๊ณผ๋ฅผ cnt์— ๋”ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด์„œ cnt๊ฐ’์„ ๋ˆ„์ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฐ ๋ฐฉ๋ฒ• ์ด์™ธ์—๋„, ํ•จ์ˆ˜์˜ ๊ฐ’์„ ๋ˆ„์ ํ•˜๋Š” ๊ฒฝ์šฐ,
๊ตํ™ฉ์„ ๋ฐฐ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•ด์ฃผ๊ฑฐ๋‚˜ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋„ฃ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

@Dolchae Dolchae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ทธ๋ฆผ์œผ๋กœ ๋ณผ ๋• ๊ฝค ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„ ์ค„ ์•Œ์•˜๋Š”๋ฐ ์—ญ์‹œ๋‚˜ ๋ณต์žกํ•˜๊ตฐ์š”๐Ÿ˜‚ ์‹œ๊ฐ„ ์ดˆ๊ณผ ํ•ด๊ฒฐํ•˜๋Š” ์•„์ด๋””์–ด ๋ฉ‹์ ธ์š”! ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants