Guest User

Untitled

a guest
Aug 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. # include <iostream>
  2. # include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. enum Color { red = 0, green = 1, blue = 2 };
  7. int d[1001][3];
  8. int rgb[1001][3];
  9.  
  10. //get minimum value from previous answer
  11. //Is there any way to improve this code?
  12. int minValue(int idx, Color exclude) {
  13. Color c1, c2;
  14. if (exclude == red) {
  15. c1 = green;
  16. c2 = blue;
  17. }
  18. else if (exclude == green) {
  19. c1 = red;
  20. c2 = blue;
  21. }
  22. else {
  23. c1 = red;
  24. c2 = green;
  25. }
  26. return min(d[idx][c1], d[idx][c2]);
  27. }
  28.  
  29. int main() {
  30. int n;
  31. cin >> n;
  32. for (int i = 1; i <= n; i++) {
  33. cin >> rgb[i][red] >> rgb[i][green] >> rgb[i][blue];
  34. }
  35.  
  36. d[1][red] = rgb[1][red];
  37. d[1][green] = rgb[1][green];
  38. d[1][blue] = rgb[1][blue];
  39.  
  40. for (int i = 2; i <= n; i++) {
  41. for (int j = red; j <= blue; j++) {
  42. Color color = static_cast<Color>(j);
  43. d[i][color] = minValue(i - 1, color) + rgb[i][color];
  44. }
  45. }
  46.  
  47. int ans = d[n][red];
  48. ans = d[n][blue] < ans ? d[n][blue] : ans;
  49. ans = d[n][green] < ans ? d[n][green] : ans;
  50. cout << ans << endl;
  51. return 0;
  52. }
Add Comment
Please, Sign In to add comment