#include
#define n 10
INT A [n 1] [N 1]; / * Plaid * /
INT B [N * n 1] = {0, 1, 0}; / * is the number tag that has been filled * /
INT POS = 1; / * Point to the current grid * /
Struct
{
INT X;
Int Y;
Int Num;
} D [n * n 1];
INT isprime (int M) / * judgment is the number of prime * /
{
INT I;
IF (m == 2)
Return 1;
IF (m == 1 || M% 2 == 0)
Return 0;
For (i = 3; i * i <= m;)
{
IF (m% i == 0)
Return 0;
i = 2;
}
Return 1;
}
INT Selectnum (int Start, INT N) / * Selects a fillible minimum integer from Start * /
{
Int J;
For (j = start; j <= n * n; j )
IF (! b [j])
Return J;
Return 0;
}
INT CHECK (INT X, INT Y) / * Checks whether A [x] [Y] is rational * /
{
IF (x> 1 &&! isprime (a [x] [y] a [x-1] [y]))
Return 0;
IF (Y> 1 &&! Isprime (a [x] [y] a [x] [y-1])))
Return 0;
Return 1;
}
Extend (int N) / * to find an integer that has not been used in the next checkered * /
{
D [ POS] .Num = Selectnum (2, n);
A [D [POS] .x] [d [pOS] .y] = d [pOS] .num
B [D [POS] .NUM] = 1;
}
Void change (int N) / * For the current checkered integers (no retrospective) * /
{
Int J;
While (POS> = 1 && (j = selectnum (d [pOS] .NUM 1, N)) == 0)
B [D [POS -]. Num] = 0;
IF (POS <1)
{
Printf ("no");
Return;
}
B [D [POS] .NUM] = 0;
D [POS] .num = j;
A [D [POS] .x] [d [pOS] .y] = d [pOS] .num
B [J] = 1;
}
Void OutputResult (INT N) / * Output Results * /
{
INT I, J;
For (i = 1; i <= n; i )
{
For (j = 1; j <= n; j )
Printf ("% 4d", A [i] [j]);
Printf ("/ n");
}
}
Void Find (INT N)
{
INT OK = 1;
DO
{
IF (ok)
IF (POS == n * n)
{
OutputResult (n);
Return;
}
Else Extend (n);
Else Change (n);
OK = Check (D [POS] .x, d [pOS] .y);
WHILE (POS> = 1);
}
Main ()
{
INT I, J;
Int n;
DO
{
Printf ("INPUT THE N:"); scanf ("% D", & n);
WHILE (N <1 || n> 10);
For (i = 1; i <= n; i ) / * initialization array a * /
For (j = 1; j <= n; j )
A [i] [j] = 0;
A [1] [1] = 1;
For (i = 1; i <= n; i ) / * initialization array D * /
{
D [i] .x = 1;
D [i] .y = i;
D [i] .num = 0;
}
D [1] .num = 1;
For (; i <2 * n; i )
{
D [i] .x = i-n 1;
D [i] .y = 1;
D [i] .num = 0;
}
IF (n> 1)
For (; i <= n * n; i )
{
D [i] .x = 2 (i-2 * n) / (n-1);
D [i] .y = i-2 * n 1- (d [i] .x-2) * (N-1) 1;
D [i] .num = 0;
}
Find (n);
Getch ();
}