-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblockcrusher.cpp
More file actions
75 lines (61 loc) · 1.06 KB
/
blockcrusher.cpp
File metadata and controls
75 lines (61 loc) · 1.06 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
using namespace std;
const int MAXW = 65;
const int MAXH = 25;
int block[MAXW][MAXH];
int W,H;
int dp[MAXW][MAXH];
int sol[MAXH];
int main(){
ios_base::sync_with_stdio(false);
while(1){
int i,j;
cin>>H>>W;
char c;
if(H<1)
break;
for(j=0;j<H;j++)
for(i=0;i<W;i++){
cin>>c;
block[i][j]=c-'0';
}
for(i=0;i<W;i++)
dp[i][0] = block[i][0];
int mindex;
//Bottom-Up DP
for(j=0;j<H;j++){
mindex=0;
for(i=0;i<W;i++){
int tmin = dp[i][j-1];
if(i>0)
tmin = min(tmin,dp[i-1][j-1]);
if(i<W-1)
tmin = min(tmin,dp[i+1][j-1]);
dp[i][j] = tmin+block[i][j];
if(dp[i][j]<dp[mindex][j])
mindex=i;
}
sol[j]=mindex;
}
for(j=H-2;j>=0;j--){
int pos = sol[j+1];
mindex=pos;
if(pos>0&&dp[pos-1][j]<dp[mindex][j])
mindex = pos-1;
if(pos<W-1&&dp[pos+1][j]<dp[mindex][j])
mindex = pos+1;
sol[j]=mindex;
}
for(j=0;j<H;j++){
for(i=0;i<W;i++){
if(sol[j]!=i)
cout<<block[i][j];
else
cout<<" ";
}
cout<<endl;
}
cout<<endl;
}
return 0;
}