• 深度优先搜索 之 CODE[VS] 1116 四色问题


    /*
    dfs,需要注意输入的测试数据的格式。
    */
     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 #include <cstddef>
     5 #include <iterator>
     6 #include <algorithm>
     7 #include <string>
     8 #include <locale>
     9 #include <cmath>
    10 #include <vector>
    11 #include <cstring>
    12 #include <map>
    13 #include <utility>
    14 #include <queue>
    15 #include <stack>
    16 #include <set>
    17 using namespace std;
    18 const int INF = -0x3f3f3f3f;
    19 const int MaxN = 55;
    20 const int modPrime = 3046721;
    21 
    22 int n;
    23 int colorArr[10];
    24 char imap[10][10];
    25 int answer = 0;
    26 
    27 bool isAdjoinColor(int node, int color)
    28 {
    29     for (int i = 0; i < n; ++i)
    30     {
    31         if ((imap[node][i] == '1') && (color == colorArr[i]))
    32         {
    33             return true;
    34         }
    35     }
    36     return false;
    37 }
    38 
    39 void Solve(int nodeNum)
    40 {
    41     if (nodeNum == n)
    42     {
    43         ++answer;
    44         return;
    45     }
    46     for (int color = 1; color <= 4; ++color)
    47     {
    48         if (!isAdjoinColor(nodeNum, color))
    49         {
    50             colorArr[nodeNum] = color;
    51             Solve(nodeNum + 1);
    52             colorArr[nodeNum] = -1;
    53         }
    54     }
    55 }
    56 
    57 
    58 int main()
    59 {
    60 #ifdef HOME
    61     freopen("in", "r", stdin);
    62     //freopen("out", "w", stdout);
    63 #endif
    64 
    65     fill(colorArr, colorArr + 10, -1);
    66     cin >> n;
    67     for (int i = 0; i < n; ++i)
    68     {
    69         for (int j = 0; j < n; ++j)
    70         {
    71             cin >> imap[i][j];
    72         }
    73     }
    74     Solve(0);
    75     cout << answer << endl;
    76 
    77 
    78 #ifdef HOME
    79     cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
    80     _CrtDumpMemoryLeaks();
    81 #endif
    82     return 0;
    83 }
    
    
    
     
     
  • 相关阅读:
    恢复误删的进程在使用的文件
    Linux系统CPU频率调整工具使用
    ubuntu opencv的使用
    ubuntu14.04 安装PCL
    boost 错误报告
    Ubuntu 查看软件版本
    Ubuntu14.04下安装glog
    PCL 编译中遇到 error C4996: 'pcl::SAC_SAMPLE_SIZE'
    EXE DLL等可执行程序添加版本号版权等信息
    ubuntu16.04中将python3设置为默认
  • 原文地址:https://www.cnblogs.com/shijianming/p/5019018.html
Copyright © 2020-2023  润新知