使用方向向量给标程压行:
#include<stdio.h> int tile, board[100][100]; void chessboard(int tr, int tc, int dr, int dc, int s) { if(!s) return ; int t = ++tile, mr = tr+s-1, mc = tc+s-1; int fi = mr<dr, fj = mc<dc; for(int i = 0; i <= 1; i++) for(int j = 0; j <= 1; j++) { if(i == fi && j == fj) chessboard(tr+s*i, tc+s*j, dr, dc, s>>1); else board[mr+i][mc+j] = t,chessboard(tr+s*i, tc+s*j, mr+i,mc+j,s>>1); } } int main() { chessboard(1,1,2,2,8); for(int i = 1; i <= 16; i++) for(int j = 1; j <= 16; j++) printf("%02d%c",board[i][j],j==16?' ':' '); return 0; }