N королев - Java - PullRequest
       74

N королев - Java

0 голосов
/ 20 апреля 2020

Я сам решал вопрос о N ферзях и использовал другой подход, который был упомянут в решении, а также в сети.

Мой код работает для входов до 4, но начинает печатать каждый случай ( даже те, которые не верны) для любого значения после 4. Я проверял это много раз, но я не могу найти ни одной ошибки в коде.

PFA код и посмотреть, можете ли вы найти ошибку , Спасибо!

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        Scanner scn = new Scanner(System.in);
        int n= scn.nextInt();
        int[][] arr = new int[n][n];
        printNQueens(arr,"",0);
        System.out.println();


    }

    public static void printNQueens(int[][] chess, String qsf, int row) {
        if(row==chess.length)
        {
            qsf = qsf + ".";
            System.out.println(qsf);
            return;
        }
        for(int j=0;j<chess[0].length;j++)
        {
            if(chess[row][j]==0)
            {
                int x=row,y=j;
                while(x<chess.length)
                {
                    chess[x][y] = 1;
                    x++;
                }
                x = row;
                while(x<chess.length && y>=0)
                {
                    chess[x][y] = 1;
                    x++;
                    y--;
                }
                x = row;
                y = j;
                 while(x<chess.length && y<chess[0].length)
                {
                    chess[x][y] = 1;
                    x++;
                    y++;
                }
                printNQueens(chess,qsf + row + "-" + j + ", ",row+1);
                x = row;
                y = j;
                 while(x<chess.length)
                {
                    chess[x][y] = 0;
                    x++;
                }
                x = row;
                while(x<chess.length && y>=0)
                {
                    chess[x][y] = 0;
                    x++;
                    y--;
                }
                x = row;
                y = j;
                 while(x<chess.length && y<chess[0].length)
                {
                    chess[x][y] = 0;
                    x++;
                    y++;
                }

            }
        }
    }
}

1 Ответ

0 голосов
/ 22 апреля 2020
import java.io.*;

import java .util. *;

publi c class Main {

public static void main(String[] args) throws Exception {
    Scanner scn = new Scanner(System.in);
    int n= scn.nextInt();
    int[][] arr = new int[n][n];
    printNQueens(arr,"",0);
    System.out.println();


}

public static void printNQueens(int[][] chess, String qsf, int row) {
    if(row==chess.length)
    {
        qsf = qsf + ".";
        System.out.println(qsf);
        return;
    }
        for(int j=0;j<chess[0].length;j++)
        {
            if(chess[row][j]==0)
            {
                chess[row][j] = 1;
                ArrayList<Integer> ro = new ArrayList<>();
                ArrayList<Integer> co = new ArrayList<>();
                int x=row+1,y=j;
                while(x<chess.length)
                {
                    if(chess[x][y]==1)
                    {
                        ro.add(x);
                        co.add(y);
                    }
                    chess[x][y] = 1;
                    x++;
                }
                x = row+1;
                y = j - 1;
                while(x<chess.length && y>=0)
                {
                    if(chess[x][y]==1)
                    {
                        ro.add(x);
                        co.add(y);
                    }
                    chess[x][y] = 1;
                    x++;
                    y--;
                }
                x = row+1;
                y = j+1;
                 while(x<chess.length && y<chess[0].length)
                {
                    if(chess[x][y]==1)
                    {
                        ro.add(x);
                        co.add(y);
                    }
                    chess[x][y] = 1;
                    x++;
                    y++;
                }
                printNQueens(chess,qsf + row + "-" + j + ", ",row+1);
                x = row;
                y = j;
                 while(x<chess.length)
                {
                    chess[x][y] = 0;
                    x++;
                }
                x = row;
                while(x<chess.length && y>=0)
                {
                    chess[x][y] = 0;
                    x++;
                    y--;
                }
                x = row;
                y = j;
                 while(x<chess.length && y<chess[0].length)
                {
                    chess[x][y] = 0;
                    x++;
                    y++;
                }
                x = 0;
                y = 0;
                while(x<ro.size() && y<co.size())
                {
                    chess[ro.get(x)][co.get(y)] = 1;
                    x++;
                    y++;
                }

            }
        }


}

}

...