Advertisement
boris-ivanov

Untitled

Jul 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. const getGets = (arr) => {
  2. let index = 0;
  3.  
  4. return () => {
  5. const toReturn = arr[index];
  6. index += 1;
  7. return toReturn;
  8. };
  9. };
  10. // this is the test
  11. const test = [
  12. '4 3',
  13. '3 2 4',
  14. '2 0 3',
  15. '1 1 5',
  16. '2 2 5'
  17. ];
  18.  
  19. const gets = this.gets || getGets(test);
  20. const print = this.print || console.log;
  21. let rowsCols= gets().split(' ').map(Number)
  22. let matrix=[]
  23. for (let i = 0; i < rowsCols[0]; i++) {
  24. matrix.push(gets().split(' ').map(Number))
  25. }
  26.  
  27. let start
  28. let m= 0
  29. let sum =0;
  30. const max =(a,b,c,d)=>{
  31. const bobi= Math.max(a,b,c,d)
  32. return bobi
  33. }
  34. matrix.reduce((acc,ele,currInd)=>{
  35.  
  36. ele.reduce((acc,item,curInd)=>{
  37. if(item==0){
  38. while(m<10){
  39. let maxi;
  40. if(matrix[currInd+1]==undefined){
  41. maxi =max(matrix[currInd-1][curInd],ele[curInd+1],ele[curInd-1],0)
  42. }else if (matrix[currInd-1]==undefined){
  43. maxi =max(matrix[currInd+1][curInd],ele[curInd+1],ele[curInd-1],0)
  44. }else if (ele[curInd+1]==undefined){
  45. maxi=max(matrix[currInd-1][curInd],matrix[currInd+1][curInd],ele[curInd-1],0)
  46. }else if(ele[curInd-1]==undefined){
  47. maxi=max(matrix[currInd-1][curInd],matrix[currInd+1][curInd],ele[curInd+1],0)
  48. }
  49. else if(curInd==0&&currInd==0){
  50. maxi=max(matrix[currInd+1][curInd],ele[curInd+1],0,0)
  51. }else if(curInd==0&&currInd==matrix.length-1){
  52. maxi=max(matrix[currInd-1][curInd],ele[curInd+1],0,0)
  53. }else if(curInd==ele.length-1&&currInd==0){
  54. maxi=max(matrix[currInd+1][curInd],ele[curInd-1],0,0)
  55. }else if(curInd==ele.length-1&&currInd==matrix.length-1){
  56. maxi=max(matrix[currInd-1][curInd],ele[curInd-1],0,0)
  57. }else{
  58. maxi=max(matrix[currInd-1][curInd],matrix[currInd+1][curInd],ele[curInd+1],ele[curInd-1])
  59. }
  60.  
  61.  
  62. if (currInd==matrix.length-1&&curInd==ele.length-1) {
  63. if (ele[curInd-1]>=matrix[currInd-1][curInd]){
  64. sum++;
  65. curInd=curInd-1;
  66. ele[curInd]--
  67. print('left')
  68. }else if(matrix[currInd-1][curInd]>ele[curInd-1]) {
  69. sum++;
  70. currInd=currInd-1;
  71. matrix[currInd][curInd]--;
  72. print('up1')
  73. }
  74. }else
  75.  
  76. if
  77. (maxi==ele[curInd-1]){
  78. sum++;
  79. curInd=curInd-1
  80. ele[curInd]--
  81. print('left')
  82. }else
  83. if(maxi==ele[curInd+1]){
  84. sum++;
  85. curInd=curInd+1
  86. ele[curInd]--
  87. print('right')
  88.  
  89. }else if(maxi==matrix[currInd-1][curInd]){
  90. sum++
  91. currInd=currInd-1
  92. matrix[currInd][curInd]--
  93. print('up');
  94.  
  95. }else if(maxi==matrix[currInd+1][curInd]){
  96. sum++
  97. currInd=currInd+1
  98. matrix[currInd][curInd]--
  99. print('down');
  100.  
  101. }
  102. //print(maxi)
  103. //print(matrix)
  104. m++}
  105. }
  106. })
  107. })
  108. print(sum)
  109. print(matrix)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement