BFS๋ฅผ ์ฌ์ฉํด์ ํ์ํ๋ค. ๊ฐ๋ฅํ ๋ชจ๋ ๋ฐฉ์ ๋ถ์ ๋ค ํฌ ์ ์์ด์ผ ํ๊ธฐ ๋๋ฌธ. ํ์์ ํ๋ฉด์ ํ์ ๋ด์์ผ ํ๋ ๊ฐ๋ค์ ๋ค์๊ณผ ๊ฐ๋ค
๋ฐฉ์ ๋ถ์ ์ผ์ก์ง๋ง โญ๏ธ ์์ง ๋ฐฉ๋ฌธ์ ํ์ง ์์ ๊ณณ โ
๋ฐฉ์ ๋ถ์ ์ผ์ง์ง ์์์ง๋ง โ ๋ฐฉ๋ฌธ์ ํ ๊ณณ โญ๏ธ
์ฌ๊ธฐ์ ๋ถ์ด ์ผ์ก๋ค
๋ ์ด ๋ฐฉ์ ๋ถ์ ์ผค ์ ์๋ ์ค์์น๊ฐ ์๋ ๊ณณ์ ์ง๋์ณ์๋ค๋ ๊ฒ์ด๊ณ , ๋ฐฉ๋ฌธ์ ํ๋ค
๋ ๊ฒ์ ๋ถ์ด ์ผ์ง ๋ฐฉ์์ ์ด๋ํ ์ ์๋ ๊ณณ์ด์๋ค๋ ๊ฒ.
์ฌ๊ธฐ์ ๋ฐฉ์ ๋ถ๋ ์ผ์ก๊ณ โญ๏ธ ๋ฐฉ๋ฌธ๋ ํ ๊ณณ์ โญ๏ธ ๊ฐ ํ์๊ฐ ์๋ค.
# ๋ถ์ผํค
import sys
from collections import deque, defaultdict
input = sys.stdin.readline
n, m = map(int, input().rstrip().split())
room = defaultdict(list)
visited = defaultdict(bool)
light = dict()
for _ in range(m):
x, y, a, b = map(lambda x: int(x)-1, input().rstrip().split())
room[(x, y)].append([a, b])
queue = deque([(0, 0)])
light[(0, 0)] = True
visited[(0, 0)] = True
while queue:
x, y = queue.popleft()
for nx, ny in room[(x, y)]:
# ์์ง ๋ฐฉ์ ๋ถ์ด ์ผ์ง์ง ์์์ง๋ง ๋ฐฉ๋ฌธ์ ํ ๊ณณ
if not light.get((nx, ny)):
light[(nx, ny)] = True
if visited[(nx, ny)]:
queue.append((nx, ny))
for nx, ny in (x-1, y), (x+1, y), (x, y-1), (x, y+1):
if nx<0 or ny<0 or nx>=n or ny>=n:
continue
# ์์ง ๋ฐฉ๋ฌธ์ ํ์ง ์์์ง๋ง ๋ถ์ ์ผ์ ธ์๋ ๊ณณ
if not visited[(nx, ny)]:
visited[(nx, ny)] = True
if light.get((nx, ny)):
queue.append((nx, ny))
print(light)
print(len(light))
"""
3 6
1 1 1 2
2 1 2 2
1 1 1 3
2 3 3 1
1 3 1 2
1 3 2 1
"""