-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1034162_UVA116_20160509.c
60 lines (50 loc) · 1.06 KB
/
A1034162_UVA116_20160509.c
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
#include<stdio.h>
main(){
int m,n;
int a[200][200],b[200][200],c[200][200];
int i,j;
int x,y,x1,y1,t,d;
while(scanf("%d %d",&m,&n)!=EOF){
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++){
b[i][n-1]=a[i][n-1];
}
for(i=n-2;i>=0;i--){
for(j=0;j<m;j++){
x=j;
y=i+1;
x1=j+1;
if(x1>=m)x1=0;
if(b[x1][y]<b[x][y]||(b[x1][y]==b[x][y]&&x1<x)){
x=x1;
}
x1=j-1;
if(x1<0)x1=m-1;
if(b[x1][y]<b[x][y]||(b[x1][y]==b[x][y]&&x1<x)){
x=x1;
}
b[j][i]=a[j][i]+b[x][y];
c[j][i]=x;
}
}
t=b[0][0];
d=0;
for(i=1;i<m;i++){
if(t>b[i][0]){
t=b[i][0];
d=i;
}
}
printf("%d",d+1);
for(i=0;i<n-1;i++){
d=c[d][i];
printf(" %d",d+1);
}
printf("\n");
printf("%d\n",t);
}
}