using System;
class Program
{
const int N = 8;
static void ConfigureRandomly(int[,] board, int[] state)
{
Random rand = new Random();
for (int i = 0; i < N; i++)
{
state[i] = rand.Next(N);
board[state[i], i] = 1;
}
}
static void PrintBoard(int[,] board)
{
for (int i = 0; i < N; i++)
{
Console.Write(" ");
for (int j = 0; j < N; j++)
{
Console.Write(board[i, j] + " ");
}
Console.WriteLine();
}
}
static void PrintState(int[] state)
{
for (int i = 0; i < N; i++)
{
Console.Write(" " + state[i] + " ");
}
Console.WriteLine();
}
static bool CompareStates(int[] state1, int[] state2)
{
for (int i = 0; i < N; i++)
{
if (state1[i] != state2[i])
{
return false;
}
}
return true;
}
static void Fill(int[,] board, int value)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
board[i, j] = value;
}
}
}
static int CalculateObjective(int[,] board, int[] state)
{
int attacking = 0;
int row, col;
for (int i = 0; i < N; i++)
{
row = state[i];
col = i - 1;
while (col >= 0 && board[row, col] != 1)
{
col--;
}
if (col >= 0 && board[row, col] == 1)
{
attacking++;
}
row = state[i];
col = i + 1;
while (col < N && board[row, col] != 1)
{
col++;
}
if (col < N && board[row, col] == 1)
{
attacking++;
}
row = state[i] - 1;
col = i - 1;
while (col >= 0 && row >= 0 && board[row, col] != 1)
{
col--;
row--;
}
if (col >= 0 && row >= 0 && board[row, col] == 1)
{
attacking++;
}
row = state[i] + 1;
col = i + 1;
while (col < N && row < N && board[row, col] != 1)
{
col++;
row++;
}
if (col < N && row < N && board[row, col] == 1)
{
attacking++;
}
row = state[i] + 1;
col = i - 1;
while (col >= 0 && row < N && board[row, col] != 1)
{
col--;
row++;
}
if (col >= 0 && row < N && board[row, col] == 1)
{
attacking++;
}
row = state[i] - 1;
col = i + 1;
while (col < N && row >= 0 && board[row, col] != 1)
{
col++;
row--;
}
if (col < N && row >= 0 && board[row, col] == 1)
{
attacking++;
}
}
return attacking / 2;
}
static void GenerateBoard(int[,] board, int[] state)
{
Fill(board, 0);
for (int i = 0; i < N; i++)
{
board[state[i], i] = 1;
}
}
static void CopyState(int[] state1, int[] state2)
{
Array.Copy(state2, state1, N);
}
static void GetNeighbour(int[,] board, int[] state)
{
int[,] opBoard = new int[N, N];
int[] opState = new int[N];
CopyState(opState, state);
GenerateBoard(opBoard, opState);
int opObjective = CalculateObjective(opBoard, opState);
int[,] neighbourBoard = new int[N, N];
int[] neighbourState = new int[N];
CopyState(neighbourState, state);
GenerateBoard(neighbourBoard, neighbourState);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (j != state[i])
{
neighbourState[i] = j;
neighbourBoard[neighbourState[i], i] = 1;
neighbourBoard[state[i], i] = 0;
int temp = CalculateObjective(neighbourBoard, neighbourState);
if (temp <= opObjective)
{
opObjective = temp;
CopyState(opState, neighbourState);
GenerateBoard(opBoard, opState);
}
neighbourBoard[neighbourState[i], i] = 0;
neighbourState[i] = state[i];
neighbourBoard[state[i], i] = 1;
}
}
}
CopyState(state, opState);
Fill(board, 0);
GenerateBoard(board, state);
}
static void HillClimbing(int[,] board, int[] state)
{
int[,] neighbourBoard = new int[N, N];
int[] neighbourState = new int[N];
CopyState(neighbourState, state);
GenerateBoard(neighbourBoard, neighbourState);
do
{
CopyState(state, neighbourState);
GenerateBoard(board, state);
GetNeighbour(neighbourBoard, neighbourState);
if (CompareStates(state, neighbourState))
{
PrintBoard(board);
break;
}
else if (CalculateObjective(board, state) == CalculateObjective(neighbourBoard, neighbourState))
{
neighbourState[new Random().Next(N)] = new Random().Next(N);
GenerateBoard(neighbourBoard, neighbourState);
}
} while (true);
}
static void Main(string[] args)
{
int[] state = new int[N];
int[,] board = new int[N, N];
ConfigureRandomly(board, state);
HillClimbing(board, state);
}
}