boj_2210_숫자판점프

pdot715·2018년 10월 24일
0

설계단계

vector로 구하고
vector 중복 erase, unique로 없애주고

dfs 돌려서
size가 5가되면
그걸 문자열벡터에 넣어주고
또 돌리고...

DFS 구현


vector<string> s;

정답코드

#include <cstring>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<string> num;

bool safe(int y, int x) {
	return (0 <= y && y < 5) && (0 <= x && x < 5);
}


int MAP[5][5];

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

void dfs(int y, int x, string s, int cnt) {

	s.push_back(MAP[y][x]);

	if (cnt == 6) {
		num.push_back(s);
		return;
	}

	for (int i = 0; i < 4; i++) {
		int ny = y + dy[i];
		int nx = x + dx[i];

		if (safe(ny, nx)) {
			dfs(ny, nx, s, cnt + 1);
		}
	}
}



int main() {
	string s;

	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 5; j++) {
			scanf("%d", &MAP[i][j]);
		}
	}
	


	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 5; j++) {
			dfs(i, j, s, 1);
		}
	}



	sort(num.begin(), num.end());
	num.erase(unique(num.begin(), num.end()), num.end());

	printf("%d", num.size());
}
profile
개발자지망

0개의 댓글