-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NQueens: done! backtracking: simple algorithm done! minConflicts: jus…
…t created!
- Loading branch information
0 parents
commit 1307759
Showing
4 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
.idea/ | ||
out/ | ||
*.iml | ||
|
||
|
||
|
||
# Default ignored files | ||
/shelf/ | ||
/workspace.xml | ||
# Datasource local storage ignored files | ||
/dataSources/ | ||
/dataSources.local.xml | ||
# Editor-based HTTP Client requests | ||
/httpRequests/ | ||
|
||
# Created by https://www.toptal.com/developers/gitignore/api/intellij,java,jetbrains | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=intellij,java,jetbrains | ||
|
||
### Intellij ### | ||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider | ||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | ||
|
||
# User-specific stuff | ||
.idea/**/workspace.xml | ||
.idea/**/tasks.xml | ||
.idea/**/usage.statistics.xml | ||
.idea/**/dictionaries | ||
.idea/**/shelf | ||
|
||
# Generated files | ||
.idea/**/contentModel.xml | ||
|
||
# Sensitive or high-churn files | ||
.idea/**/dataSources/ | ||
.idea/**/dataSources.ids | ||
.idea/**/dataSources.local.xml | ||
.idea/**/sqlDataSources.xml | ||
.idea/**/dynamic.xml | ||
.idea/**/uiDesigner.xml | ||
.idea/**/dbnavigator.xml | ||
|
||
# Gradle | ||
.idea/**/gradle.xml | ||
.idea/**/libraries | ||
|
||
# Gradle and Maven with auto-import | ||
# When using Gradle or Maven with auto-import, you should exclude module files, | ||
# since they will be recreated, and may cause churn. Uncomment if using | ||
# auto-import. | ||
# .idea/artifacts | ||
# .idea/compiler.xml | ||
# .idea/jarRepositories.xml | ||
# .idea/modules.xml | ||
# .idea/*.iml | ||
# .idea/modules | ||
# *.iml | ||
# *.ipr | ||
|
||
# CMake | ||
cmake-build-*/ | ||
|
||
# Mongo Explorer plugin | ||
.idea/**/mongoSettings.xml | ||
|
||
# File-based project format | ||
*.iws | ||
|
||
# IntelliJ | ||
out/ | ||
|
||
# mpeltonen/sbt-idea plugin | ||
.idea_modules/ | ||
|
||
# JIRA plugin | ||
atlassian-ide-plugin.xml | ||
|
||
# Cursive Clojure plugin | ||
.idea/replstate.xml | ||
|
||
# Crashlytics plugin (for Android Studio and IntelliJ) | ||
com_crashlytics_export_strings.xml | ||
crashlytics.properties | ||
crashlytics-build.properties | ||
fabric.properties | ||
|
||
# Editor-based Rest Client | ||
.idea/httpRequests | ||
|
||
# Android studio 3.1+ serialized cache file | ||
.idea/caches/build_file_checksums.ser | ||
|
||
### Intellij Patch ### | ||
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 | ||
|
||
# *.iml | ||
# modules.xml | ||
# .idea/misc.xml | ||
# *.ipr | ||
|
||
# Sonarlint plugin | ||
# https://plugins.jetbrains.com/plugin/7973-sonarlint | ||
.idea/**/sonarlint/ | ||
|
||
# SonarQube Plugin | ||
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin | ||
.idea/**/sonarIssues.xml | ||
|
||
# Markdown Navigator plugin | ||
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced | ||
.idea/**/markdown-navigator.xml | ||
.idea/**/markdown-navigator-enh.xml | ||
.idea/**/markdown-navigator/ | ||
|
||
# Cache file creation bug | ||
# See https://youtrack.jetbrains.com/issue/JBR-2257 | ||
.idea/$CACHE_FILE$ | ||
|
||
# CodeStream plugin | ||
# https://plugins.jetbrains.com/plugin/12206-codestream | ||
.idea/codestream.xml | ||
|
||
### Java ### | ||
# Compiled class file | ||
*.class | ||
|
||
# Log file | ||
*.log | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.nar | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
|
||
### JetBrains ### | ||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider | ||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | ||
|
||
# User-specific stuff | ||
|
||
# Generated files | ||
|
||
# Sensitive or high-churn files | ||
|
||
# Gradle | ||
|
||
# Gradle and Maven with auto-import | ||
# When using Gradle or Maven with auto-import, you should exclude module files, | ||
# since they will be recreated, and may cause churn. Uncomment if using | ||
# auto-import. | ||
# .idea/artifacts | ||
# .idea/compiler.xml | ||
# .idea/jarRepositories.xml | ||
# .idea/modules.xml | ||
# .idea/*.iml | ||
# .idea/modules | ||
# *.iml | ||
# *.ipr | ||
|
||
# CMake | ||
|
||
# Mongo Explorer plugin | ||
|
||
# File-based project format | ||
|
||
# IntelliJ | ||
|
||
# mpeltonen/sbt-idea plugin | ||
|
||
# JIRA plugin | ||
|
||
# Cursive Clojure plugin | ||
|
||
# Crashlytics plugin (for Android Studio and IntelliJ) | ||
|
||
# Editor-based Rest Client | ||
|
||
# Android studio 3.1+ serialized cache file | ||
|
||
### JetBrains Patch ### | ||
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 | ||
|
||
# *.iml | ||
# modules.xml | ||
# .idea/misc.xml | ||
# *.ipr | ||
|
||
# Sonarlint plugin | ||
# https://plugins.jetbrains.com/plugin/7973-sonarlint | ||
|
||
# SonarQube Plugin | ||
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin | ||
|
||
# Markdown Navigator plugin | ||
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced | ||
|
||
# Cache file creation bug | ||
# See https://youtrack.jetbrains.com/issue/JBR-2257 | ||
|
||
# CodeStream plugin | ||
# https://plugins.jetbrains.com/plugin/12206-codestream | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/intellij,java,jetbrains |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
|
||
public class Backtracking { | ||
int variablesCount; | ||
|
||
public Backtracking(int variablesCount) { | ||
this.variablesCount = variablesCount; | ||
} | ||
|
||
public boolean FindResult(HashMap<Integer, Integer> assignments) { | ||
if (assignments.size() == variablesCount) | ||
return true; | ||
HashSet<Integer> conflictedValues = new HashSet<>(); | ||
int variable = FindVariableWithMRV(assignments, conflictedValues); | ||
for (int value = 0; conflictedValues.size() < variablesCount && value < variablesCount; value++) | ||
if (!conflictedValues.contains(value)) { | ||
assignments.put(variable, value); | ||
if (FindResult(assignments)) | ||
return true; | ||
else { | ||
assignments.remove(variable); | ||
} | ||
} | ||
if (assignments.size() > 220) | ||
System.out.println(assignments.size()); | ||
return false; | ||
} | ||
|
||
/** | ||
* find a variable with smallest domain of values( Minimum Remaining Values ). | ||
* | ||
* @param assignments pairs of variables and their values. | ||
* @param conflictedValues set of conflicted values for best variable. | ||
* @return the variable with Minimum Remaining Values( the one with smallest domain ). | ||
*/ | ||
private int FindVariableWithMRV(HashMap<Integer, Integer> assignments, HashSet<Integer> conflictedValues) { | ||
if (assignments.isEmpty()) { | ||
return variablesCount / 2; | ||
} | ||
int maxConflictedValuesCount = 0; | ||
int bestVariable = -1; | ||
for (int i = 0; i < variablesCount; i++) | ||
if (!assignments.containsKey(i)) { | ||
HashSet<Integer> conflictedValuesOfCurrentVariable = new HashSet<>(); | ||
for (int variable : assignments.keySet()) { | ||
conflictedValuesOfCurrentVariable.add(assignments.get(variable)); | ||
/**first line equation( y2-y1=m*(x2-x1) ): "x1=variable", "x2=i", "m=1", "y1=assignment.get(variable)" and "y2=value=?".*/ | ||
int value = variable - i + assignments.get(variable); | ||
if (value < variablesCount && value >= 0) | ||
conflictedValuesOfCurrentVariable.add(value); | ||
/**second line equation: "x1=variable", "x2=i", "m=-1", "y1=assignment.get(variable)" and "y2=value=?".*/ | ||
value = -variable + i + assignments.get(variable); | ||
if (value < variablesCount && value >= 0) | ||
conflictedValuesOfCurrentVariable.add(value); | ||
} | ||
if (conflictedValuesOfCurrentVariable.size() > maxConflictedValuesCount) { | ||
maxConflictedValuesCount = conflictedValuesOfCurrentVariable.size(); | ||
bestVariable = i; | ||
conflictedValues.clear(); | ||
conflictedValues.addAll(conflictedValuesOfCurrentVariable); | ||
} | ||
} | ||
return bestVariable; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
public class MinConflicts { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import java.util.HashMap; | ||
import java.util.Scanner; | ||
|
||
public class NQueens { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
System.out.println("how many queens?"); | ||
int n = scanner.nextInt(); | ||
Backtracking resultFinder = new Backtracking(n); | ||
// MinConflicts resultFinder = new MinConflicts(n); | ||
HashMap<Integer, Integer> result = new HashMap<>(); | ||
resultFinder.FindResult(result); | ||
for (int key : result.keySet()) | ||
System.out.println(key + ": " + result.get(key)); | ||
scanner.next(); | ||
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < n; j++) { | ||
if (result.get(i) == j) | ||
System.out.print("|Q"); | ||
else | ||
System.out.print("|_"); | ||
} | ||
System.out.println("|"); | ||
} | ||
} | ||
} |