Last active
July 2, 2017 17:07
-
-
Save philib/4346e4b595967eb4ba833873d0b02827 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package backtracking; | |
| /** | |
| * Created by Phil on 30.06.2017. | |
| */ | |
| public class Dameproblem { | |
| public int count = 0; | |
| public boolean isCandidate(int[][] brett, int zeile, int spalte){ | |
| if(zeile == 0 ){ | |
| return true; | |
| } | |
| for(int i = 1; i <= zeile; i++){ | |
| if(spalte-i >=0 && brett[zeile-i][spalte-i] == 1){ | |
| return false; | |
| } | |
| if(spalte+i < brett.length && brett[zeile-i][spalte+i] == 1){ | |
| return false; | |
| } | |
| if(brett[zeile-i][spalte] == 1){ | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| public void solve(int[][] brett, int stufe){ | |
| for(int spalte = 0; spalte < brett.length; spalte++){ | |
| if(isCandidate(brett,stufe,spalte)){ | |
| brett[stufe][spalte] = 1; | |
| if(stufe == brett.length-1){ | |
| print(brett); | |
| count = count + 1; | |
| }else { | |
| solve(brett, stufe + 1); | |
| } | |
| brett[stufe][spalte] = 0; | |
| } | |
| } | |
| } | |
| public void print(int[][] tmp){ | |
| for (int i = 0; i < tmp.length; i++){ | |
| for (int j = 0; j < tmp.length; j++){ | |
| System.out.print(tmp[i][j]+" "); | |
| } | |
| System.out.println(); | |
| } | |
| System.out.println("--------------"); | |
| } | |
| public static void main(String args[]){ | |
| int[][] brett = new int[5][5]; | |
| Dameproblem d = new Dameproblem(); | |
| d.solve(brett, 0); | |
| System.out.println(d.count); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment