Gold Mine Problem

Here given code implementation process.

// Java Program 
// Gold Mine Problem
public class GoldMineProblem
{
	public void maxGold(int[][] goldMine)
	{
		int row = goldMine.length;
		int col = goldMine[0].length;
		int max = 0;
		for (int i = 1; i < col; ++i)
		{
			for (int j = 0; j < row; ++j)
			{
				max = Integer.MIN_VALUE;
				if (goldMine[j][i - 1] + goldMine[j][i] > max)
				{
					// Left
					max = goldMine[j][i - 1] + goldMine[j][i];
				}
				if (j - 1 >= 0 && goldMine[j - 1][i - 1] + goldMine[j][i] > max)
				{
					// Top Left
					max = goldMine[j - 1][i - 1] + goldMine[j][i];
				}
				if (j + 1 < row && goldMine[j + 1][i - 1] + goldMine[j][i] > max)
				{
					// Botoom Left
					max = goldMine[j + 1][i - 1] + goldMine[j][i];
				}
				goldMine[j][i] = max;
			}
		}
		// Check result in last column
		max = goldMine[0][col - 1];
		for (int i = 1; i < row; ++i)
		{
			if (max < goldMine[i][col - 1])
			{
				max = goldMine[i][col - 1];
			}
		}
		System.out.println(max);
	}
	public static void main(String[] args)
	{
		GoldMineProblem task = new GoldMineProblem();
		int[][] matrix = {
			{
				14 , 5 , 6 , 9 , 3
			},
			{
				12 , 9 , 7 , 10 , 2
			},
			{
				15 , 6 , 8 , 3 , 1
			},
			{
				13 , 8 , 9 , 4 , 2
			}
		};
		// [15 + 9 + 8 + 10 + 3] = 45
		task.maxGold(matrix);
	}
}

Output

45
// Include header file
#include <iostream>
#include <vector>
#include <limits.h>

using namespace std;
// C++ Program 
// Gold Mine Problem
class GoldMineProblem
{
	public: void maxGold(vector < vector < int >> goldMine)
	{
		int row = goldMine.size();
		int col = goldMine[0].size();
		int max = 0;
		for (int i = 1; i < col; ++i)
		{
			for (int j = 0; j < row; ++j)
			{
				max = INT_MIN;
				if (goldMine[j][i - 1] + goldMine[j][i] > max)
				{
					// Left
					max = goldMine[j][i - 1] + goldMine[j][i];
				}
				if (j - 1 >= 0 && goldMine[j - 1][i - 1] + goldMine[j][i] > max)
				{
					// Top Left
					max = goldMine[j - 1][i - 1] + goldMine[j][i];
				}
				if (j + 1 < row && goldMine[j + 1][i - 1] + goldMine[j][i] > max)
				{
					// Botoom Left
					max = goldMine[j + 1][i - 1] + goldMine[j][i];
				}
				goldMine[j][i] = max;
			}
		}
		// Check result in last column
		max = goldMine[0][col - 1];
		for (int i = 1; i < row; ++i)
		{
			if (max < goldMine[i][col - 1])
			{
				max = goldMine[i][col - 1];
			}
		}
		cout << max << endl;
	}
};
int main()
{
	GoldMineProblem *task = new GoldMineProblem();
	vector < vector < int >> matrix
	{
		{
			14 , 5 , 6 , 9 , 3
		} , {
			12 , 9 , 7 , 10 , 2
		} , {
			15 , 6 , 8 , 3 , 1
		} , {
			13 , 8 , 9 , 4 , 2
		}
	};
	// [15 + 9 + 8 + 10 + 3] = 45
	task->maxGold(matrix);
	return 0;
}

Output

