initial commit

This commit is contained in:
2025-08-03 20:24:38 -07:00
commit 6b9f8b2f04
586 changed files with 2006175 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
+87
View File
@@ -0,0 +1,87 @@
using System.Collections.Generic;
using System.Linq;
namespace nqueens
{
public class NBoard
{
public readonly int[] Columns;
public int Size { get { return Columns.Length; } }
public readonly int[] Conflicts;
public NBoard(int[] columns)
{
Columns = columns;
Conflicts =
(from n in Enumerable.Range(0, Size)
select GetConflictCount(n)).ToArray();
}
public int GetQueenRow(int column)
{
return Columns[column];
}
public bool IsFeasible()
{
return Conflicts.All(c => c == 0);
}
public int GetConflictCount(int column)
{
return GetConflicts(column).Count;
}
public int TestConflictCount(int column, int row)
{
var originalRow = Columns[column];
Columns[column] = row;
var conflicts = GetConflictCount(column);
Columns[column] = originalRow;
return conflicts;
}
private List<int> GetConflicts(int column)
{
var queenRow = GetQueenRow(column);
var conflicts = new List<int>();
for (var iColumn = 0; iColumn < Size; iColumn++)
{
if (iColumn == column)
continue;
var iRow = Columns[iColumn];
if (iRow == queenRow)
{
conflicts.Add(iColumn);
continue;
}
var columnDiff = column - iColumn;
if (queenRow - columnDiff == iRow || queenRow + columnDiff == iRow)
conflicts.Add(iColumn);
}
return conflicts;
}
public void UpdateRow(int column, int row)
{
var oldConflictColumns = GetConflicts(column);
foreach (var i in oldConflictColumns)
Conflicts[i]--;
var sum = Conflicts.Any(c => c < 0);
Columns[column] = row;
var newConflictColumns = GetConflicts(column);
Conflicts[column] = newConflictColumns.Count;
foreach (var i in newConflictColumns)
Conflicts[i]++;
}
}
}
+115
View File
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace nqueens
{
class Program
{
static void Main(string[] args)
{
var n = Convert.ToInt32(args[0]);
var lsQueens = new MinConflictsQueens(n);
var solution = lsQueens.Solve();
if (solution != null)
{
var s = string.Join(" ", solution.Select(i => i.ToString()));
Console.WriteLine(n);
Console.WriteLine(s);
Debug.WriteLine(n);
Debug.WriteLine(s);
}
else
{
Console.WriteLine("No solution");
}
}
}
public class MinConflictsQueens
{
private readonly int _n;
public MinConflictsQueens(int n)
{
_n = n;
}
private NBoard Initial()
{
//var random = new Random(1);
//var randValues = (from i in Enumerable.Range(0, _n)
// select random.Next(0, _n)).ToArray();
//var board = new List<int>(_n);
//for (var i = 0; i < _n; i++)
//{
// if (i % 2 == 0)
// board.Insert(0,i);
// else
// board.Insert(i,i);
//}
//return new NBoard(board.ToArray());
var board = new int[_n];
var n2 = _n/2;
for (var i = 1; i <= n2; i++)
{
board[i - 1] = 2*i - 1;
board[n2 + i - 1] = 2* (i - 1);
}
//board[n2 - 1] = 0;
return new NBoard(board);
}
public IList<int> Solve()
{
var nBoard = Initial();
var random = new Random();
int lastColumn = -1;
int iterations = 0;
while (true)
{
if (nBoard.IsFeasible())
return nBoard.Columns;
if (++iterations % 100 == 0)
Debug.WriteLine("{0} : {1}", iterations, nBoard.Conflicts.Sum());
// select the next biggest conflict count
var maxConflictColumn =
(from c in Enumerable.Range(0, _n)
let conflicts = nBoard.Conflicts[c]
select new {c, conflicts})
.GroupBy(arg => arg.conflicts)
.OrderByDescending(grouping => grouping.Key)
.First()
.OrderBy(arg => random.Next())
.First()
.c;
if (maxConflictColumn == lastColumn)
maxConflictColumn = random.Next(0, _n);
lastColumn = maxConflictColumn;
var minConflictRow =
(from row in Enumerable.Range(0, _n)
let conflicts = nBoard.TestConflictCount(maxConflictColumn, row)
select new {row, conflicts})
.GroupBy(arg => arg.conflicts)
.OrderBy(grouping => grouping.Key)
.First()
.OrderBy(arg => random.Next())
.First()
.row;
nBoard.UpdateRow(maxConflictColumn, minConflictRow);
}
}
}
}
+36
View File
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("nqueens")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("nqueens")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("2653647d-52e8-48ef-af3d-40adb6a2e58e")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
+57
View File
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0C81AFB2-E3EC-4535-86A6-955BA5BE2576}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>nqueens</RootNamespace>
<AssemblyName>nqueens</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Google.OrTools.ConstraintSolver">
<HintPath>..\..\..\..\Documents\or-tools.Windows64\bin\Google.OrTools.ConstraintSolver.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="NBoard.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
+26
View File
@@ -0,0 +1,26 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nqueens", "nqueens.csproj", "{0C81AFB2-E3EC-4535-86A6-955BA5BE2576}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nqueens.tests", "..\nqueens.tests\nqueens.tests.csproj", "{EE40FD45-A2A0-4198-86A0-F7E3ED334B51}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0C81AFB2-E3EC-4535-86A6-955BA5BE2576}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C81AFB2-E3EC-4535-86A6-955BA5BE2576}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C81AFB2-E3EC-4535-86A6-955BA5BE2576}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C81AFB2-E3EC-4535-86A6-955BA5BE2576}.Release|Any CPU.Build.0 = Release|Any CPU
{EE40FD45-A2A0-4198-86A0-F7E3ED334B51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE40FD45-A2A0-4198-86A0-F7E3ED334B51}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE40FD45-A2A0-4198-86A0-F7E3ED334B51}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE40FD45-A2A0-4198-86A0-F7E3ED334B51}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal