• 棋盘覆盖问题


    <1>问题:棋盘覆盖问题

    <2>代码:

    #include<iostream>
    using namespace std;
    int Board[4][4] = {0};
    int tile = 1;
    void ChessBoard(int tr,int tc,int dr,int dc,int size)
    {
     if(size == 1)
      return;
     int t = tile++;
     int s = size/2;
     if(dr<tr+s && dc<tc+s)
      ChessBoard(tr,tc,dr,dc,s);
     else
     {
      Board[tr+s-1][tc+s-1] = t;
      ChessBoard(tr,tc,tr+s-1,tc+s-1,s);
     }

     if(dr<tr+s && dc>=tc+s)
      ChessBoard(tr,tc+s,dr,dc,s);
     else
     {
      Board[tr+s-1][tc+s] = t;
      ChessBoard(tr,tc+s,tr+s-1,tc+s,s);
     }

     if(dr>=tr+s && dc<tc+s)
      ChessBoard(tr+s,tc,dr,dc,s);
     else
     {
      Board[tr+s][tc+s-1] = t;
      ChessBoard(tr+s,tc,tr+s-1,tc+s,s);
     }

     if(dr>=tr+s && dc>=tc+s)
      ChessBoard(tr+s,tc+s,dr,dc,s);
     else
     {
      Board[tr+s][tc+s] = t;
      ChessBoard(tr+s,tc,tr+s,tc+s,s);
     }
    }

    void main()
    {
     cout<<endl<<"覆盖前,棋盘状态:"<<endl;
     for(int i = 0;i < 4;i++)
     {
      for(int j = 0;j < 4;j++)
       cout<<Board[i][j]<<" ";
      cout<<endl;
     }
     cout<<endl<<"覆盖后,棋盘状态:"<<endl;
     ChessBoard(0,0,2,3,4);
     for(i = 0;i < 4;i++)
     {
      for(int j = 0;j < 4;j++)
       cout<<Board[i][j]<<" ";
      cout<<endl;
     }
    }

    <3>运行结果截图:


     

  • 相关阅读:
    使用高精度计算斐波那契数列 c++
    纪中9日T4 2298. 异或
    洛谷 P1416 攻击火星
    线段树小结
    纪中5日T3 1566. 幸运锁(lucky.pas/c/cpp)
    Title
    Title
    Title
    Title
    Title
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3052284.html
Copyright © 2020-2023  润新知