문제
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에 효과적인 배추흰지렁이를 구입하기로 결심한다. 이 지렁이는 배추근처에 서식하며 해충을 잡아 먹음으로써 배추를 보호한다. 특히, 어떤 배추에 배추흰지렁이가 한 마리라도 살고 있으면 이 지렁이는 인접한 다른 배추로 이동할 수 있어, 그 배추들 역시 해충으로부터 보호받을 수 있다. 한 배추의 상하좌우 네 방향에 다른 배추가 위치한 경우에 서로 인접해있는 것이다.
한나가 배추를 재배하는 땅은 고르지 못해서 배추를 군데군데 심어 놓았다. 배추들이 모여있는 곳에는 배추흰지렁이가 한 마리만 있으면 되므로 서로 인접해있는 배추들이 몇 군데에 퍼져있는지 조사하면 총 몇 마리의 지렁이가 필요한지 알 수 있다. 예를 들어 배추밭이 아래와 같이 구성되어 있으면 최소 5마리의 배추흰지렁이가 필요하다. 0은 배추가 심어져 있지 않은 땅이고, 1은 배추가 심어져 있는 땅을 나타낸다.
1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 1 |
0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 1 |
입력
입력의 첫 줄에는 테스트 케이스의 개수 T가 주어진다. 그 다음 줄부터 각각의 테스트 케이스에 대해 첫째 줄에는 배추를 심은 배추밭의 가로길이 M(1 ≤ M ≤ 50)과 세로길이 N(1 ≤ N ≤ 50), 그리고 배추가 심어져 있는 위치의 개수 K(1 ≤ K ≤ 2500)이 주어진다. 그 다음 K줄에는 배추의 위치 X(0 ≤ X ≤ M-1), Y(0 ≤ Y ≤ N-1)가 주어진다. 두 배추의 위치가 같은 경우는 없다.
출력
각 테스트 케이스에 대해 필요한 최소의 배추흰지렁이 마리 수를 출력한다.
예제 입력 1 복사
2
10 8 17
0 0
1 0
1 1
4 2
4 3
4 5
2 4
3 4
7 4
8 4
9 4
7 5
8 5
9 5
7 6
8 6
9 6
10 10 1
5 5
예제 출력 1 복사
5
1
예제 입력 2 복사
1
5 3 6
0 2
1 2
2 2
3 2
4 2
4 0
예제 출력 2 복사
2
이 문제 같은 경우는 전에 풀었던 단지번호붙이기 문제랑 유사한 문제였다.
단지번호붙이기 문제 풀러가기
2667번: 단지번호붙이기
<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여
www.acmicpc.net
단지번호붙이기 문제 풀이보러가기
[BAEKJOON] 2667 단지번호붙이기
문제 과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여기
alfredev.tistory.com
아무튼 입력 설정
뭐 이건 전체 x y 다 돌아야되니까 그냥 작성하기 편한 dfs로 사용해서 작성했다.
끄덕)
그리고 t를 받아 돌려돌려 돌림판~
그래서 이렇게 전체 코드를 제출했는데
아니 ??
에러가 나는게 아니겠음??
게다가 파이썬 오래해봤다고 자부하고 있었는데
저런 에러는 진짜 처음 봤다.. ㄷ
그래서 구글링해보니까
기본적으로 파이썬이 재귀 호출을 할 때에 재귀 깊이를 초과할 때 이루어지는 예외처리라고 한다.
파이썬은 기본적으로 재귀 깊이는 1000으로 설정되있다고 한다.
1000개의 깊이를 넘어가면 해당 에러를 발생시키는 것
재귀 제한을 증가시키는 방법은 해당 함수를 사용하는 것이다.
The Python RecursionError is an exception that occurs when the maximum recursion depth is exceeded. This typically occurs when a function calls itself recursively, and the recursion doesn't have a proper stopping condition (base case).
What Causes RecursionError
A RecursionError in Python is caused by a function calling itself recursively without a proper base case. Python has a limit on the number of times a function can call itself recursively. This is to ensure that the function does not execute indefinitely. If this limit is exceeded by a recursive function, a RecursionError is raised.
Python RecursionError Example
Here’s an example of a Python RecursionError thrown when calling a recursive function that does not have a base case:
def func():
func()
func()
Since the recursive function func() does not have a terminating condition, calling it creates an infinite loop as the function keeps calling itself over and over again until the RecursionError: maximum recursion depth exceeded error occurs:
Traceback (most recent call last):
File "test.py", line 4, in <module>
func()
File "test.py", line 2, in func
func()
File "test.py", line 2, in func
func()
File "test.py", line 2, in func
func()
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
How to Fix RecursionError in Python
Here are some approaches to fix a recursion error in Python:
- Adding a base case: The most common cause of a recursion error is that the function does not have a base case to stop the recursion. In such cases, a base case can be added to the function that stops recursion when a condition is met.
- Increasing the recursion limit: Python has a default maximum recursion depth of 1000. If a function exceeds this limit, it can be increased using the sys.setrecursionlimit(n) function. Developers should be careful when increasing the limit as this can cause a crash if the recursion is not properly controlled.
- Using an iterative approach: If a recursive approach is causing a recursion error, it may be possible to use an iterative approach instead e.g. a for or while loop. This can reduce the risk of hitting the maximum recursion depth, and in some cases can also lead to more efficient and easier to understand code.
Reference : https://rollbar.com/blog/python-recursionerror/
아무튼 전체 코드
아무튼 잡지식 +1
'알고리즘' 카테고리의 다른 글
[BAEKJOON] 14568 2017 연세대학교 프로그래밍 경시대회 (0) | 2023.05.23 |
---|---|
[BAEKJOON] 10808 알파벳 개수 (0) | 2023.05.21 |
[BAEKJOON] 2644 촌수계산 (0) | 2023.05.21 |
[BAEKJOON] 5014 스타트링크 (0) | 2023.05.19 |
[BAEKJOON] 1697 숨바꼭질 (0) | 2023.05.19 |