使用分治法解决棋盘覆盖问题
2006-09-16 23:09
435 查看
棋盘覆盖问题的解法,使用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 ---------- */
相关文章推荐
- 使用分治法解决棋盘覆盖问题zz
- 利用分治法解决棋盘覆盖问题
- 利用分治法解决棋盘覆盖问题
- 分治法解决棋盘覆盖问题
- jpgraph的一个使用例子及汉字乱码的解决方法
- web 调用WCF 每次都要重新编译才能正常使用 终于解决了
- 看看如何解决“SQL Server只能使用Windows身份登录,不能使用sa等Sql server身份进行登录”的问题
- ode.js 版本控制 nvm 和 n 使用 及 nvm 重启终端失效的解决方法
- 解决openfire在使用MySQL数据库后的中文乱码问题
- 初次使用pycharm 的interpreter option为空解决办法。
- linux 下使用yum命令出错解决办法
- 设计能够导出所有记录到excel的分页(使用AspNetPager)页面,并解决excel文件名中文乱码问题
- ant 的ftp任务使用,实现自动备份等,解决中文问题
- apache 服务器不能使用本机IP访问解决办法
- 【转载】使用Bitmap.FromStream(GetType().Assembly.GetManifestResourceStream())出现System.ArgumentException的解决办法
- 一次替换多个词,批量替换多个字符串(使用不了的解决办法)
- centos使用vim命令出现E325错误解决方法
- Mysql中使用FIND_IN_SET解决IN条件为字符串时只有第一个数据可用的问题
- zsh ctrl-r 不能使用解决办法
- vsftpd使用高端口不能连接或者不能列出目录解决方法