Implementation of the Board coverage problem by the method of divide and conquer

Source: Internet
Author: User

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 ();

}

Test data:

Size=8

Line number for special grid position: 3,4

The results of this experimental procedure are as follows:

Output.txt:

3 3 4 4 8 8 9 9

3 2 2 4 8 7 7 9

5 2 6 6 10 10 7 11

5 5 6 1 0 10 11 11

13 13 14 1 1 18 19 19

13 12 14 14 18 18 17 19

15 12 12 16 20 17 17 21

15 15 16 16 20 20 21 21

because the use is C + + Console program, so can not be graphically output, the results are not intuitive. and the number of output squares in the console interface should not be too much, so I wrote the last output to the Output.txt file, so that the number of squares to display more!

Implementation of the Board coverage problem by the method of divide and conquer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.