45
// Include namespace system
using System;
// Csharp Program 
// Gold Mine Problem
public class GoldMineProblem
{
	public void maxGold(int[,] goldMine)
	{
		int row = goldMine.GetLength(0);
		int col = goldMine.GetLength(1);
		int max = 0;
		for (int i = 1; i < col; ++i)
		{
			for (int j = 0; j < row; ++j)
			{
				max = int.MinValue;
				if (goldMine[j,i - 1] + goldMine[j,i] > max)
				{
					// Left
					max = goldMine[j,i - 1] + goldMine[j,i];
				}
				if (j - 1 >= 0 && goldMine[j - 1,i - 1] + goldMine[j,i] > max)
				{
					// Top Left
					max = goldMine[j - 1,i - 1] + goldMine[j,i];
				}
				if (j + 1 < row && goldMine[j + 1,i - 1] + goldMine[j,i] > max)
				{
					// Botoom Left
					max = goldMine[j + 1,i - 1] + goldMine[j,i];
				}
				goldMine[j,i] = max;
			}
		}
		// Check result in last column
		max = goldMine[0,col - 1];
		for (int i = 1; i < row; ++i)
		{
			if (max < goldMine[i,col - 1])
			{
				max = goldMine[i,col - 1];
			}
		}
		Console.WriteLine(max);
	}
	public static void Main(String[] args)
	{
		GoldMineProblem task = new GoldMineProblem();
		int[,] matrix = {
			{
				14 , 5 , 6 , 9 , 3
			},
			{
				12 , 9 , 7 , 10 , 2
			},
			{
				15 , 6 , 8 , 3 , 1
			},
			{	
				13 , 8 , 9 , 4 , 2
			}
		};
		// [15 + 9 + 8 + 10 + 3] = 45
		task.maxGold(matrix);
	}
}

Output

45
package main
import "math"
import "fmt"
// Go Program 
// Gold Mine Problem
type GoldMineProblem struct {}
func getGoldMineProblem() * GoldMineProblem {
	var me *GoldMineProblem = &GoldMineProblem {}
	return me
}
func(this GoldMineProblem) maxGold(goldMine[][] int) {
	var row int = len(goldMine)
	var col int = len(goldMine[0])
	var max int = 0
	for i := 1 ; i < col ; i++ {
		for j := 0 ; j < row ; j++ {
			max = math.MinInt64
			if goldMine[j][i - 1] + goldMine[j][i] > max {
				// Left
				max = goldMine[j][i - 1] + goldMine[j][i]
			}
			if j - 1 >= 0 && goldMine[j - 1][i - 1] + goldMine[j][i] > max {
				// Top Left
				max = goldMine[j - 1][i - 1] + goldMine[j][i]
			}
			if j + 1 < row && goldMine[j + 1][i - 1] + goldMine[j][i] > max {
				// Botoom Left
				max = goldMine[j + 1][i - 1] + goldMine[j][i]
			}
			goldMine[j][i] = max
		}
	}
	// Check result in last column
	max = goldMine[0][col - 1]
	for i := 1 ; i < row ; i++ {
		if max < goldMine[i][col - 1] {
			max = goldMine[i][col - 1]
		}
	}
	fmt.Println(max)
}
func main() {
	var task * GoldMineProblem = getGoldMineProblem()
	var matrix = [][] int   {
	    { 14 , 5 , 6 , 9 , 3 },
		{ 12 , 9 , 7 , 10 , 2 },
		{ 15 , 6 , 8 , 3 , 1 },
		{ 13 , 8 , 9 , 4 , 2 },
	}
	// [15 + 9 + 8 + 10 + 3] = 45
	task.maxGold(matrix)
}

Output

