using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
public static int minsum = 0, count = 0;
public static int[] pathrow = new int[100];
public static int[] pathcol = new int[100];
public static int[] finalpathrow = new int[100];
public static int[] finalpathcol = new int[100];
static void Main(string[] args)
{
int[,] arr ={
{1,2,9},
{4,5,6},
{7,8,-1}
};
fun(arr, 0, 0, 0, 0);
for (int i = 0; i < arr.GetLength(1); i++)
{
Console.Write(arr[finalpathrow[i], finalpathcol[i]]);
Console.Write(" ");
}
Console.WriteLine();
Console.WriteLine(minsum);
}
public static void fun(int[,]arr, int row, int col, int thissum, int ele)
{
if(col==arr.GetLength(1))
{
if (count == 0)
{
minsum = thissum;
for (int i = 0; i < arr.GetLength(1); i++)
{
finalpathrow[i] = pathrow[i];
finalpathcol[i] = pathcol[i];
}
count++;
}
else if (thissum < minsum)
{
minsum = thissum;
for (int i = 0; i < arr.GetLength(1); i++)
{
finalpathrow[i] = pathrow[i];
finalpathcol[i] = pathcol[i];
}
}
return;
}
else if (row == -1 || row == arr.GetLength(0))
{
if (row == -1)
{
row = arr.GetLength(0) - 1;
thissum = thissum + arr[row, col];
pathrow[ele] = row;
pathcol[ele] = col;
fun(arr, row + 1, col + 1, thissum, ele + 1);
fun(arr, row, col + 1, thissum, ele + 1);
fun(arr, row - 1, col + 1, thissum, ele + 1);
}
else
{
row = 0;
thissum = thissum + arr[row, col];
pathrow[ele] = row;
pathcol[ele] = col;
fun(arr, row + 1, col + 1, thissum, ele + 1);
fun(arr, row, col + 1, thissum, ele + 1);
fun(arr, row - 1, col + 1, thissum, ele + 1);
}
}
else
{
thissum = thissum + arr[row,col];
pathrow[ele] = row;
pathcol[ele] = col;
fun(arr, row + 1, col + 1, thissum, ele + 1);
fun(arr, row, col + 1, thissum, ele + 1);
fun(arr, row - 1, col + 1, thissum, ele + 1);
}
}
}
}
No comments:
Post a Comment