Skip to content

Instantly share code, notes, and snippets.

@denistsyplakov
Created April 25, 2018 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denistsyplakov/433f9e6cd12fff745ef7a388bfb1254a to your computer and use it in GitHub Desktop.
Save denistsyplakov/433f9e6cd12fff745ef7a388bfb1254a to your computer and use it in GitHub Desktop.
Задача Алисе - перебор чисел.
package ru.yandex.lc.alice2;
public class Test {
public static void main(String[] args) {
int[] pa = new int[] {1, 2, 3,4,5,6,7,8,9};
prmt(pa, 0);
}
private static void prmt(int[] pa, int i) {
if (i == pa.length - 1) {
doTest(pa);
} else {
for (int j = i; j < pa.length; j++) {
aswap(pa, i, j);
prmt(pa, i + 1);
aswap(pa, i, j);
}
}
}
private static void aswap(int[] pa, int i, int j) {
int k = pa[i];
pa[i] = pa[j];
pa[j] = k;
}
private static void doTest(int[] pa) {
int[][] sq;
sq = new int[3][3];
for (int x= 0;x<3;x++ ){
for(int y = 0;y<3;y++){
sq[x][y] = -1;
}
}
sq[1][2] = 0;
int p = 0;
for (int x= 0;x<3;x++ ){
for(int y = 0;y<3;y++){
if ( sq[x][y]!= 0 ) {
sq[x][y] = pa[p++];
}
}
}
//test
int[] xs = new int[3];
int[] xy = new int[3];
for (int x= 0;x<3;x++ ){
for(int y = 0;y<3;y++){
xs[x] = xs[x] + sq[x][y];
xy[y] = xy[y] + sq[x][y];
}
}
for (int i = 0;i<3;i++ ){
if ((xs[i]!=14)||(xy[i]!=14)){
return;
}
}
for (int x= 0;x<3;x++ ){
for(int y = 0;y<3;y++){
System.out.print(sq[x][y]+ " ");
}
System.out.println();
}
System.out.println("------------------");
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment