티스토리 뷰
https://www.acmicpc.net/problem/15684
15684번: 사다리 조작
사다리 게임은 N개의 세로선과 M개의 가로선으로 이루어져 있다. 인접한 세로선 사이에는 가로선을 놓을 수 있는데, 각각의 세로선마다 가로선을 놓을 수 있는 위치의 개수는 H이고, 모든 세로선
www.acmicpc.net
C++ 소스 코드
#include <cstdio>
#include <vector>
using namespace std;
int n,m,h,len,ans = -1;
int garo[100][100]; // 가로선 정보 저장
vector<pair<int, int> > a; // 가로선 후보 위치
int start(int c){ // 출발점 c에서 c로 도착할 수 있는지 체크
int r = 1;
while(r<=h){
if (garo[r][c]==1){
//가로선의 왼쪽인 경우 오른쪽으로 이동
c++;
}else if(garo[r][c]==2){
//가로선의 오른쪽인 경우 왼쪽으로 이동
c--;
}
r++;
}
return c;
}
bool go(){ // 전체 사다리 체크
for(int i=1;i<=n;i++){
int res = start(i);
if (res!=i) return false;
}
return true;
}
void backtrack(int cnt,int idx){
if (cnt>3) return; //가로선 3개 이상 만들 수 없음
for(int i=idx;i<len;i++){
int x=a[i].first;
int y=a[i].second;
if (garo[x][y]!= 0|| garo[x][y+1]!=0) continue;
garo[x][y]=1;
garo[x][y+1]=2;
if (go()){
if (ans == -1 || ans > cnt) ans = cnt;
}
backtrack(cnt+1,i+1);
garo[x][y]=0;
garo[x][y+1]=0;
}
}
int main(){
scanf("%d %d %d", &n, &m, &h);
while(m--){
int u,v;
scanf("%d %d", &u, &v);
garo[u][v]= 1; // 가로선의 왼쪽은 1
garo[u][v+1]= 2; // 가로선의 오른쪽은 2
}
for(int i=1;i<=h;i++){
for(int j=1;j<=n-1;j++){
if (garo[i][j]!=0) continue;
if (garo[i][j+1]!=0) continue;
// 가로선 만들 수 있는 후보 위치 저장
a.push_back(make_pair(i,j));
}
}
if (go()){ //가로선 추가없이 사다리 통과
printf("0\n");
return 0;
}
len = (int)a.size(); //가로선 가능 갯수
backtrack(1,0);
printf("%d\n",ans);
return 0;
}
'Algorithm > 알고리즘 문제풀이' 카테고리의 다른 글
BOJ) 15686 - 치킨 배달 (0) | 2020.06.13 |
---|---|
BOJ) 1261 - 알고스팟 (0) | 2020.06.12 |
BOJ) 15683 - 감시 (0) | 2020.06.11 |
BOJ) 11726 - 2xn 타일링 (1) | 2020.06.11 |
BOJ) 14891,15662 - 톱니바퀴 , 톱니바퀴 (2) (0) | 2020.06.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- UIHostingController
- 코코아팟
- blendshape
- SwiftUI
- 프로그래머스
- infallible
- Reactivex
- ios
- DispatchQueue
- blendshapes
- boj
- 백준온라인저지
- rxswift6
- Swift weak
- rxswift
- cocoapods
- 카카오인턴십
- SWEA
- Swift unowned
- 백준
- GraphDB
- 알고리즘
- Swift
- disposeBag
- C++
- Lottie
- 안드로이드
- ARKit
- Kotlin
- coreml
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함