在Java中使用回溯的4 * 4矩阵解决方案查找器,其中每一行和每一列应仅包含一个特定编号的实例

时间:2019-04-13 07:52:13

标签: java backtracking

我创建了一个4 * 4的sudoko求解器,除了一个典型的9 * 9求解器之外,但是我的程序在某些情况下无法提供预期的输出,因为在某些情况下会产生正确的输出

我尝试打印2d数组的每个状态,以查看可能发生错误的位置,并发现当检测到错误的解决方案并回溯时,需要回溯的单元会在应执行先前单元状态的地方再次执行< / p>

`

import java.util.*;
class backtrack
{
    static int a[][];
    static int b[];
    public static boolean isvalid(int r,int c,int k)//check if the no.
                                                   //can be placed in a cel
    {
      //i have tested this function and is correct
        for(int i=0;i<4;i++)
        {
            if(a[i][c]==k)
            return false;
            if(a[r][i]==k)
            return false;
        }
        if(r==0 || r==2)
        {
            if(c==0 || c==2)
            {
                if(a[r+1][c+1]==k)
                return false;
            }
            else
            {
                if(a[r+1][c-1]==k)
                return false;
            }
        }
        else
        {
            if(c==0 || c==2)
            {
                if(a[r-1][c+1]==k)
                return false;
            }
            else
            {
                if(a[r-1][c-1]==k)
                return false;
            }
        }
        return true;
    }
//this is the backtracking function to solve the sudoko
    public static boolean solve(int i)
    {
           if(i==13)//base case
           {
            return true;
           }

            for(int k=1;k<=4;k++)//this checks for all the 4 possibility
            {
               int tmp=b[i];
               int r=tmp/10;
               int c=tmp%10;
               //System.out.println("i= "+r+" j= "+c+" k= "+k);
               if(isvalid(r,c,k))
               {
                   //System.out.println("i= "+i+" j= "+j+" k= "+k);
                   a[r][c]=k;

                   if(solve(++i))
                   {
                        return true;
                   }
                   //backtracing
                    a[r][c]=0;
                }
            }
            return false;

    }
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        a=new int[4][4];//the sudoko board
        b=new int[13];//stores the paces which are empty 
        //the no. gets input like the cell index to input in the first cell
        // 00 is the input by the user likewise
        for(int i=0;i<3;i++)
        {
            System.out.println("Enter the index to add the nos");
            int ch=in.nextInt();
            System.out.println("Enter which no to be added");
            int n1=in.nextInt();

            a[ch/10][ch%10]=n1;
        }
        int co=0;
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                if(a[i][j]==0)
                {
                    b[co++]=(10*j)+i;
                }
            }

        }

        boolean x=solve(0);
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                System.out.print(a[i][j]);
            }
            System.out.println();
        }
    }
}`

输出应该是sudoko的解决方案,但是 使用以下sudoko输入: 0 0 0 0 0 0 4 0 0 3 0 0 0 0 0 1

输出不是解决方案,它的某些单元格留空

0 个答案:

没有答案