-
[C++] 삼성 1949. [모의 SW 역량테스트] 등산로 조성코딩테스트/코딩테스트 연습 2020. 10. 27. 18:06728x90
#include <iostream> #include <vector> using namespace std; int arr[8][8]; int visit[8][8]; int dir[][2] = { {1,0},{0,1},{-1,0},{0,-1} }; int max_dist = 0; int N; int K; void solution(int x, int y, bool k, int dist) { if (x < 0 || y < 0 || x >= N || y >= N) return; for (int i = 0; i < 4; i++) { int next_x = x + dir[i][0]; int next_y = y + dir[i][1]; if (next_x < 0 || next_y < 0 || next_x >= N || next_y >= N) continue; if (visit[next_x][next_y] == 1) continue; if (arr[x][y] > arr[next_x][next_y]){ visit[x][y] = 1; solution(next_x, next_y, k, dist+1); visit[x][y] = 0; } else if (k == false) { for (int j = 1; j <= K; j++) { arr[next_x][next_y] -= j; if (arr[x][y] > arr[next_x][next_y]) { visit[x][y] = 1; solution(next_x, next_y, true, dist+1); visit[x][y] = 0; } arr[next_x][next_y] += j; } } } if (max_dist < dist) max_dist = dist; } int main() { int T; cin >> T; for (int t = 1; t <= T; t++) { max_dist = 0; int max_top = 0; cin >> N >> K; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> arr[i][j]; if (max_top < arr[i][j]) max_top = arr[i][j]; } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (arr[i][j] == max_top) solution(i, j, false, 1); } } cout << "#" << t << " " << max_dist << endl; } return 0; }
728x90'코딩테스트 > 코딩테스트 연습' 카테고리의 다른 글
[C++] 삼성 1247. [S/W 문제해결 응용] 3일차 - 최적 경로 (0) 2020.10.27 [C++] 삼성 1244. [S/W 문제해결 응용] 2일차 - 최대 상금 (0) 2020.10.27 [C++] 백준 15685번 드래곤커브 (0) 2020.10.27 [C++] 백준 14503번. 로봇 청소기 (0) 2020.10.27 [C++] 삼성 1767. [SW Test 샘플문제] 프로세서 연결하기 (0) 2020.10.27