-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem4019.cpp
83 lines (74 loc) · 1.84 KB
/
problem4019.cpp
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
76
77
78
79
80
81
82
83
#include <stdio.h>
int main()
{
short n, m;
int num;
scanf("%hd%hd", &n, &m);
short path[n + 1][m + 1][2];
path[1][1][0] = -1;
path[1][1][1] = -1;
int colSum[m + 1], rowSum;
for (short i = 1; i <= m; i++)
colSum[i] = 0;
for (short i = 1; i <= n; i++)
{
rowSum = 0;
for (short j = 1; j <= m; j++)
{
scanf("%d", &num);
if (i > 1 && j > 1)
{
if (colSum[j] > colSum[j - 1])
{
path[i][j][0] = i;
path[i][j][1] = j - 1;
colSum[j] = colSum[j - 1] + num;
}
else
{
path[i][j][0] = i - 1;
path[i][j][1] = j;
colSum[j] = colSum[j] + num;
}
}
else if (i == 1 && j > 1)
{
colSum[j] = colSum[j - 1] + num;
path[i][j][0] = i;
path[i][j][1] = j - 1;
}
else if (i > 1 && j == 1)
{
colSum[j] += num;
path[i][j][0] = i - 1;
path[i][j][1] = j;
}
else
{
colSum[j] += num;
}
rowSum += num;
}
}
printf("%d\n", colSum[m]);
short shortPath[n + m - 1][2];
short i = n, j = m, k = 0;
while (i != 1 || j != 1)
{
shortPath[k][0] = i;
shortPath[k][1] = j;
k++;
short tempI = path[i][j][0];
short tempJ = path[i][j][1];
i = tempI;
j = tempJ;
}
shortPath[k][0] = 1;
shortPath[k][1] = 1;
for (short i = k; i >= 0; i--)
{
printf("%hd %hd", shortPath[i][0], shortPath[i][1]);
if (i != 0)
printf("\n");
}
}