https://www.acmicpc.net/problem/3109
3109번: 빵집
유명한 제빵사 김원웅은 빵집을 운영하고 있다. 원웅이의 빵집은 글로벌 재정 위기를 피해가지 못했고, 결국 심각한 재정 위기에 빠졌다. 원웅이는 지출을 줄이고자 여기저기 지출을 살펴보던
www.acmicpc.net
생각해보기
- 이동 가능 방향은 ↗➡↘로 3가지가 있다.
- 맨 위에서 부터 시작해서 아래로 내려가며 탐색하면 된다.
- 이미 이동한 곳은 통과가 가능하던 불가능하던 다시 방문할 필요가 없으므로 방문체크를 해주자.
코드
#include<iostream>
#include<string>
using namespace std;
int R,C;
int ans=0;
string pipes[10001];
int my[3]={-1,0,1};
void input(){
cin>>R>>C;
for(int i=0;i<R;++i){
cin>>pipes[i];
}
}
bool dfs(int y, int x){
pipes[y][x]='X';
if(x==C-1){
++ans;
return true;
}
for(int i=0;i<3;++i){
int ny=y+my[i];
int nx=x+1;
if(ny<0||ny>=R||pipes[ny][nx]!='.')
continue;
if(dfs(ny,nx))
return true;
}
return false;
}
void solution(){
for(int i=0;i<R;++i){
dfs(i,0);
}
cout<<ans<<'\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
input();
solution();
return 0;
}

728x90
'알고리즘 > 백준 문제' 카테고리의 다른 글
| 1781 - 컵라면 (0) | 2023.07.03 |
|---|---|
| 13144 - List of Unique Number (0) | 2023.06.30 |
| 17182 - 우주 탐사선 (0) | 2023.06.26 |
| 7490 - 0 만들기 (0) | 2023.06.22 |
| 4190 - 불! (0) | 2023.06.22 |
댓글