๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐“ก๐“ธ๐“ธ๐“ถ๐Ÿฃ: ๐’œ๐“๐‘”๐‘œ๐“‡๐’พ๐“‰๐’ฝ๐“‚/๐“ก๐Ÿฃ๐Ÿข๐Ÿฃ: ๐’œ๐“๐‘”๐‘œ๐“‡๐’พ๐“‰๐’ฝ๐“‚

[ALPS Study] DFS(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰) - ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ(BOJ 2667)

์‚ฌ์‹ค ๋‚˜ ๋ฌธ์ œ ์ œ๋Œ€๋กœ ์ฝ์ง€๋„ ์•Š๊ณ  dfs ๋ฌธ์ œ ์•„๋ฌด๊ฑฐ๋‚˜ ์ฐ์–ด์„œ ํ’€๋ ค๊ณ  ์˜ฌ๋ ค๋†“๊ณ  ์ฝ์–ด๋ณด๋‹ˆ๊นŒ ์ €๋ฒˆ ๋ฌธ์ œ๋ž‘ ๊ฑฐ์˜ ๊ทธ๋ƒฅ ์™„๋ฒฝํžˆ ๊ฐ™์€..๋ฌธ์ œ์˜€๋‹ค..ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ ์™œ ํ•œ ๊ฑฐ ์ง€...? ๋‹ค๋ฅธ ์ ์ด ์žˆ๋‹ค๋ฉด ๊ฐ ๋‹จ์ง€๋‚ด ์ง‘์˜ ์ˆ˜๋ฅผ ์ •๋ ฌํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค๋Š”๊ฑฐ..??

 

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h> 

int map[50][50] = { 0, };
int count[50] = { 0, };
int M, N, K;
int cnt = 0;
int end = 0; int baechu = 0;

void func();
void dfs(int x, int y);
void printmap();
int main() {
	int T;
	int i;
	scanf("%d", &T);
	
	for (i = 0; i < T; i++) {
		func();
	}
}

void func() {
	int i, tmpx, tmpy;
	cnt = 0; end = 0; baechu = 0;
	scanf("%d %d %d", &N, &M, &K);
	for (i = 0; i < K; i++) {
		scanf("%d %d", &tmpx, &tmpy);
		map[tmpx][tmpy] = 1;
	}
	dfs(0, 0);
	printf("\n%d\n", cnt);
}

void dfs(int x, int y) {
	printf("dfs(%d, %d)\n", x, y);
	printmap();	printf("\n");
	printf("cnt = %d", cnt);	printf("\n");

	if (x == N - 1 && y == N - 1) {
		end = 1;
		return;
	}

	while (!end) {
		if (map[x][y] == 0) {
			if (baechu == 0) {
				if (x == N - 1)
					dfs(0, y + 1);
				else
					dfs(x + 1, y);
			}
			if (baechu == 1) {
				return;
			}
		}

		if (map[x][y] == 1 && baechu == 0) {
			baechu = 1;	cnt++;
			if (x != N - 1)
				dfs(x + 1, y);
			dfs(x, y + 1);
			map[x][y] = 0;
			baechu = 0;
		}

		if (map[x][y] == 1 && baechu == 1) {
			if (x != N - 1)
				dfs(x + 1, y);
			dfs(x, y + 1);
			map[x][y] = 0;
		}
	}
}

void printmap() {
	int i, j;
	for (i = 0; i < M; i++) {
		for (j = 0; j < N; j++) {
			printf("%d ", map[j][i]);
		}
		printf("\n");
	}
}

์ €๋ฒˆ ์ฝ”๋“œ์—์„œ ์กฐ๊ธˆ ์ˆ˜์ •ํ•ด๋ณด์•˜๋‹ค!