fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8.  
  9. class PourWater {
  10. public void pourWater(int[] heights, int water, int location) {
  11. int[] waters = new int[heights.length];
  12. int pourLocation;
  13.  
  14. while (water > 0) {
  15. int left = location - 1;
  16. while (left >= 0) {
  17. if (heights[left] + waters[left] > heights[left + 1] + waters[left + 1]) {
  18. break;
  19. }
  20. left--;
  21. }
  22. if (heights[left + 1] + waters[left + 1] < heights[location] + waters[location]) {
  23. pourLocation = left + 1;
  24. waters[pourLocation]++;
  25. water--;
  26. continue;
  27. }
  28.  
  29. int right = location + 1;
  30. while (right < heights.length) {
  31. if (heights[right] + waters[right] > heights[right - 1] + waters[right - 1]) {
  32. break;
  33. }
  34. right++;
  35. }
  36. if (heights[right - 1] + waters[right - 1] < heights[location] + waters[location]) {
  37. pourLocation = right - 1;
  38. waters[pourLocation]++;
  39. water--;
  40. continue;
  41. }
  42.  
  43. pourLocation = location;
  44. waters[pourLocation]++;
  45. water--;
  46. }
  47.  
  48. print(heights, waters);
  49. }
  50.  
  51. private void print(int[] heights, int[] waters) {
  52. int n = heights.length;
  53.  
  54. int maxHeight = 0;
  55. for (int i = 0; i < n; i++) {
  56. maxHeight = Math.max(maxHeight, heights[i] + waters[i]);
  57. }
  58.  
  59. for (int height = maxHeight; height >= 0; height--) {
  60. for (int i = 0; i < n; i++) {
  61. if (height <= heights[i]) {
  62. System.out.print("+");
  63. } else if (height > heights[i] && height <= heights[i] + waters[i]) {
  64. System.out.print("W");
  65. } else {
  66. System.out.print(" ");
  67. }
  68. }
  69. System.out.println();
  70. }
  71. System.out.println();
  72. }
  73. }
  74.  
  75. public class Main {
  76. public static void main(String[] args) {
  77. PourWater pw = new PourWater();
  78. int[] heights = {5,4,2,1,2,3,2,1,0,1,2,4};
  79. for (int i = 1; i <= 9; i++) {
  80. pw.pourWater(heights, i, 5);
  81. }
  82. }
  83. }
  84.  
Success #stdin #stdout 0.06s 2184192KB
stdin
Standard input is empty
stdout
+           
++         +
++   +     +
+++W+++   ++
++++++++ +++
++++++++++++

+           
++         +
++W  +     +
+++W+++   ++
++++++++ +++
++++++++++++

+           
++         +
++WW +     +
+++W+++   ++
++++++++ +++
++++++++++++

+           
++         +
++WWW+     +
+++W+++   ++
++++++++ +++
++++++++++++

+           
++         +
++WWW+     +
+++W+++   ++
++++++++W+++
++++++++++++

+           
++         +
++WWW+     +
+++W+++  W++
++++++++W+++
++++++++++++

+           
++         +
++WWW+     +
+++W+++ WW++
++++++++W+++
++++++++++++

+           
++         +
++WWW+     +
+++W+++WWW++
++++++++W+++
++++++++++++

+           
++         +
++WWW+    W+
+++W+++WWW++
++++++++W+++
++++++++++++