๐Ÿ‘ฉโ€๐ŸŽ“๊ณต๋ถ€ - 2024.01.11 ์˜ค๋Š˜์˜ ๋ฐฑ์ค€ ๋ฌธ์ œํ’€์ด

์œ ๋ น๊ฐœยท2024๋…„ 1์›” 11์ผ
0

PS-์•Œ๊ณ ๋ฆฌ์ฆ˜2

๋ชฉ๋ก ๋ณด๊ธฐ
11/34
post-thumbnail

16929๋ฒˆ - Two Dots

์‚ฌ์ดํด์„ ๋Œ๋ฆฌ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์œ„์— ๋ณด์‹œ๋Š” ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ์‹œ์ž‘์ ์—์„œ ๋‹ค๋ฅธ ์ ๊ณผ ๊ฒน์น˜์ง€ ์•Š๊ฒŒ ๋‹ค์‹œ ๋Œ์•„์™€ ์‚ฌ์ดํด์„ ํ˜•์„ฑ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด Yes, ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด No๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ํƒ์ƒ‰ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.


์ดˆ๊ธฐ ๊ตฌ์ƒ

์ผ๋‹จ ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๋– ์˜ค๋ฅธ ๊ฑด ์‹œ์ž‘์ ์—์„œ๋ถ€ํ„ฐ visited๋กœ ํ‘œ๊ธฐํ•˜๋ฉด์„œ DFS๋กœ ํƒ์ƒ‰์„ ์ด์–ด๋‚˜๊ฐ€๋˜ ํƒ์ƒ‰์˜ ํšŸ์ˆ˜๊ฐ€ 3ํšŒ ์ด์ƒ์ด ๋˜๋ฉด์„œ ํ˜„์žฌ ์œ„์น˜์—์„œ 4๋ฐฉํ–ฅ ํƒ์ƒ‰์„ ํ–ˆ๋Š”๋ฐ ์‹œ์ž‘์ ์ด ๋‚˜์˜จ๋‹ค๋ฉด ์‚ฌ์ดํด(์ง์‚ฌ๊ฐํ˜•)์ด ํ˜•์„ฑ ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ Yes๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค...๋Š” ๋Š๋‚Œ์œผ๋กœ ์ƒ๊ฐ์„ ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.


๊ธฐ๋ณธ ๋ณ€์ˆ˜ ์„ ์–ธ

N,M = map(int,input().split())
graph = []
distance = 0
boolean = False
ans = []
visited = [[0]*M for _ in range(N)]
for _ in range(N):
    graph.append(list(input()))

๊ทธ๋ž˜ํ”„์™€ ๋ฐฉ๋ฌธ์—ฌ๋ถ€, ํƒ์ƒ‰ ๊ฑฐ๋ฆฌ ๋ฐ bool ๊ฐ’์„ ์„ ์–ธํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

for i in range(N):
    for j in range(M):
        dfs(i,j,i,j)
        visited = [[0]*M for _ in range(N)]
        distance = 0

๊ฐ ์ธ๋ฑ์Šค๋งˆ๋‹ค ํƒ์ƒ‰์„ ๋Œ์•„ ๋‚˜์˜ค๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค ํ‹€๋ฆฌ๋ฏ€๋กœ ๋ชจ๋“  ์ธ๋ฑ์Šค์—์„œ DFS ํƒ์ƒ‰์„ ๋‹ค ๋Œ๊ฒ ์Šต๋‹ˆ๋‹ค.

import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline 

dx = [-1,1,0,0]
dy = [0,0,1,-1]

ํŒŒ์ด์ฌ์—์„œ DFS ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•˜๋Š” ๊ฒƒ๋“ค์ž…๋‹ˆ๋‹ค.
recursionerror์— ๊ฑธ๋ฆฌ์ง€ ์•Š๊ธฐ ์œ„ํ•ด setrecursionlimit, ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•œ sysinput๋“ฑ์„ ๊ฐ€์ ธ์˜ค๊ณ  ์ƒํ•˜์ขŒ์šฐ DFS ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ dx์™€ dy๋“ฑ์„ ์„ ์–ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


DFS ์ˆ˜ํ–‰

def dfs(x,y,first_x,first_y):
    global distance,boolean
    visited[x][y] = 1

