Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20302
  • 博文数量: 17
  • 博客积分: 730
  • 博客等级: 军士长
  • 技术积分: 175
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-24 12:09
文章分类

全部博文(17)

文章存档

2010年(17)

我的朋友
最近访客

分类: C/C++

2010-01-24 13:34:52

/*问题描述*/
/*在一个n*n的棋盘中,有一个地方是 不同的,需要填充 用'L'型的方块填充。
*/

/*用递归的方式,分成四个部分,根据该块的位置,选择一个点填充,另外的也填充。。
*/
int a[4][4];

void chess(int line,int cols,int p_line,int p_cols,int size){
printf("begin\n");
if(size==1)
return ;
size /=2;
printf("size=%d\n",size);
/*divide it into 4,judge them if the particular one in it,then take action*/

if(p_line
/* it means that the element in this unit ,and at the left top of the big unit */
chess(line,cols,p_line,p_cols,size);
}
else{
/*not in this unit ,so we should value it's right below number */
a[line+size-1][cols+size-1]=1;
printf("a[%d][%d]=1",line+size-1,cols+size-1);
chess(line,cols,line+size-1,cols+size-1,size);
}

/*since it have four directorys,so the process will repeated*/
/*right top*/
if(p_line =cols+size){
chess(line,cols+size,p_line,p_cols,size);
}
else{
printf("a[%d][%d]=1",line+size-1,cols+size);
a[line+size-1][cols+size]=1;
chess(line,cols+size,line+size-1,cols+size,size);
}
/*left below */
if(p_line >=line+size && p_cols
chess(line+size,cols,p_line,p_cols,size);
}
else{
printf("a[%d][%d]=1",line+size,cols+size-1);
a[line+size][cols+size-1]=1;
chess(line+size,cols,line+size,cols+size-1,size);
}
/*right below*/
if(p_line >= line+size && p_cols >= cols+size){
chess(line+size,cols+size,p_line,p_cols,size);
}
else{
printf("a[%d][%d]=1",line+size,cols+size);
a[line+size][cols+size]=1;
chess(line+size,cols+size,line+size,cols+size,size);
}
}

int main(){
int i=0,j=0;
for(;i<4;++i)
for(j=0;j<4;++j)
a[i][j]=0;
a[1][1]=1;

for(i=0;i<4;++i){
for(j=0;j<4;++j)
printf("%d",a[i][j]);
printf("\n");
}
printf("\n\n");

chess(0,0,1,1,4);
for(i=0;i<4;++i){
for(j=0;j<4;++j)
printf("%d",a[i][j]);
printf("\n");
}
exit(0);
}
/*analysize */
/*according this program ,we should have found that 
 * this skill have advantages in the solution of such problem
 * put a big problem into many little steps
 * follow the steps above we will reach the practicularly element ,and draw the element arount it*/

阅读(462) | 评论(0) | 转发(0) |
0

上一篇:快速排序

下一篇:全排列

给主人留下些什么吧!~~