棋盤覆蓋問題 在一個2k×2k 個方格組成的棋盤中,恰有一個方格與其它方格不同,稱該方格為一特殊方格,且稱該棋盤為一特殊棋盤。在棋盤覆蓋問題中,要用圖示的4種不同形態的L型骨牌覆蓋給定的特殊棋盤上除特殊方格以外的所有方格,且任何2個L型骨牌不得重疊覆蓋。 算法描述如下:


 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include<string.h>
 4 
 5 int nCount = 0;
 6 int Matrix[100][100];
 7 
 8 void chessBoard(int tr, int tc, int dr, int dc, int size);
 9 
10 int main()
11 {
12     int size, r, c, row, col;
13     memset(Matrix, 0, sizeof(Matrix));
14     scanf_s("%d", &size);
15     scanf_s("%d%d", &row, &col);
16     chessBoard(0, 0, row, col, size);
17 
18     for (r = 0; r < size; r++)
19     {
20         for (c = 0; c < size; c++)
21         {
22             printf("%2d ", Matrix[r][c]);
23         }
24         printf("\n");
25     }
26 
27     return 0;
28 }
29 
30 void chessBoard(int tr, int tc, int dr, int dc, int size)
31 {
32     int s, t;
33     if (1 == size) return;
34     s = size / 2;
35     t = ++nCount;
36     if (dr < tr + s && dc < tc + s)
37     {
38         chessBoard(tr, tc, dr, dc, s);
39     }
40     else
41     {
42         Matrix[tr + s - 1][tc + s - 1] = t;
43         chessBoard(tr, tc, tr + s - 1, tc + s - 1, s);
44     }
45 
46     if (dr < tr + s && dc >= tc + s)
47     {
48         chessBoard(tr, tc + s, dr, dc, s);
49     }
50     else
51     {
52         Matrix[tr + s - 1][tc + s] = t;
53         chessBoard(tr, tc + s, tr + s - 1, tc + s, s);
54     }
55     if (dr >= tr + s && dc < tc + s)
56     {
57         chessBoard(tr + s, tc, dr, dc, s);
58     }
59     else
60     {
61         Matrix[tr + s][tc + s - 1] = t;
62         chessBoard(tr + s, tc, tr + s, tc + s - 1, s);
63     }
64     if (dr >= tr + s && dc >= tc + s)
65     {
66         chessBoard(tr + s, tc + s, dr, dc, s);
67     }
68     else
69     {
70         Matrix[tr + s][tc + s] = t;
71         chessBoard(tr + s, tc + s, tr + s, tc + s, s);
72     }
73 
74 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM