Skip to content

Conversation

@alirz-pixel
Copy link
Member

🚀 이슈 번호

Resolve: {#2319}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지: DP + DFS
  • 🔹 어떤 방식으로 접근했는지

다른 문제들을 푸는 것처럼 DFS을 진행하되 방문 처리에 유의가 필요하다.

루프가 생기는 지점을 감지하기 위해 "방문" 자체는 -1로 표기하고,
순회를 마친 경로에 대해선 재탐색을 진행할 때의 시간복잡도 개선을 위하여 메모라이제이션을 진행한다.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(RC)
  • 이유:

메모라이제이션을 통해 이미 방문한 지점은 추가 방문하지 않으므로
최대 board의 크기만큼 진행된다.

💻 구현 코드

#include <iostream>
#include <vector>
#include <deque>

using namespace std;

struct Info {
	int cnt;
	int y;
	int x;
};

int visited[51][51] = { 0, };

int dy[4] = { 1, -1, 0, 0 };
int dx[4] = { 0, 0, 1, -1 };
int dfs(vector<string>& board, int& r, int& c, int y, int x) {
	int &ret = visited[y][x];
	if (ret) return ret;
	ret = -1;

	int step = 1;
	int move = board[y][x] - '0';
	for (int dir = 0; dir < 4; dir++) {
		int ny = y + (move * dy[dir]);
		int nx = x + (move * dx[dir]);

		if (ny < 0 || nx < 0 || ny >= r || nx >= c)
			continue;

		if (board[ny][nx] == 'H')
			continue;

		// visited 검사
		int cur = dfs(board, r, c, ny, nx);
		if (cur == -1)
			return -1;
		step = max(step, cur + 1);
	}

	return ret = step;
}

int main() {
	int r, c;
	cin >> r >> c;

	vector<string> board(r);
	for (int y = 0; y < r; y++)
		cin >> board[y];

	cout << dfs(board, r, c, 0, 0) << "\n";

	/*
	for (int y = 0; y < r; y++) {
		for (int x = 0; x < c; x++) {
			cout << visited[y][x] << " ";
		}
		cout << "\n";
	}
	*/

	return 0;
}

@alirz-pixel alirz-pixel self-assigned this Jan 20, 2026
@alirz-pixel alirz-pixel linked an issue Jan 20, 2026 that may be closed by this pull request
@alirz-pixel alirz-pixel merged commit ed448a7 into main Jan 25, 2026
1 check passed
@alirz-pixel alirz-pixel deleted the munhyeong/2319/1 branch January 25, 2026 11:31
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.

260120 : 코딩테스트

2 participants