백트래킹 문제.
중복이 가능하기 때문에 방문 확인 없이 6자리라는 조건만 지키면 된다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#include <iostream>
#include <string>
#include <map>
#define MAX 6
using namespace std;
typedef pair<int,int> pir;
int xx[4]={1,0,-1,0};
int yy[4]={0,1,0,-1};
char Num[MAX][MAX];
map<string,int> m;
int ans;
void solved(string str, int x, int y){
if(str.size()==6 && m[str]==0){
m[str]=1;
ans++;
return;
}
if(str.size()>6) return;
for(int i=0;i<4;i++){
int nx=x+xx[i];
int ny=y+yy[i];
if(nx<0 || ny<0 || nx>=5 || ny>=5) continue;
solved(str+Num[nx][ny], nx, ny);
}
}
int main(){
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cin >> Num[i][j];
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
string str="";
solved(str+Num[i][j], i, j);
}
}
cout << ans << endl;
}
|
cs |
'Coding > 백준' 카테고리의 다른 글
| 2309번 일곱 난쟁이 (0) | 2020.03.21 |
|---|---|
| 7568번 덩치 (0) | 2020.03.20 |
| 14888번 연산자 끼워넣기 (0) | 2020.03.17 |
| 1890번 점프 (0) | 2020.03.17 |
| 1965번 상자넣기 (0) | 2020.03.16 |