#include <stdio.h> #include <stdlib.h> #include <string.h>
#define SIZE 20 #define L 1 #define R 2
int global_mountain[SIZE][SIZE] = { { 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, { 5628, 6232, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, { 29052, 1558, 26150, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, { 12947, 29926, 11981, 22371, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, { 4078, 28629, 4665, 2229, 24699, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, { 27370, 3081, 18012, 24965, 2064, 26890, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, { 21054, 5225, 11777, 29853, 2956, 22439, 3341, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, { 31337, 14755, 5689, 24855, 4173, 32304, 292, 5344, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, { 15512, 12952, 1868, 10888, 19581, 13463, 32652, 3409, 28353, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, { 26151, 14598, 12455, 26295, 25763, 26040, 8285, 27502, 15148, 4945, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, { 26170, 1833, 5196, 9794, 26804, 2831, 11993, 2839, 9979, 27428, 6684, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, { 4616, 30265, 5752, 32051, 10443, 9240, 8095, 28084, 26285, 8838, 18784, 6547, 0, 0, 0, 0, 0, 0, 0, 0,}, { 7905, 8373, 19377, 18502, 27928, 13669, 25828, 30502, 28754, 32357, 2843, 5401, 10227, 0, 0, 0, 0, 0, 0, 0,}, { 22871, 20993, 8558, 10009, 6581, 22716, 12808, 4653, 24593, 21533, 9407, 6840, 30369, 2330, 0, 0, 0, 0, 0, 0,}, { 3, 28024, 22266, 19327, 18114, 18100, 15644, 21728, 17292, 8396, 27567, 2002, 3830, 12564, 1420, 0, 0, 0, 0, 0,}, { 29531, 21820, 9954, 8319, 10918, 7978, 24806, 30027, 17659, 8764, 3258, 20719, 6639, 23556, 25786, 11048, 0, 0, 0, 0,}, { 3544, 31948, 22, 1591, 644, 25981, 26918, 31716, 16427, 15551, 28157, 7107, 27297, 24418, 24384, 32438, 22224, 0, 0, 0,}, { 12285, 12601, 13235, 21606, 2516, 13095, 27080, 16331, 23295, 20696, 31580, 28758, 10697, 4730, 16055, 22208, 2391, 20143, 0, 0,}, { 16325, 24537, 16778, 17119, 18198, 28537, 11813, 1490, 21034, 1978, 6451, 2174, 24812, 28772, 5283, 6429, 15484, 29353, 5942, 0,}, { 7299, 6961, 32019, 24731, 29103, 17887, 17338, 26840, 13216, 8789, 12474, 24299, 19818, 18218, 14564, 31409, 5256, 31930, 26804, 9736,} };
unsigned char global_behavior[SIZE]; int global_max;
void print() { int i; printf("%9d: ", global_max); for (i = 0; i < SIZE - 1; i++) { if (global_behavior[i] == R) { printf("R"); } else if (global_behavior[i] == L) { printf("L"); } else { fprintf(stderr, "Error\n"); exit(-1); } } puts(""); }
void dfs(int x, int y, int sum) { sum += global_mountain[y][x]; if (y + 1 == SIZE) { if (sum > global_max) { global_max = sum; print(); } return; }
global_behavior[y] = L; dfs(x, y + 1, sum);
global_behavior[y] = R; dfs(x + 1, y + 1, sum); }
int main() { global_max = 0; dfs(0, 0, 0); }
|