Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C# by asd ( 5 years ago )
using System;
using System.Globalization;
using System.Collections.Generic;
namespace CChris
{
class Program
{
static bool solve(string[,] b,int n,int col = 0)
{
if (col >= n)
{
return true;
}
for (int y = 0;y < n; y++)
{
b[y, col] = "1";
if (checkBoard(b,n) == false)
{
b[y, col] = "0";
}
else
{
if (solve(b, n, col + 1) == true)
{
return true;
}
else
{
b[y, col] = "0";
}
}
}
return false;
}
static bool checkBoard(String[,] b, int n)
{
int posx;
int posy;
//Get Queen
for (int y = 0; y < n; y++)
{
for (int x = 0; x < n; x++)
{
if (b[y, x] == "1")
{
//Check Horizontal
for (int i = 0; i < n; i++)
{
if (b[y, i] == "1" && i != x)
{
return false;
}
}
//Check Diagonal⬈⬋
posx = x;
posy = y;
while (posx + 1 < n && posy + 1 < n)
{
posy += 1;
posx += 1;
if (b[posy, posx] == "1")
{
return false;
}
}
posx = x;
posy = y;
while (posx + 1 < n && posy - 1 >= 0)
{
posy -= 1;
posx += 1;
if (b[posy, posx] == "1")
{
return false;
}
}
//////////////////////////////////////
}
}
}
return true;
}
static void printBoard(string[,] b,int n)
{
for (int y = 0; y < n; y++)
{
Console.Write("|");
for (int x = 0; x < n; x++)
{
Console.Write($"{b[y,x]}|");
}
Console.WriteLine();
}
Console.WriteLine();
}
static void Main(string[] args)
{
int n;
Console.Write("Board : ");
n = Convert.ToInt32(Console.ReadLine());
String[,] board = new String[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
board[i, j] = "0";
}
}
printBoard(board,n);
solve(board, n);
printBoard(board, n);
}
}
}
Revise this Paste