C++
재귀함수
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int N,M,S,T;
int map_[110][40],dt[40][110],result=110;
int serch(int room,int day){
if(dt[room][day]!=-1)
return dt[room][day];
if(day==T)
return 0;
int min_=110,cnt=0;
for(int i=1;i<=M;i++){
if(map_[day+1][i]==1){
int a=serch(i,day+1);
if(a==-1)
return -1;
if(i==room){
min_=min(min_,a);
}else{
min_=min(min_,a+1);
}
cnt++;
}
}
if(cnt==0||min_==-1)
return -1;
return dt[room][day] = min_;
}
int main()
{
memset(dt,-1,sizeof(dt));
scanf("%d %d",&N,&M);
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
char a;
scanf(" %c",&a);
map_[i][j]=(a=='X')? 0:1;
}
}
scanf("%d %d",&S,&T);
T--;
for(int i=1;i<=M;i++){
if(map_[S][i]==1)
result=min(result,serch(i,S));
}
if(result==110)
result=-1;
printf("%d",result);
return 0;
}
Bottom-up
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int N,M,S,T;
int map_[110][40];
int main()
{
scanf("%d %d",&N,&M);
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
char a;
scanf(" %c",&a);
map_[i][j]=(a=='X')? 0:1+map_[i-1][j];
}
}
scanf("%d %d",&S,&T);
T--;
int t=T,cnt=0;
while(t>=S){
int max_=-1;
cnt++;
for(int i=1;i<=M;i++){
max_=max(max_,map_[t][i]);
}
if(max_<1){
printf("-1");
return 0;
}
t-=max_;
}
printf("%d",cnt-1);
return 0;
}
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 공지 | 안내사항 | 관리자 | 2019.12.02 | 179 |
| 201 | 4893 | 관리자 | 2019.12.20 | 149 |
| 200 | 4878 | 관리자 | 2019.12.20 | 149 |
| 199 | 4848 | 관리자 | 2019.12.20 | 152 |
| 198 | 4698 | 관리자 | 2019.12.20 | 148 |
| 197 | 4685 | 관리자 | 2019.12.20 | 206 |
| 196 | 4073 | 관리자 | 2020.04.06 | 160 |
| 195 | 4068 | 관리자 | 2020.04.06 | 219 |
| 194 | 4065 | 관리자 | 2019.12.20 | 155 |
| 193 | 4064 | 관리자 | 2020.04.06 | 157 |
| 192 | 4059 | 관리자 | 2020.04.06 | 5941 |
| 191 | 4055 | 관리자 | 2020.04.06 | 156 |
| 190 | 4044 | 관리자 | 2020.04.06 | 167 |
| 189 | 4043 | 관리자 | 2020.04.06 | 155 |
| » | 4040 | 관리자 | 2020.04.06 | 154 |
| 187 | 4039 | 관리자 | 2019.12.20 | 157 |
| 186 | 4035 | 관리자 | 2020.04.06 | 159 |
| 185 | 4034 | 관리자 | 2020.04.06 | 151 |
| 184 | 4028 | 관리자 | 2020.04.06 | 175 |
| 183 | 4023 | 관리자 | 2019.12.20 | 178 |
| 182 | 3740 | 관리자 | 2019.12.20 | 218 |
| 181 | 3730 | 관리자 | 2019.12.20 | 181 |
| 180 | 3719 | 관리자 | 2019.12.20 | 158 |
| 179 | 3716 | 관리자 | 2019.12.20 | 147 |
| 178 | 3713 | 관리자 | 2019.12.20 | 167 |
| 177 | 3712 | 관리자 | 2019.12.20 | 164 |
| 176 | 3709 | 관리자 | 2019.12.20 | 150 |
| 175 | 3708 | 관리자 | 2019.12.20 | 117 |
| 174 | 3707 | 관리자 | 2019.12.20 | 112 |
| 173 | 3705 | 관리자 | 2019.12.20 | 115 |