棋盘覆盖问题的解法,使用C语言写成,放在这里是为了以后忘记了可以拿过来看看。
/*
* ============================================================================
*
* Filename: chess_board.c
*
* Description: the solution of chess board's coverage
*
* Version: 1.0
* Created: 09/16/06 22:37:43 CST
* Revision: none
* Compiler: gcc
*
* Author: lirui (colin), lirui@ncic.ac.cn
* Company: asl.ncic.ac.cn
*
* ============================================================================
*/
#include <stdio.h>
#define MAXSIZE 1<<10
int chess[MAXSIZE][MAXSIZE];
void chess_board(int tr, int tc, int dr, int dc, int size)
{
static int tile = 1;
if(1==size) return;
int t = tile++;
int s = (size>>1);
/* ---- left-up corner ---- */
if(dr<tr+s && dc<tc+s){
chess_board(tr, tc, dr, dc, s);
} else {
chess[tr+s-1][tc+s-1] = t;
chess_board(tr, tc, tr+s-1, tc+s-1, s);
}
/* ---- right-up corner --- */
if(dr<tr+s && dc>=tc+s){
chess_board(tr, tc+s, dr, dc, s);
} else {
chess[tr+s-1][tc+s] = t;
chess_board(tr, tc+s, tr+s-1, tc+s, s);
}
/* ---- left-down corner--- */
if(dr>=tr+s && dc<tc+s){
chess_board(tr+s, tc, dr, dc, s);
} else {
chess[tr+s][tc+s-1] = t;
chess_board(tr+s, tc, tr+s, tc+s-1, s);
}
/* ---- right-down corner-- */
if(dr>=tr+s && dc>=tc+s){
chess_board(tr+s, tc+s, dr, dc, s);
} else {
chess[tr+s][tc+s] = t;
chess_board(tr+s, tc+s, tr+s, tc+s, s);
}
}
void print_chess(int size)
{
int i, j;
for(i = 0; i<size; i++){
for(j=0; j<size; j++)
printf("%4d", chess[i][j]);
printf(" ");
}
}
int main( int argc, char *argv[] )
{
chess_board(0, 0, 1, 2, 16);
print_chess(16);
return 0;
} /* ---------- end of function main ---------- */
* ============================================================================
*
* Filename: chess_board.c
*
* Description: the solution of chess board's coverage
*
* Version: 1.0
* Created: 09/16/06 22:37:43 CST
* Revision: none
* Compiler: gcc
*
* Author: lirui (colin), lirui@ncic.ac.cn
* Company: asl.ncic.ac.cn
*
* ============================================================================
*/
#include <stdio.h>
#define MAXSIZE 1<<10
int chess[MAXSIZE][MAXSIZE];
void chess_board(int tr, int tc, int dr, int dc, int size)
{
static int tile = 1;
if(1==size) return;
int t = tile++;
int s = (size>>1);
/* ---- left-up corner ---- */
if(dr<tr+s && dc<tc+s){
chess_board(tr, tc, dr, dc, s);
} else {
chess[tr+s-1][tc+s-1] = t;
chess_board(tr, tc, tr+s-1, tc+s-1, s);
}
/* ---- right-up corner --- */
if(dr<tr+s && dc>=tc+s){
chess_board(tr, tc+s, dr, dc, s);
} else {
chess[tr+s-1][tc+s] = t;
chess_board(tr, tc+s, tr+s-1, tc+s, s);
}
/* ---- left-down corner--- */
if(dr>=tr+s && dc<tc+s){
chess_board(tr+s, tc, dr, dc, s);
} else {
chess[tr+s][tc+s-1] = t;
chess_board(tr+s, tc, tr+s, tc+s-1, s);
}
/* ---- right-down corner-- */
if(dr>=tr+s && dc>=tc+s){
chess_board(tr+s, tc+s, dr, dc, s);
} else {
chess[tr+s][tc+s] = t;
chess_board(tr+s, tc+s, tr+s, tc+s, s);
}
}
void print_chess(int size)
{
int i, j;
for(i = 0; i<size; i++){
for(j=0; j<size; j++)
printf("%4d", chess[i][j]);
printf(" ");
}
}
int main( int argc, char *argv[] )
{
chess_board(0, 0, 1, 2, 16);
print_chess(16);
return 0;
} /* ---------- end of function main ---------- */
0 件のコメント:
コメントを投稿