#include <stdio.h> #include <iostream> using namespace std; int map[10][10]; int choice[10]; int visited[1000000]; int ans; int dx[4] = {-1, 1, 0, 0}; // 상하좌우 int dy[4] = {0, 0, -1, 1}; // 상하좌우 int safe(int x, int y) { if(x >= 0 && y >= 0 && x < 5 && y < 5) { return 1; } else { return 0; } } void DFS(int x, int y, int cnt) { if(cnt == 7) { int val = choice[0]*100000 + choice[1]*10000 + choice[2]*1000 + choice[3]*100 + choice[4]*10 + choice[5]*1; if(visited[val] == 0) { visited[val] = 1; ans++; } return; } for(int i = 0; i < 4; i++) { int xpos = x+dx[i]; int ypos = y+dy[i]; if(safe(xpos, ypos) == 1) { choice[cnt] = map[xpos][ypos]; DFS(xpos, ypos, cnt+1); } } } int main(void) { // freopen("B2210_input.txt", "r", stdin); 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, 0); } } cout << ans; return 0; } | cs |
'Baekjoon > DFS' 카테고리의 다른 글
[백준 2667] 단지번호 붙이기 (DFS) (C/C++) (0) | 2020.01.26 |
---|---|
[백준 10451] 순열 사이클 (DFS) (C/C++) (0) | 2020.01.26 |
[백준 13023] ABCDE (DFS) (C/C++) (0) | 2019.12.08 |
[백준 14500] 테트로미노 (DFS) (C/C++) (★) (0) | 2019.11.19 |
[백준 10971] 외판원 순회2 (DFS) (C/C++) (★) (0) | 2019.11.19 |