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 75
| #include <stdio.h> #include <stdlib.h> #include<string.h>
int nCount = 0; int Matrix[100][100];
void chessBoard(int tr, int tc, int dr, int dc, int size);
int main() { int size, r, c, row, col; memset(Matrix, 0, sizeof(Matrix)); scanf_s("%d", &size); scanf_s("%d%d", &row, &col); chessBoard(0, 0, row, col, size);
for (r = 0; r < size; r++) { for (c = 0; c < size; c++) { printf("%2d ", Matrix[r][c]); } printf("\n"); }
return 0; }
void chessBoard(int tr, int tc, int dr, int dc, int size) { int s, t; if (1 == size) return; s = size / 2; t = ++nCount; if (dr < tr + s && dc < tc + s) { chessBoard(tr, tc, dr, dc, s); } else { Matrix[tr + s - 1][tc + s - 1] = t; chessBoard(tr, tc, tr + s - 1, tc + s - 1, s); }
if (dr < tr + s && dc >= tc + s) { chessBoard(tr, tc + s, dr, dc, s); } else { Matrix[tr + s - 1][tc + s] = t; chessBoard(tr, tc + s, tr + s - 1, tc + s, s); } if (dr >= tr + s && dc < tc + s) { chessBoard(tr + s, tc, dr, dc, s); } else { Matrix[tr + s][tc + s - 1] = t; chessBoard(tr + s, tc, tr + s, tc + s - 1, s); } if (dr >= tr + s && dc >= tc + s) { chessBoard(tr + s, tc + s, dr, dc, s); } else { Matrix[tr + s][tc + s] = t; chessBoard(tr + s, tc + s, tr + s, tc + s, s); }
}
|