#include<iostream>
#include<iomanip>
#include<stdlib.h>
using namespace std;


int tile = 0;
int *(*board) = NULL;

void ChessBoard(int tr, int tc, int dr, int dc, int size)
{
    if (size == 1) return;
    int t = tile++,
    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, tc + s - 1, 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 + s, tr + s, tc + s, s);
    }
}


int main()
{
    p1:int tx = 0, ty = 0, sp, dx, dy, zsize;
    cout << "方格行号:";  cin >> dx;
    cout << "方格列号 :"; cin >> dy;
    cout << "要填充的数字: "; cin >> sp;
    cout << "请输入棋盘的大小: ";
    cin >> zsize; cout << endl;
    board = new int *[zsize];
    for (int i = 0; i < zsize; i++)
    {
        board[i] = new int[zsize];
    }
    board[dx-1][dy-1] = sp;
    ChessBoard(tx, ty, dx-1, dy-1, zsize);
    for (int j = 0; j < zsize; j++)
    {
        for (int m = 0; m < zsize; m++)
        {
            cout << setw(8) << board[j][m];
        }
        cout << endl;
    }
    goto p1;
    return 0;
}
0
Posted in ACM

Leave a Comment:

电子邮件地址不会被公开。