The C + + program source code is as follows: Board overlay. CPP: Defines the entry point of the console application. // #include "stdafx.h" #include <iostream> #include <fstream> using namespace Std; int tile=1; Number of L-type dominoes (increment) int board[100][100]; Board /***************************************************** * Recursive method to implement checkerboard overlay algorithm * Input Parameters: * TR: The line number in the upper left corner of the current checkerboard * TC: The column number in the upper left corner of the current checkerboard * DR: the line number of the current special square * DC: The column number where the current special square is located * Size:size=2^k, board specifications for 2^k*2^k *****************************************************/ void chessboard (int tr, int tc, int dr, int dc, int size) { if (size==1)//Checkerboard size is 1, indicating recursion to the innermost Return int t=tile++; Increment 1 each time int S=SIZE/2; Row, column number (equal) in the middle of the chessboard Check if the special block is in the upper-left sub-chessboard if (dr<tr+s && dc<tc+s)//In Chessboard (TR, TC, DR, DC, s); else//not, treat the block in the lower right corner of the sub-checkerboard as a special block { board[tr+s-1][tc+s-1]=t; Chessboard (TR, TC, TR+S-1, tc+s-1, s); } Check if the special block is in the upper-right sub-chessboard if (dr<tr+s && dc>=tc+s)//In Chessboard (TR, tc+s, Dr, DC, s); else//not, treat the lower left corner of the sub-checkerboard as a special block { board[tr+s-1][tc+s]=t; Chessboard (tr, tc+s, Tr+s-1, Tc+s, s); } Check if the special block is in the bottom-left sub-chessboard if (dr>=tr+s && dc<tc+s)//In Chessboard (Tr+s, TC, DR, DC, s); else//not, treat the block in the upper right corner of the sub-checkerboard as a special block { board[tr+s][tc+s-1]=t; Chessboard (Tr+s, TC, Tr+s, tc+s-1, s); } Check if the special block is in the lower-right sub-chessboard if (dr>=tr+s && dc>=tc+s)//In Chessboard (Tr+s, Tc+s, Dr, DC, s); else//not, treat the block in the upper left corner of the sub-checkerboard as a special block { board[tr+s][tc+s]=t; Chessboard (Tr+s, Tc+s, Tr+s, Tc+s, s); } } int fuc (int i) { int count = 0; while (i) { Count + = i&0x01; I >>= 1; } if (Count < 2) return 1; Else return 0; } void menu (); void input () { Ofstream outfile; Outfile.open ("output.txt"); int size; int index_x,index_y; Flag cout<< "The size of the input chessboard (must be 2 n power):"; cin>>size; if (fuc (size)!=1) { cout<< "input error,"; Goto Flag; } Flag2: cout<< "Enter the line number and column number of the special grid position (starting from 0):"; cin>>index_x>>index_y; if (index_x >= size| | Index_y>=size) { cout<< "Special grid position beyond the checkerboard range,"; Goto Flag2; } Chessboard (0,0,index_x,index_y,size); for (int i=0; i<size; i++) { for (int j=0; j<size; J + +) { cout<<board[i][j]<< "\ t"; } cout<<endl<<endl; } Output to File for (int i=0; i<size; i++) { for (int j=0; j<size; J + +) { outfile<<board[i][j]<< "\ t"; } outfile<<endl<<endl; } cout<< "Last board overlay result has been output to Output.txt" <<endl; Outfile.close (); menu (); } void menu () { char c; cout<< "Do you want to continue typing (y/n)? "; cin>>c; if (c = = ' Y ' | | c = = ' Y ') Input (); } void Main () { Input (); } |