728x90

문제 링크 : www.acmicpc.net/problem/2210

 

2210번: 숫자판 점프

111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, 212121 이 가능한 경우들이다.

www.acmicpc.net

 

 

 

문제

5×5 크기의 숫자판이 있다. 각각의 칸에는 숫자(digit, 0부터 9까지)가 적혀 있다. 이 숫자판의 임의의 위치에서 시작해서, 인접해 있는 네 방향으로 다섯 번 이동하면서, 각 칸에 적혀있는 숫자를 차례로 붙이면 6자리의 수가 된다. 이동을 할 때에는 한 번 거쳤던 칸을 다시 거쳐도 되며, 0으로 시작하는 000123과 같은 수로 만들 수 있다.

숫자판이 주어졌을 때, 만들 수 있는 서로 다른 여섯 자리의 수들의 개수를 구하는 프로그램을 작성하시오.

입력

다섯 개의 줄에 다섯 개의 정수로 숫자판이 주어진다.

출력

첫째 줄에 만들 수 있는 수들의 개수를 출력한다.

 

배열을 char 형으로 설정해서 string의 push_back(), 

//
// DFS_BOJ2210_숫자판점프.cpp
// Coding_Test_Practice
//
// Created by 김난영 on 2021/04/11.
// Copyright © 2021 KimNanyoung. All rights reserved.
//
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
char map[5][5];
//위 오 아 왼
int dr[4] = {-1,0,1,0};
int dc[4] = {0,1,0,-1};
vector<string> strVec;
void DFS(int r, int c, int toPick, string str){
if(toPick==0){
if(find(strVec.begin(), strVec.end(), str) == strVec.end()){
strVec.push_back(str);
}
return;
}
for(int i = 0; i<4; i++){
int nr = r + dr[i];
int nc = c + dc[i];
if(nr<0 || nr>=5 || nc<0 || nc>=5) continue;
str.push_back(map[nr][nc]);
DFS(nr, nc, toPick-1,str);
str.pop_back();
}
}
int main(){
for(int i = 0; i<5; i++){
for(int j = 0; j<5; j++){
cin >> map[i][j];
}
}
for(int i = 0; i<5; i++){
for(int j = 0; j<5; j++){
DFS(i,j,6, "");
}
}
cout << strVec.size();
return 0;
}

728x90

+ Recent posts