📄 chess.cpp
字号:
#include <iostream.h>
int title=1;
int b[4][4];
void chess_bord( int x, int y, int x0, int y0 , int size)
{
if (size==1) return ;
int t=title++;
int s;
//骨牌编号加1,用于覆盖中间区域
s=size/2; //将棋盘分成四块
if (x0<x+s&& y0<y+s) //特殊方格在左上角的棋盘中
chess_bord(x,y,x0,y0,s); //该子棋盘直接构成子问题,直接递归
else
{
b[x+s-1][y+s-1]=t; //将该子棋盘右下角盖上
chess_bord(x,y,x+s-1,y+s-1,s); //其余方格构成递归
}
if (x0>=x+s && y0 <y+s) //特殊方格在左下角的棋盘中
chess_bord(x+s,y,x0,y0,s);
else
{
b[x+s][y+s-1]=t; //将该子棋盘右上角盖上
chess_bord(x+s,y,x+s,y+s-1,s) ;//其余方格构成递归
}
if (x0<x+s&&y0>=y+s) //特殊方格在右上角子棋盘中
chess_bord(x,y+s,x0,y0,s);
else
{
b[x+s-1][y+s]=t; //将该子棋盘左下角盖上
chess_bord(x,y+s,x+s-1,y+s,s);//其余方格构成递归
}
if (x0>=x+s&& y0>=y+s) //特殊方格在右下角子棋盘中
chess_bord(x+s,y+s,x0,y0,s);
else
{
b[x+s][y+s]=t;//将该子棋盘左下角盖上
chess_bord(x+s,y+s,x+s,y+s,s);//其余方格构成递归
}
}
int main()
{
int x0,y0;
cout<<"请输入特殊方格所在的行号和列号(从零开始):"<<endl;
cin>>x0>>y0;
chess_bord(0,0,x0,y0,4);
for (int i=0;i<4;i++)
{
for (int j=0;j<4;j++)
cout<<b[i][j]<<" ";
cout<<endl;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -