Classic case of divide and conquer algorithm-checkerboard problem

Source: Internet
Author: User

2017-08-26 20:18:50

Writer:pprp

The question probably describes:

Have a 2k? 2k checkers, just one square is black, the other is white. Your task is to cover all white squares with an L-shaped card containing 3 squares.

Black squares cannot be overwritten, and any white squares cannot be overwritten by two or more cards at the same time.

Divide and conquer the solution, when the division of the time to determine the state, need what state, the end of conditions

The end condition is that when size becomes 1, it exits;

Desired state, starting point, position of black block, size side length

/*@theme: Checkerboard with L-shaped block heap full @writer:pprp@declare: The most classical @date:2017/8/26 algorithm of divide and conquer*/#include<bits/stdc++.h>#include<iostream>using namespacestd;Const intMAXN =10000;intCHESS[MAXN][MAXN];intNumber ;voidChessboard (intRowintColumnintXintYintSize) {       if(Size = =1)return;//Exit Criteria       intSS = size/2;//divide and conquer, halve the scale              intT = + +Number ; intCenterrow = row +SS; intCentercolumn = column +SS; //start judging Four directions//Upper left corner judgment       if(x < Centerrow && y <centercolumn)       {chessboard (ROW,COLUMN,X,Y,SS); }       Else{Chess[centerrow-1][centercolumn-1] =T; Chessboard (Row,column,centerrow-1, centercolumn-1, SS); }              //Upper right corner judgment       if(x < Centerrow && y >=centercolumn)       {chessboard (ROW,CENTERCOLUMN,X,Y,SS); }       Else{Chess[centerrow-1][centercolumn] =T; Chessboard (Row,centercolumn,centerrow-1, CENTERCOLUMN,SS); }              //Bottom left corner judgment       if(x >= centerrow && y <centercolumn)       {chessboard (CENTERROW,COLUMN,X,Y,SS); }       Else{Chess[centerrow][centercolumn-1] =T; Chessboard (Centerrow,column,centerrow,centercolumn-1, SS); }                                                                                 //Bottom right corner judgment       if(x >= centerrow && y >=centercolumn)       {chessboard (CENTERROW,CENTERCOLUMN,X,Y,SS); }       Else{Chess[centerrow][centercolumn]=BT;       Chessboard (CENTERROW,CENTERCOLUMN,CENTERROW,CENTERCOLUMN,SS); }       }intMain () {intSize; intx, y;  while(Cin >> Size &&Size) {memset (chess,0,sizeof(chess)); CIN>> x >>y; Chessboard (0,0, x,y,size); Chess[x][y]= Number =1;  for(inti =0; i < Size; i++)            {                   for(intj =0; J < Size; J + +) {cout<< chess[i][j]<<"\ t"; } cout<< Endl <<Endl;; }      }                  return 0;}

Classic case of divide and conquer algorithm-checkerboard problem

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.