1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| package ch01;
import java.util.Scanner;
import static java.lang.Math.pow;
public class FillChess { private static int ChessSize; private static int x1,y1; private static int[][] chess; private static int t = 1; public static void main(String[] args) { ChessSize = (int) pow(2,new Scanner(System.in).nextInt()); Scanner sc = new Scanner(System.in); String[] numbers = sc.nextLine().split(" "); x1=Integer.parseInt(numbers[0])-1; y1=Integer.parseInt(numbers[1])-1; chess = new int[ChessSize][ChessSize]; three(0, 0, x1, y1, ChessSize); print(); }
private static void print() { for (int i = 0; i < ChessSize; i++) { for (int j = 0; j < ChessSize; j++) { if (i==x1 && j==y1) chess[i][j]=-1; System.out.print(chess[i][j] + "\t"); } System.out.println(); } }
public static void three(int chess_h, int chess_l, int t_h, int t_l,int size) { if (size == 1) return; int num = t++; int p = size / 2; if (t_h < chess_h + p && t_l < chess_l + p) { three(chess_h, chess_l, t_h, t_l, p); } else { chess[chess_h + p - 1][chess_l + p - 1] = num; three(chess_h, chess_l, chess_h + p - 1, chess_l + p - 1, p); } if (t_h < chess_h + p && t_l >= chess_l + p) { three(chess_h, chess_l + p, t_h, t_l, p); } else { chess[chess_h + p - 1][chess_l + p] = num; three(chess_h, chess_l + p, chess_h + p - 1, chess_l + p, p); } if (t_h >= chess_h + p && t_l < chess_l + p) { three(chess_h + p, chess_l, t_h, t_l, p); } else { chess[chess_h + p][chess_l + p - 1] = num; three(chess_h + p, chess_l, chess_h + p, chess_l + p - 1, p); } if (t_h >= chess_h + p && t_l >= chess_l + p) { three(chess_h + p, chess_l + p, t_h, t_l, p); } else { chess[chess_h + p][chess_l + p] = num; three(chess_h + p, chess_l + p, chess_h + p , chess_l + p , p); } } }
|