Map 4 Color algorithm
#include <stdio.h>
#define N 10
void output (int color[])/* Output a shading scheme */
{int i;
for (i = 0; i < N; i++)
printf ("%4d", Color[i]);
printf ("/n");
}
int back (int *ip, int color[])/* Backtrack */
{
int c = 4;
while (c = = 4)
{
if (*ip <= 0)
return 0;
--(*IP);
c = Color[*ip];
COLOR[*IP] =-1;
}
return C;
}
/* Check area I for availability of C colors */
int Colorok (int i, int c, int adj[][n], int color[])
{
Int J;
for (j = 0; J < i; J + +)
if (adj[i][j]! = 0 && Color[j] = = c)
return 0;
return 1;
}
/* Select a lilt color for area I */
int select (int i, int c, int adj[][n], int color[])
{int k;
for (k = c; k <= 4; k++)
if (Colorok (I,k,adj,color))
return k;
return 0;
}
int coloring (int adj[][n])/* Find various coloring schemes */
{
int Color[n], I, C, CNT;
for (i = 0; i < N; i++)
Color[i] =-1;
i = c = 0;
CNT = 0;
while (1) {
if ((c =select (i,c+1,adj,color)) = = 0) {
c = Back (&i, color);
if (c = = 0) return CNT;
}
Else
{
Color[i]=c; i++;
if (i = = N) {
output (color);
++cnt;
c = Back (&i, color);
}
Else
c = 0;
}
}
}
void Main ()
{
int Adj[n][n] = {{0,1,0,1,1,1,1,1,1,1},
{1,0,1,1,0,1,1,1,1,0},
{0,1,0,1,0,1,1,0,1,1},
{1,1,1,0,1,1,0,0,1,1},
{1,0,0,1,0,1,0,0,0,0},
{1,1,1,1,1,0,1,0,0,1},
{1,1,1,0,0,1,0,0,1,0},
{1,1,0,0,0,0,0,0,1,1},
{1,1,1,1,0,0,1,1,0,1},
{1,0,1,1,0,1,0,1,1,0}};
printf ("Total%d set of solutions./n", coloring (adj)); coloring (adj);
}
This is a recursive and looping nesting.
Use loops to search around three squares of each map grid.
(a total of four squares around, one of which is the initial lattice)
For each search, the next level of search is then recursive.
By looping and recursively, you can fully search the entire map.