45
<?php
// Php Program 
// Gold Mine Problem
class GoldMineProblem
{
	public	function maxGold($goldMine)
	{
		$row = count($goldMine);
		$col = count($goldMine[0]);
		$max = 0;
		for ($i = 1; $i < $col; ++$i)
		{
			for ($j = 0; $j < $row; ++$j)
			{
				$max = -PHP_INT_MAX;
				if ($goldMine[$j][$i - 1] + $goldMine[$j][$i] > $max)
				{
					// Left
					$max = $goldMine[$j][$i - 1] + $goldMine[$j][$i];
				}
				if ($j - 1 >= 0 && $goldMine[$j - 1][$i - 1] + 
                    $goldMine[$j][$i] > $max)
				{
					// Top Left
					$max = $goldMine[$j - 1][$i - 1] + $goldMine[$j][$i];
				}
				if ($j + 1 < $row && $goldMine[$j + 1][$i - 1] + 
                    $goldMine[$j][$i] > $max)
				{
					// Botoom Left
					$max = $goldMine[$j + 1][$i - 1] + $goldMine[$j][$i];
				}
				$goldMine[$j][$i] = $max;
			}
		}
		// Check result in last column
		$max = $goldMine[0][$col - 1];
		for ($i = 1; $i < $row; ++$i)
		{
			if ($max < $goldMine[$i][$col - 1])
			{
				$max = $goldMine[$i][$col - 1];
			}
		}
		echo($max.
			"\n");
	}
}

function main()
{
	$task = new GoldMineProblem();
	$matrix = array(
      array(14, 5, 6, 9, 3), 
      array(12, 9, 7, 10, 2), 
      array(15, 6, 8, 3, 1), 
      array(13, 8, 9, 4, 2)
    );
	// [15 + 9 + 8 + 10 + 3] = 45
	$task->maxGold($matrix);
}
main();

Output

45
// Node JS Program 
// Gold Mine Problem
class GoldMineProblem
{
	maxGold(goldMine)
	{
		var row = goldMine.length;
		var col = goldMine[0].length;
		var max = 0;
		for (var i = 1; i < col; ++i)
		{
			for (var j = 0; j < row; ++j)
			{
				max = -Number.MAX_VALUE;
				if (goldMine[j][i - 1] + goldMine[j][i] > max)
				{
					// Left
					max = goldMine[j][i - 1] + goldMine[j][i];
				}
				if (j - 1 >= 0 && goldMine[j - 1][i - 1] + goldMine[j][i] > max)
				{
					// Top Left
					max = goldMine[j - 1][i - 1] + goldMine[j][i];
				}
				if (j + 1 < row && goldMine[j + 1][i - 1] + goldMine[j][i] > max)
				{
					// Botoom Left
					max = goldMine[j + 1][i - 1] + goldMine[j][i];
				}
				goldMine[j][i] = max;
			}
		}
		// Check result in last column
		max = goldMine[0][col - 1];
		for (var i = 1; i < row; ++i)
		{
			if (max < goldMine[i][col - 1])
			{
				max = goldMine[i][col - 1];
			}
		}
		console.log(max);
	}
}

function main()
{
	var task = new GoldMineProblem();
	var matrix = [
		[14, 5, 6, 9, 3],
		[12, 9, 7, 10, 2],
		[15, 6, 8, 3, 1],
		[13, 8, 9, 4, 2]
	];
	// [15 + 9 + 8 + 10 + 3] = 45
	task.maxGold(matrix);
}
main();

Output

45
import sys
#  Python 3 Program 
#  Gold Mine Problem
class GoldMineProblem :
	def maxGold(self, goldMine) :
		row = len(goldMine)
		col = len(goldMine[0])
		max = 0
		i = 1
		while (i < col) :
			j = 0
			while (j < row) :
				max = -sys.maxsize
				if (goldMine[j][i - 1] + goldMine[j][i] > max) :
					#  Left
					max = goldMine[j][i - 1] + goldMine[j][i]
				
				if (j - 1 >= 0 and goldMine[j - 1][i - 1] + 
                  goldMine[j][i] > max) :
					#  Top Left
					max = goldMine[j - 1][i - 1] + goldMine[j][i]
				
				if (j + 1 < row and goldMine[j + 1][i - 1] + 
                  goldMine[j][i] > max) :
					#  Botoom Left
					max = goldMine[j + 1][i - 1] + goldMine[j][i]
				
				goldMine[j][i] = max
				j += 1
			
			i += 1
		
		#  Check result in last column
		max = goldMine[0][col - 1]
		i = 1
		while (i < row) :
			if (max < goldMine[i][col - 1]) :
				max = goldMine[i][col - 1]
			
			i += 1
		
		print(max)
	

