https://www.acmicpc.net/problem/1577
1577번: 도로의 개수
첫째 줄에 도로의 가로 크기 N과 세로 크기 M이 주어진다. N과 M은 100보다 작거나 같은 자연수이고, 둘째 줄에는 공사중인 도로의 개수 K가 주어진다. K는 0보다 크거나 같고, 100보다 작거나 같은
www.acmicpc.net
[난이도] Gold4
[유형] DP
[풀이]
map[100][100][2] 배열을 선언해서
(y,x) -> (y+1,x) 길이 막혀있는 경우 map[y][x][0]=1,
(y,x) -> (y,x+1) 길이 막혀있는 경우 map[y][x][1]=1 로 체크한 뒤
DP로 모든 경우를 구해주면 된다.
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
using ll = long long;
int dy[2]={1,0},dx[2]={0,1};
int K,N,M,map[101][101][2];
ll dp[101][101];
ll sol(int y,int x){
if(y==N&&x==M) return 1;
ll& ret = dp[y][x];
if(ret!=-1) return ret;
ret = 0;
for(int i=0;i<2;i++){
if(!map[y][x][i]){
int ny=y+dy[i],nx=x+dx[i];
if(ny<=N&&nx<=M) ret+=sol(ny,nx);
}
}
return ret;
}
int main(){
scanf("%d%d%d",&N,&M,&K);
while(K--){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>c)swap(a,c);
else if(b>d)swap(b,d);
if(a<c) map[a][b][0]=1;
else if(b<d) map[a][b][1]=1;
}
memset(dp,-1,sizeof(dp));
printf("%lld",sol(0,0));
}
https://github.com/has2/Problem-Solving/blob/master/boj-solved.ac/Gold4/1577.cpp
'Problem-Solving > BOJ' 카테고리의 다른 글
[BOJ/백준][Gold4] 16932 : 모양 만들기 (C++) (0) | 2021.01.31 |
---|---|
[BOJ/백준][Gold4] 8981 : 입력숫자 (C++) (0) | 2021.01.31 |
[BOJ/백준][Gold4] 1153 : 네 개의 소수 (C++) (0) | 2021.01.31 |
[BOJ/백준][Gold4] 14267 : 회사 문화 1 (C++) (0) | 2021.01.31 |
[BOJ/백준][Gold4] 15483 : 최소 편집 (C++) (0) | 2021.01.31 |