๋จผ์ € ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค. first_x,first_y๋Š” ์‹œ์ž‘์  ํ‘œ๊ธฐ๋กœ ๋ญ ๋”ฑํžˆ ์—ฐ์‚ฐํ•˜๋Š”๋ฐ ์จ๋จน์„ ๊ฒŒ ์•„๋‹Œ ์‹œ์ž‘์ ์œผ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ ๋Œ๊ณ  ์˜จ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.

    for i in range(4):
        nx = dx[i] + x
        ny = dy[i] + y

4๋ฐฉํ–ฅ ํƒ์ƒ‰์„ ์‹ค์‹œํ•ด์ฃผ๊ณ 

        if [nx,ny] == [first_x,first_y] and distance > 2:
            boolean = True

๋งŒ์•ฝ ๊ฑฐ๋ฆฟ๊ฐ’์ด 3 ์ด์ƒ์ด๊ณ  ํ˜„์žฌ ์กฐ์‚ฌ์ค‘์ธ ์ธ๋ฑ์Šค์—์„œ ์‹œ์ž‘์ ์œผ๋กœ ๋Œ์•„๊ฐ€๋Š”๊ฒŒ ๊ฐ€๋Šฅํ•˜๋‹ค! ํ•˜๋ฉด boolean ๊ฐ’์„ True๋กœ ๋ฆฌํ„ดํ•ด์ค์‹œ๋‹ค.

        if nx<0 or ny<0 or nx>=N or ny>=M:
            continue

๊ทธ๋ž˜ํ”„ ๋ฐ–์„ ๋ฒ—์–ด๋‚˜์„œ ํƒ์ƒ‰ํ•˜๋ ค ์‹œ๋„ํ•œ๋‹ค๋ฉด continue๋กœ ํ•ด๋‹น ํƒ์ƒ‰์œ„์น˜๋Š” ๋„˜๊ฒจ๋ฒ„๋ฆฌ๊ณ 

        if not visited[nx][ny] and graph[nx][ny] == graph[x][y]:
            distance += 1
            # print(boolean,'x ํƒ์ƒ‰๊ตฌ๊ฐ„:',nx,'y ํƒ์ƒ‰๊ตฌ๊ฐ„:',ny,'๊ฑฐ๋ฆฌ:',distance)
            dfs(nx,ny,first_x,first_y)

4๋ฐฉํ–ฅ ํƒ์ƒ‰์ค‘ ํ˜„์žฌ ์ƒ‰๊น”์ด ๋‹ค์Œ ํƒ์ƒ‰ํ•  ๊ตฌ๊ฐ„์˜ ์ƒ‰๊น”๊ณผ ์ผ์น˜ํ•˜๊ณ  ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์ด๋ฉด ๊ฑฐ๋ฆฌ๋ฅผ ํ•˜๋‚˜ ์˜ฌ๋ ค์ฃผ๋ฉฐ ์žฌ๊ท€๋กœ ํƒ์ƒ‰ํ•ด์ฃผ๋„๋ก ํ•ฉ์‹œ๋‹ค.

    distance -= 1

๋งˆ์ง€๋ง‰์œผ๋กœ ๊ทธ๋ž˜ํ”„ ๊นŠ์ˆ™ํžˆ ํƒ์ƒ‰ํ•˜๋‹ค ๋” ๋“ค์–ด๊ฐˆ ์ˆ˜ ์—†๋‹ค๋ฉด distance๋ฅผ 1์”ฉ ๋นผ์ฃผ๋ฉด์„œ ๋‚˜์™€์ฃผ๋„๋ก ํ•ฉ์‹œ๋‹ค.


์ถœ๋ ฅ

if boolean == True:
    print('Yes')
else:
    print('No')    

boolean๊ฐ’์ด True์ผ ๊ฒฝ์šฐ Yes๋ฅผ, False์ผ ๊ฒฝ์šฐ No๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ์ •๋‹ต์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.




profile
ํ•œ๋ฆผ๋Œ€ํ•™๊ต ์ •๋ณด๊ณผํ•™๋Œ€ 2ํ•™๋…„ ์žฌํ•™์ค‘ / ์œก๊ตฐ ์ •๋ณด๋ณดํ˜ธ๋ณ‘ 22-2๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€