Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getGets = (arr) => {
- let index = 0;
- return () => {
- const toReturn = arr[index];
- index += 1;
- return toReturn;
- };
- };
- // this is the test
- const test = [
- '4 3',
- '3 2 4',
- '2 0 3',
- '1 1 5',
- '2 2 5'
- ];
- const gets = this.gets || getGets(test);
- const print = this.print || console.log;
- let rowsCols= gets().split(' ').map(Number)
- let matrix=[]
- for (let i = 0; i < rowsCols[0]; i++) {
- matrix.push(gets().split(' ').map(Number))
- }
- let start
- let m= 0
- let sum =0;
- const max =(a,b,c,d)=>{
- const bobi= Math.max(a,b,c,d)
- return bobi
- }
- matrix.reduce((acc,ele,currInd)=>{
- ele.reduce((acc,item,curInd)=>{
- if(item==0){
- while(m<10){
- let maxi;
- if(matrix[currInd+1]==undefined){
- maxi =max(matrix[currInd-1][curInd],ele[curInd+1],ele[curInd-1],0)
- }else if (matrix[currInd-1]==undefined){
- maxi =max(matrix[currInd+1][curInd],ele[curInd+1],ele[curInd-1],0)
- }else if (ele[curInd+1]==undefined){
- maxi=max(matrix[currInd-1][curInd],matrix[currInd+1][curInd],ele[curInd-1],0)
- }else if(ele[curInd-1]==undefined){
- maxi=max(matrix[currInd-1][curInd],matrix[currInd+1][curInd],ele[curInd+1],0)
- }
- else if(curInd==0&&currInd==0){
- maxi=max(matrix[currInd+1][curInd],ele[curInd+1],0,0)
- }else if(curInd==0&&currInd==matrix.length-1){
- maxi=max(matrix[currInd-1][curInd],ele[curInd+1],0,0)
- }else if(curInd==ele.length-1&&currInd==0){
- maxi=max(matrix[currInd+1][curInd],ele[curInd-1],0,0)
- }else if(curInd==ele.length-1&&currInd==matrix.length-1){
- maxi=max(matrix[currInd-1][curInd],ele[curInd-1],0,0)
- }else{
- maxi=max(matrix[currInd-1][curInd],matrix[currInd+1][curInd],ele[curInd+1],ele[curInd-1])
- }
- if (currInd==matrix.length-1&&curInd==ele.length-1) {
- if (ele[curInd-1]>=matrix[currInd-1][curInd]){
- sum++;
- curInd=curInd-1;
- ele[curInd]--
- print('left')
- }else if(matrix[currInd-1][curInd]>ele[curInd-1]) {
- sum++;
- currInd=currInd-1;
- matrix[currInd][curInd]--;
- print('up1')
- }
- }else
- if
- (maxi==ele[curInd-1]){
- sum++;
- curInd=curInd-1
- ele[curInd]--
- print('left')
- }else
- if(maxi==ele[curInd+1]){
- sum++;
- curInd=curInd+1
- ele[curInd]--
- print('right')
- }else if(maxi==matrix[currInd-1][curInd]){
- sum++
- currInd=currInd-1
- matrix[currInd][curInd]--
- print('up');
- }else if(maxi==matrix[currInd+1][curInd]){
- sum++
- currInd=currInd+1
- matrix[currInd][curInd]--
- print('down');
- }
- //print(maxi)
- //print(matrix)
- m++}
- }
- })
- })
- print(sum)
- print(matrix)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement