Today's written examination of the question, the answer on the Internet search.

Source: Internet
Author: User
Tags printf

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.