Java 最小成本矩阵

Java 最小成本矩阵,java,android,matrix,Java,Android,Matrix,嗨,我正在尝试计算矩阵示例2的最小成本,有输出,但示例1没有输出 路径的总成本是每个访问单元中的整数之和。解决方案需要 处理各种尺寸的网格,最少1行5列,最多10行100列 柱。如果在下一步中,总成本将超过50,则该路径将被放弃 此挑战的目的是找到成本最低的路径(即成本最低的路径) 任何可能路径的总成本)。通过两个略有不同的5 x 6网格的成本最低的路径 如下所示。栅格值仅在底部行中不同。轴上栅格的路径 右图利用了第一行和最后一行之间的邻接关系 但只有在第一个例子中才显示出错误。它正在显示 是

嗨,我正在尝试计算矩阵示例2的最小成本,有输出,但示例1没有输出

路径的总成本是每个访问单元中的整数之和。解决方案需要 处理各种尺寸的网格,最少1行5列,最多10行100列 柱。如果在下一步中,总成本将超过50,则该路径将被放弃

此挑战的目的是找到成本最低的路径(即成本最低的路径) 任何可能路径的总成本)。通过两个略有不同的5 x 6网格的成本最低的路径 如下所示。栅格值仅在底部行中不同。轴上栅格的路径 右图利用了第一行和最后一行之间的邻接关系

但只有在第一个例子中才显示出错误。它正在显示

是
18
121222
但是我需要像
Yes 16 123445
这样的输出

public class MainActivity extends AppCompatActivity {

    private TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findPath();
    }

    private void findPath() {
        TextView txtMatrixDisplay = (TextView) findViewById(R.id.textView_display);

        // 1st example
        int[][] matrix1 = new int[][]{
                { 3, 4, 1, 2, 8, 6 },
                { 6, 1, 8, 2, 7, 4 },
                { 5, 9, 3, 9, 9, 5 },
                { 8, 4, 1, 3, 2, 6 },
                { 3, 7, 2, 8, 6, 4 } };

        String txtMatrix1 = "";
        for(int i=0; i<matrix1.length; i++){
            for(int j=0; j<matrix1[0].length; j++){
                txtMatrix1 += matrix1[i][j] + " ";
            }
            txtMatrix1 += "\n";
        }
        txtMatrixDisplay.setText(txtMatrix1);

        Solution firstExample = new Solution();
        firstExample.Path(matrix1);
        String result1 = "\n";
        if(firstExample.getStat()){
            result1 += "Yes\n";
        } else {
            result1 += "No\n";
        }

        result1 += firstExample.getCost() + "\n";
        result1 += firstExample.getPath() + "\n";
        txtMatrixDisplay.append(result1);

        // 2nd example
        int[][] matrix2 = new int[][]{
                {3, 4, 1, 2, 8, 6 },
                {6, 1, 8, 2, 7, 4 },
                {5, 9, 3, 9, 9, 5 },
                {8, 4, 1, 3, 2, 6 },
                {3, 7, 2, 1, 2, 3 } };

        String txtMatrix2 = "\n";
        for(int i=0; i<matrix2.length; i++){
            for(int j=0; j<matrix2[0].length; j++){
                txtMatrix2 += matrix2[i][j] + " ";
            }
            txtMatrix2 += "\n";
        }
        txtMatrixDisplay.append(txtMatrix2);

        Solution secondExample = new Solution();
        secondExample.Path(matrix2);
        String result2 = "\n";
        if(secondExample.getStat()){
            result2 += "Yes\n";
        } else {
            result2 += "No\n";
        }

        result2 += secondExample.getCost() + "\n";
        result2 += secondExample.getPath() + "\n";
        txtMatrixDisplay.append(result2);

        // 1st example
        int[][] matrix3 = new int[][]{
                {19, 10, 19, 10, 19 },
                {21, 23, 20, 19, 12 },
                {20, 12, 20, 11, 10 } };

        String txtMatrix3 = "\n";
        for(int i=0; i<matrix3.length; i++){
            for(int j=0; j<matrix3[0].length; j++){
                txtMatrix3 += matrix3[i][j] + " ";
            }
            txtMatrix3 += "\n";
        }
        txtMatrixDisplay.append(txtMatrix3);

        Solution thirdExample = new Solution();
        thirdExample.Path(matrix3);
        String result3 = "\n";
        if(thirdExample.getStat()){
            result3 += "Yes\n";
        } else {
            result3 += "No\n";
        }

        result3 += thirdExample.getCost() + "\n";
        result3 += thirdExample.getPath();
        txtMatrixDisplay.append(result3);
    }
}
public类MainActivity扩展了AppCompatActivity{
私有文本视图显示;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findPath();
}
私有void findPath(){
TextView txtMatrixDisplay=(TextView)findViewById(R.id.TextView\u显示);
//第一个例子
int[][]矩阵1=新int[][]{
{ 3, 4, 1, 2, 8, 6 },
{ 6, 1, 8, 2, 7, 4 },
{ 5, 9, 3, 9, 9, 5 },
{ 8, 4, 1, 3, 2, 6 },
{ 3, 7, 2, 8, 6, 4 } };
字符串txtMatrix1=“”;
对于(int i=0;i