package eight.queen.algorithm; public class Queen { private int N = 8; private int chess[][]; public Queen(){ chess = new int[N][N]; System.out.println(N+" sizes chess table contain "+NQueen(0)+"\n"); } public int NQueen(int currentRow){ if(currentRow==N) return 1; int totalQueenSetting = 0; for(int i = 0;i<N;i++){ if(chess[currentRow][i]==0){ chess[currentRow][i] = 1+currentRow; setConsumedSquares(currentRow,i); totalQueenSetting += NQueen(currentRow+1); for(int k = 0; k<N;k++) for(int j = 0;j<N;j++) if(chess[k][j]==8){ print(); System.out.println("\t"); break; } chess[currentRow][i] = 0; recover(currentRow); } } return totalQueenSetting; } public void setConsumedSquares(int row,int i){ for(int j = row+1;j<N;j++){ if(chess[j][i]==0) chess[j][i] = (1+row); if(i-(j-row)>=0 && chess[j][i-(j-row)] == 0) chess[j][i-(j-row)] = (1+row); if(i+(j-row) < N && chess[j][i+(j-row)] == 0) chess[j][i+(j-row)] = (1+row); } } public void recover(int row){ for(int i = row;i<N;i++) for(int j = 0;j<N;j++) if(chess[i][j]==(1+row)) chess[i][j] = 0; } public void print(){ for(int i = 0; i<N;i++) for(int j = 0;j<N;j++){ System.out.print(chess[i][j]+" "); if(j == N-1) System.out.println("\t"); } } public static void main(String[] args){ Queen test = new Queen(); } }
Monday, 15 October 2012
N Queen Algorithm
insertion sorting
public class insertionSorting { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub insertionSorting test = new insertionSorting(); } public insertionSorting(){ int array[] = {2,5,1,9,12,3,6,7,11,11,11,8,21,17,45,46,33,22}; sorting(array); print(array); } public void sorting(int[] buffer){ int key,i; for(int j = 1;j<buffer.length;j++) { key = buffer[j]; i = j-1; while((i>=0) && buffer[i] > key){ buffer[i+1] = buffer[i]; i = i-1; } buffer[i+1] = key; } } public void print(int[] buffer){ for(int i = 0;i<buffer.length;i++) System.out.print(buffer[i]+", "); } }
Merge Sorting
public class mergeSort { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub mergeSort test = new mergeSort(); } private int[] array; private int N; public mergeSort(){ N = 10; array = new int[N]; for(int i=0;i<array.length;i++) array[i] = (int)(Math.random()*10)+1; print(array); sort(array,0,N-1); print(array); } public void print(int []array){ for(int i = 0;i<array.length;i++) System.out.print(array[i]+", "); System.out.println(""); } public void sort(int []array,int p,int r){ if(r-p>=1) { int q = (int)(Math.floor((double)((p+r)/2))); sort(array,p,q); sort(array,q+1,r); merge(array,p,q,r); } } public void merge(int []array,int p,int q,int r){ int n1 = q-p+1; int n2 = r-q; int left[] = new int[n1+1]; int right[] = new int[n2+1]; for(int i = 0;i<left.length-1;i++) left[i] = array[p+i]; for(int i=0;i<right.length-1;i++) right[i] = array[q+i+1]; left[n1] = 11; right[n2] = 11; int i = 0; int j = 0; for(int k = p;k<=r;k++){ if(left[i]<=right[j]){ array[k] = left[i]; i++; }else{ array[k] = right[j]; j++; } } } }
Etiketler:
algorithm,
Java,
merge,
mergesorting,
recursive,
recursivesort,
sorting
Subscribe to:
Posts (Atom)