def main() :
	task = GoldMineProblem()
	matrix = [
		[14, 5, 6, 9, 3],
		[12, 9, 7, 10, 2],
		[15, 6, 8, 3, 1],
		[13, 8, 9, 4, 2]
	]
	#  [15 + 9 + 8 + 10 + 3] = 45
	task.maxGold(matrix)

if __name__ == "__main__": main()

Output

45
#  Ruby Program 
#  Gold Mine Problem
class GoldMineProblem 
	def maxGold(goldMine) 
		row = goldMine.length
		col = goldMine[0].length
		max = 0
		i = 1
		while (i < col) 
			j = 0
			while (j < row) 
				max = -(2 ** (0. size * 8 - 2))
				if (goldMine[j][i - 1] + goldMine[j][i] > max) 
					#  Left
					max = goldMine[j][i - 1] + goldMine[j][i]
				end

				if (j - 1 >= 0 && goldMine[j - 1][i - 1] + 
                    goldMine[j][i] > max) 
					#  Top Left
					max = goldMine[j - 1][i - 1] + goldMine[j][i]
				end

				if (j + 1 < row && goldMine[j + 1][i - 1] + 
                    goldMine[j][i] > max) 
					#  Botoom Left
					max = goldMine[j + 1][i - 1] + goldMine[j][i]
				end

				goldMine[j][i] = max
				j += 1
			end

			i += 1
		end

		#  Check result in last column
		max = goldMine[0][col - 1]
		i = 1
		while (i < row) 
			if (max < goldMine[i][col - 1]) 
				max = goldMine[i][col - 1]
			end

			i += 1
		end

		print(max, "\n")
	end

end

def main() 
	task = GoldMineProblem.new()
	matrix = [
		[14, 5, 6, 9, 3],
		[12, 9, 7, 10, 2],
		[15, 6, 8, 3, 1],
		[13, 8, 9, 4, 2]
	]
	#  [15 + 9 + 8 + 10 + 3] = 45
	task.maxGold(matrix)
end

main()

Output

45
// Scala Program 
// Gold Mine Problem
class GoldMineProblem()
{
	def maxGold(goldMine: Array[Array[Int]]): Unit = {
		var row: Int = goldMine.length;
		var col: Int = goldMine(0).length;
		var max: Int = 0;
		var i: Int = 1;
		while (i < col)
		{
			var j: Int = 0;
			while (j < row)
			{
				max = Int.MinValue;
				if (goldMine(j)(i - 1) + goldMine(j)(i) > max)
				{
					// Left
					max = goldMine(j)(i - 1) + goldMine(j)(i);
				}
				if (j - 1 >= 0 && goldMine(j - 1)(i - 1) + 
                    goldMine(j)(i) > max)
				{
					// Top Left
					max = goldMine(j - 1)(i - 1) + goldMine(j)(i);
				}
				if (j + 1 < row && goldMine(j + 1)(i - 1) + 
                    goldMine(j)(i) > max)
				{
					// Botoom Left
					max = goldMine(j + 1)(i - 1) + goldMine(j)(i);
				}
				goldMine(j)(i) = max;
				j += 1;
			}
			i += 1;
		}
		// Check result in last column
		max = goldMine(0)(col - 1);
		i = 1;
		while (i < row)
		{
			if (max < goldMine(i)(col - 1))
			{
				max = goldMine(i)(col - 1);
			}
			i += 1;
		}
		println(max);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: GoldMineProblem = new GoldMineProblem();
		var matrix: Array[Array[Int]] = Array(
          Array(14, 5, 6, 9, 3), 
          Array(12, 9, 7, 10, 2), 
          Array(15, 6, 8, 3, 1), 
          Array(13, 8, 9, 4, 2)
        );
		// [15 + 9 + 8 + 10 + 3] = 45
		task.maxGold(matrix);
	}
}

Output

45
import Foundation;
// Swift 4 Program 
// Gold Mine Problem
class GoldMineProblem
{
	func maxGold(_ goldMine: inout[[Int]])
	{
		let row: Int = goldMine.count;
		let col: Int = goldMine[0].count;
		var max: Int = 0;
		var i: Int = 1;
		while (i < col)
		{
			var j: Int = 0;
			while (j < row)
			{
				max = Int.min;
				if (goldMine[j][i - 1] + goldMine[j][i] > max)
				{
					// Left
					max = goldMine[j][i - 1] + goldMine[j][i];
				}
				if (j - 1 >= 0 && goldMine[j - 1][i - 1] + 
                    goldMine[j][i] > max)
				{
					// Top Left
					max = goldMine[j - 1][i - 1] + goldMine[j][i];
				}
				if (j + 1 < row && goldMine[j + 1][i - 1] + 
                    goldMine[j][i] > max)
				{
					// Botoom Left
					max = goldMine[j + 1][i - 1] + goldMine[j][i];
				}
				goldMine[j][i] = max;
				j += 1;
			}
			i += 1;
		}
		// Check result in last column
		max = goldMine[0][col - 1];
		i = 1;
		while (i < row)
		{
			if (max < goldMine[i][col - 1])
			{
				max = goldMine[i][col - 1];
			}
			i += 1;
		}
		print(max);
	}
}
func main()
{
	let task: GoldMineProblem = GoldMineProblem();
	var matrix: [[Int]] = [
		[14, 5, 6, 9, 3],
		[12, 9, 7, 10, 2],
		[15, 6, 8, 3, 1],
		[13, 8, 9, 4, 2]
	];
	// [15 + 9 + 8 + 10 + 3] = 45
	task.maxGold(&matrix);
}
main();

Output

45
// Kotlin Program 
// Gold Mine Problem
class GoldMineProblem
{
	fun maxGold(goldMine: Array < Array < Int >> ): Unit
	{
		val row: Int = goldMine.count();
		val col: Int = goldMine[0].count();
		var max: Int;
		var i: Int = 1;
		while (i < col)
		{
			var j: Int = 0;
			while (j < row)
			{
				max = Int.MIN_VALUE;
				if (goldMine[j][i - 1] + goldMine[j][i] > max)
				{
					// Left
					max = goldMine[j][i - 1] + goldMine[j][i];
				}
				if (j - 1 >= 0 && goldMine[j - 1][i - 1] + 
                    goldMine[j][i] > max)
				{
					// Top Left
					max = goldMine[j - 1][i - 1] + goldMine[j][i];
				}
				if (j + 1 < row && goldMine[j + 1][i - 1] + 
                    goldMine[j][i] > max)
				{
					// Botoom Left
					max = goldMine[j + 1][i - 1] + goldMine[j][i];
				}
				goldMine[j][i] = max;
				j += 1;
			}
			i += 1;
		}
		// Check result in last column
		max = goldMine[0][col - 1];
		i = 1;
		while (i < row)
		{
			if (max < goldMine[i][col - 1])
			{
				max = goldMine[i][col - 1];
			}
			i += 1;
		}
		println(max);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: GoldMineProblem = GoldMineProblem();
	val matrix: Array < Array < Int >> = arrayOf(
      arrayOf(14, 5, 6, 9, 3), 
      arrayOf(12, 9, 7, 10, 2), 
      arrayOf(15, 6, 8, 3, 1), 
      arrayOf(13, 8, 9, 4, 2)
    );
	// [15 + 9 + 8 + 10 + 3] = 45
	task.maxGold(matrix);
}

Output

45

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved