728x90
문제
link: https://www.acmicpc.net/problem/2931
접근
모든 칸을 순회하며, 빈칸('.')인 경우, 주위에 빈칸 방향으로 열린 가스관이 있는지 확인한다.
코드
import sys
input = sys.stdin.readline
d = ((1, 0), (0, 1), (-1, 0), (0, -1))
arr = [{'|', '+', '2', '3'}, {'-', '+', '3', '4'}, {'|', '+', '1', '4'}, {'-', '+', '1', '2'}]
road = {0b1111: '+', 0b1010: '|', 0b0101: '-', 0b1100: 1, 0b0110: 2, 0b0011: 3, 0b1001: 4}
r, c = map(int, input().split())
board = [list(input().rstrip()) for _ in range(r)]
ans = 0
tmp = 0
for i in range(r):
if ans:
break
for j in range(c):
tmp = 0
if board[i][j] == '.':
for k in range(4):
nx, ny = i+d[k][0], j+d[k][1]
if 0 <= nx < r and 0 <= ny < c and board[nx][ny] in arr[k]:
tmp |= (1 << (3-k))
if tmp > 0:
ans = [i+1, j+1, road[tmp]]
break
print(*ans)728x90
'알고리즘' 카테고리의 다른 글
| [백준] 32293 | ABB to BA (Hard) [파이썬/python] (0) | 2025.10.10 |
|---|---|
| [백준] 27966 | △ [파이썬/python] (0) | 2025.10.09 |
| [백준] 1655 | 가운데를 말해요 [파이썬/python] (0) | 2025.10.07 |
| [백준] 3584 | 가장 가까운 공통 조상 [파이썬/python] (0) | 2025.10.04 |
| [백준] 16934 | 게임 닉네임 [파이썬/python] (0) | 2025.10.03 |