Horse traversal problem

xiaoxiao2021-03-05  31

#include

#include

INT Deltai [] = {2, 1, -1, -2, -2, -1, 1, 2};

INT deltaj [] = {1, 2, 2, 1, -1, -2, -2, -1};

int Board [8] [8];

/ * Requisit (i, j)'s export number, and the export portions in A [], S is the order of the start number * /

Int EXITN (INT I, INT J, INT S, INT A [])

{

INT I1, J1, K, Count = 0;

FOR (k = 0; k <8; k )

{

I1 = i deltai [(S K)% 8];

J1 = J Deltaj [(S K)% 8];

IF (i1> = 0 && i1 <8 && j1> = 0 && J1 <8 &&board [i1] [j1] == 0)

a [count ] = (S K)% 8;

}

Return count;

}

/ * Select the next exit * /

INT NEXT (INT I, INT J, INT S)

{

INT M, K, KK, Min, A [8], B [8], TEMP

M = exitn (i, j, s, a);

IF (m == 0) return -1; / * No exit * /

min = 9;

For (k = 0; k

{

Temp = EXITN (i deltai [a [a [k]], j deltaj [a [k]], s, b);

IF (Temp

{

MIN = TEMP;

KK = a [k];

}

}

Return KK; / * Return to the selected method * /

}

void main ()

{

INT SX, SY, I, J, Step, NO, Start; / * START is used to control 8 possible selection sequences * /

For (sx = 0; sx <8; sx )

FOR (Sy = 0; SY <8; SY )

{

START = 0; / * From the beginning of the No. 0 method * /

DO

{

/ * Checoon * /

For (i = 0; i <8; i )

For (j = 0; j <8; j )

Board [i] [j] = 0;

Board [sx] = 1; // Departure location

I = SX; J = Sy;

For (Step = 2; Step <= 64; Step )

{

IF ((NO = NEXT (I, J, START) == - 1) BREAK;

i = deltai [no];

J = Deltaj [NO];

Board [i] [j] = step;

}

IF (Step> 64)

Break;

START ; // The previous one did not find the solution

} while (step <= 64);

/ * Output solution * /

For (i = 0; i <8; i )

{

For (j = 0; j <8; j )

Printf ("% 4D", Board [i] [j]);

Printf ("/ n / n");

}

Getch ();

Printf ("/ n / n");

}

}

转载请注明原文地址:https://www.9cbs.com/read-33413.html

New Post(0)