티스토리 뷰
https://www.acmicpc.net/problem/2210
2210번: 숫자판 점프
111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, 212121 이 가능한 경우들이다.
www.acmicpc.net
한 정점에서 다른 정점으로 중복 방문이 가능하기에, 인접 정점으로 DFS를 수행하면서 깊이가 6이 될 때의 값을 Set에 저장합니다.
Set은 중복된 값을 허용하지 않기에 Set의 사이즈가 답이 됩니다.
C++ 소스 코드
#include <cstdio>
#include <string>
#include <set>
using namespace std;
set<int> st;
int a[5][5];
const int dx[]={0,0,-1,1}, dy[]={-1,1,0,0};
void backtrack(int x, int y, int len, int now){
if (len == 6){
st.insert(now);
return;
}
for(int i=0;i<4;i++){
int nx = x+ dx[i], ny = y+dy[i];
if (nx <0|| nx>=5 || ny<0 || ny>=5) continue;
backtrack(nx,ny,len+1,now*10+a[nx][ny]);
}
}
int main(){
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
scanf("%1d", a[i][j]);
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
backtrack(i,j,1, a[i][j]);
}
}
printf("%d\n",(int)st.size());
return 0;
}'Algorithm > 알고리즘 문제풀이' 카테고리의 다른 글
| BOJ) 2234 - 성곽 (0) | 2020.06.30 |
|---|---|
| BOJ) 2422 - 한윤정이 이탈리아에 가서 아이스크림을 사먹는데 (0) | 2020.06.28 |
| BOJ) 3568 - iSharp (0) | 2020.06.26 |
| BOJ) 12904 - A와 B (0) | 2020.06.24 |
| BOJ) 10986 - 나머지 합 (0) | 2020.06.23 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- rxswift6
- boj
- ARKit
- 백준온라인저지
- 알고리즘
- SwiftUI
- coreml
- UIHostingController
- SWEA
- Kotlin
- 코코아팟
- infallible
- Swift unowned
- Lottie
- C++
- 백준
- Reactivex
- rxswift
- DispatchQueue
- Swift weak
- blendshapes
- cocoapods
- blendshape
- 프로그래머스
- GraphDB
- 카카오인턴십
- Swift
- ios
- disposeBag
- 안드로이드
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함