Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. global highest_sum;
  2. global g_path;
  3.  
  4. def is_even(num):
  5. return num % 2 == 0;
  6.  
  7. matrix=[[215,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  8. [192,124,0,0,0,0,0,0,0,0,0,0,0,0,0],
  9. [117,269,442,0,0,0,0,0,0,0,0,0,0,0,0],
  10. [218,836,347,235,0,0,0,0,0,0,0,0,0,0,0],
  11. [320,805,522,417,345,0,0,0,0,0,0,0,0,0,0],
  12. [229,601,728,835,133,124,0,0,0,0,0,0,0,0,0],
  13. [248,202,277,433,207,263,257,0,0,0,0,0,0,0,0],
  14. [359,464,504,528,516,716,871,182,0,0,0,0,0,0,0],
  15. [461,441,426,656,863,560,380,171,923,0,0,0,0,0,0],
  16. [381,348,573,533,448,632,387,176,975,449,0,0,0,0,0],
  17. [223,711,445,645,245,543,931,532,937,541,444,0,0,0,0],
  18. [330,131,333,928,376,733,17,778,839,168,197,197,0,0,0],
  19. [131,171,522,137,217,224,291,413,528,520,227,229,928,0,0],
  20. [223,626,34,683,839,52,627,310,713,999,629,817,410,121,0],
  21. [924,622,911,233,325,139,721,218,253,223,107,233,230,124,233]]
  22.  
  23. def find_highest_sum(row_index, column_index, previous_even, current_sum, path, direction):
  24. global highest_sum;
  25. global g_path;
  26. if(row_index >= len(matrix) or column_index >= len(matrix[row_index])):
  27. return;
  28. current_value = matrix[row_index][column_index];
  29. if(direction != ''):
  30. path += column_index*(' ');
  31. path += (direction) + '\n';
  32. path += column_index*(' ');
  33. path += str(current_value) + '\n';
  34. if((previous_even and is_even(current_value)) or (not previous_even and not is_even(current_value)) and row_index != 0):
  35. print('Invalid path');
  36. print(path);
  37. return;
  38. current_sum += current_value;
  39. if(current_sum > highest_sum):
  40. highest_sum = current_sum;
  41. g_path = path;
  42. find_highest_sum(row_index + 1, column_index, is_even(current_value), current_sum, path,'↓')
  43. find_highest_sum(row_index + 1, column_index + 1, is_even(current_value), current_sum, path, '↘')
  44.  
  45.  
  46. highest_sum = 0;
  47. find_highest_sum(0, 0, False, 0, '', '');
  48.  
  49. print(highest_sum)
  50. print('Best path')
  51. print(g_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement