inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비

86. 피자 배달 거리(DFS활용)

질문이 있어요 선생님!

170

celestial_

작성한 질문수 72

0

선생님, 안녕하세요?

저는

dfs를 통해서 조합을 각각 만들어내고, (x좌표,y좌표,움직인횟수) 

그 조합을 대하여 큐에 넣어준 이후 dfs를 돌렸습니다.

큐에서는 fcfs로 어차피 1을 먼저 만나게 되는 정보로 방문처리해주면 그

#include<iostream>
#include<queue>
#include<math.h>
#include<algorithm>
using namespace std;

int map[51][51];
bool visited[51][51];
int N, M;

int dx[4] = { 1,-1,0,0 };
int dy[4] = { 0,0,1,-1 };

typedef struct node {
	int x;
	int y;
	int cnt = 0;
}Node;

queue<Node>Q;
Node piz[2501];
int pcnt = 0;
int pizza[2501];
int flag = 0;
int mini = 10000;


void BFS() {
	int cres = 0;
	Node current;

	while (!Q.empty()) {
		current.x = Q.front().x;
		current.y = Q.front().y;
		current.cnt = Q.front().cnt;
		Q.pop();
		//printf("current x : %d y : %d cnt : %d\n\n", current.x, current.y, current.cnt);
		if (map[current.x][current.y] == 1) {
			cres += current.cnt;
			//printf("cres : %d \n\n", cres);
		}

		else {
			for (int i = 0; i < 4; i++) {
				int x = current.x + dx[i];
				int y = current.y + dy[i];
				int c = current.cnt + 1;
				if (x >= 1 && y >= 1 && x <= N && y <= N && !visited[x][y] && map[x][y] == 1) {
					visited[x][y] = true;
					Node next;
					next.x = x;
					next.y = y;
					next.cnt = c;
					//printf("next x : %d y :%d cnt : %d\n\n", next.x, next.y, next.cnt);
					Q.push(next);
				}
				if (x >= 1 && y >= 1 && x <= N && y <= N && !visited[x][y] && map[x][y] == 2) {
					visited[x][y] = true;
					Node next;
					next.x = x;
					next.y = y;
					next.cnt = c;
					//printf("next x : %d y :%d cnt : %d\n\n", next.x, next.y, next.cnt);
					Q.push(next);
				}
				if (x >= 1 && y >= 1 && x <= N && y <= N && !visited[x][y] && map[x][y] == 0) {
					visited[x][y] = true;
					Node next;
					next.x = x;
					next.y = y;
					next.cnt = c;
					//printf("next x : %d y :%d cnt : %d\n\n", next.x, next.y, next.cnt);
					Q.push(next);

				}
			}
		}

		
	}

	if (mini > cres) {
		mini = cres;
	}
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			visited[i][j] = false;
		}
	}
	

}


void DFS(int start, int level) {
	if (level == M) {
		//printf("<<<start>>>\n\n");
		for (int i = 0; i < M; i++) {
			visited[piz[pizza[i]].x][piz[pizza[i]].y] = true;
			//printf("x : %d y : %d \n\n", piz[pizza[i]].x, piz[pizza[i]].y);
			Q.push(piz[pizza[i]]);
		}
		BFS();
	}
	else {

		for (int i = start; i < pcnt; i++) {
			pizza[level] = i;
			DFS(i + 1, level + 1);
		}




	}
}


int main() {

	ios_base::sync_with_stdio(false);

	cin >> N >> M;

	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			int input;
			cin >> input;
			map[i][j] = input;
			if (input == 2) {
				Node start;
				start.x = i;
				start.y = j;
				start.cnt = 0;
				piz[pcnt++] = start;
			}
		}
	}
	DFS(0, 0);

	printf("%d", mini);

}



이후 중복의 여지도 없을 것이라 생각했구요, 

아무튼

1을 만나게 되면 그 때의 이동값을 누적하는 방식으로 해결했씁니다.

아마 이거 백준에 비슷한 문제있었던 거같은데요

이 문제에서는 3번 케이스에서 유일하게 시간 초과가 나는군요ㅜㅜㅜㅜ

어떤 점이 문제일 지 궁금합니다 선생님.ㅜㅜ!

C++ 코테 준비 같이 해요!

답변 1

0

김태원

안녕하세요^^

영상에 방법처럼 DFS 하나로 끝내면 좋겠습니다.

테스트 케이스 질문

0

367

1

병합정렬 시간복잡도 질문

0

458

1

41.연속된 자연수의 합 문제풀이에서 수학적인 원리를 모르고 있습니다.

0

1339

2

질문드립니다.

0

371

1

질문드립니다!

0

425

1

dev 프로그램 질문

0

270

1

문제가 이해가 안되요

0

371

1

4번 나이차이 문제 접근법 질문 드립니다.

0

301

1

source file not compiled

0

1029

3

59번 질문드립니다.

0

366

1

25번 문제 질문

0

343

1

4. 나이차이 문제 질문입니다.

0

364

1

90번 라이언 킹 심바 1번 테스트 케이스

0

464

1

71번 문제 전역 변수 질문 있습니다

0

353

1

75번, 79번 priority_queue관련

1

351

1

75.최대 수입 스케줄

0

390

2

복면산 정답의 수

0

424

1

테스트 케이스에 대해서

0

438

1

수업 내용 질문입니다!

1

226

1

풀어보면 좋은 문제 목록 - 2580 스토쿠 DFS 질문입니다!!

0

812

2

12. 플로이드-와샬(그래프 최단거리) . 27:25초

0

248

1

다른 풀이 방식

0

310

1

크루스칼 vs 프림

0

300

1

숫자 총개수 small 질문있습니다.

0

231

1