λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

Coding Test/λ°±μ€€(BOJ)

[BOJ] λ°±μ€€ #2210. 숫자판 점프 (C++)

🎨 문제

문제 링크: https://www.acmicpc.net/problem/2210

  • μ•Œκ³ λ¦¬μ¦˜ λΆ„λ₯˜: 깊이 μš°μ„  탐색 (DFS), κ·Έλž˜ν”„ 탐색, 브루트포슀
  • λ‚œμ΄λ„: Silver 2

 

πŸ’¬ 풀이

1. arr[5][5]에 μˆ«μžνŒμ„ μž…λ ₯λ°›κ³ ,

2. λͺ¨λ“  μ’Œν‘œμ— λŒ€ν•΄ DFS 경둜 탐색

  - 쀑볡을 ν—ˆμš©ν•˜μ§€ μ•ŠλŠ” set을 μ΄μš©ν•œλ‹€.

 

 

πŸ‘©‍πŸ’» μ½”λ“œ

C++

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <set>

#define N 5
using namespace std;

int arr[N][N];
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
set<int> s;

void DFS(int y, int x, int n, int len) {
	if (len == 6) {
		s.insert(n);
		return;
	}
	for (int i = 0; i < 4; i++) {
		int ny = y + dy[i];
		int nx = x + dx[i];
		if (ny >= 0 && ny < N && nx >= 0 && nx < N) {
			DFS(ny, nx, n * 10 + arr[ny][nx], len + 1);
		}
	}
}

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	//freopen("input.txt", "rt", stdin);
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> arr[i][j];
		}
	} // μž…λ ₯

	/* λͺ¨λ“  μ’Œν‘œμ˜ DFS 경둜 탐색 */
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			DFS(i, j, arr[i][j], 1);
		}
	}

	cout << s.size();

	return 0;
}