zoukankan      html  css  js  c++  java
  • 棋盘覆盖问题

    <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>运行结果截图:


     

  • 相关阅读:
    IO编程
    File类
    对于Java集合理解
    Java泛型
    多线程编程
    异常处理
    Static.final修饰符、super关键字及常量与变量
    java类的基本结构
    数组
    二叉树后序遍历 递归 非递归
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3052284.html
Copyright © 2011-2022 走看看