1116 four-color problem, 1116 four-color Problem

Source: Internet
Author: User

1116 four-color problem, 1116 four-color Problem
1116 four-color Problem

 

Time Limit: 1 s space limit: 128000 KB title level: Gold QuestionDescription Description

Given the map of N (less than or equal to 8) points and the adjacent relationship between each point on the map, please output the number of all schemes that color the map in four colors (the adjacent two points must not be painted in the same color)

In the data, 0 indicates not adjacent, and 1 indicates adjacent.

Input description Input Description

The first line is an integer n, indicating that there are n points on the map.

In the next n rows, there are n integers in each line. Each integer is 0 or 1. The value of column j in row I indicates whether the column I and j are adjacent or not adjacent. The adjacent column is 1 and the non-adjacent column is 0.

We guarantee that a [I] [j] = a [j] [I] (a [I, j] = a [j, I])

Output description Output Description

Number of dyeing schemes

Sample Input Sample Input

8
0 0 0 1 0 1 0 0
0 0 0 0 1 0 1
0 0 0 0 0 1 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 1 0 0 0 0 0
1 0 1 0 0 0 0
0 1 0 0 0 0 0

Sample output Sample Output

15552

Data range and prompt Data Size & Hint

N <= 8

1 # include <iostream> 2 3 # include <cstdlib> 4 5 # include <cstdio> 6 7 # include <cstring> 8 9 # include <cmath> 10 11 # include <algorithm> 12 13 using namespace std; 14 15 int n, lin [10] [10], color [10], ans = 0; 16 17 void dfs (int point, int col) 18 19 // The color of point 20 21 {22 23 if (point = n + 1) // if all vertices have a color of 24 25 {26 27 for (int I = 1; I <= n; I ++) 28 29 {30 31 for (int j = 1; j <= n; j ++) 32 33 {34 35 if (lin [I] [j] = 1 & color [I] = color [j]) // determine whether the adjacent values are 36 37 return; 38 39} 40 41} 42 43 ans ++; // if all values match 44 45 return; 46 47} 48 49 color [point] = col; 50 51 for (int I = 1; I <= 4; I ++) 52 53 {54 55 dfs (point + 1, I); // The color 56 57} 58 59 return; 60 61} 62 63 int main () represented by the next point in Deep Search () 64 65 {66 67 scanf ("% d", & n); 68 69 for (int I = 1; I <= n; I ++) 70 71 for (int j = 1; j <= n; j ++) 72 73 scanf ("% d", & lin [I] [j]); 74 75 dfs (1, 1); 76 77 cout <ans; 78 79 return 0; 80 81}

 

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.