深度优先搜索 之 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 }

 
 
时间: 2024-07-31 14:04:16

深度优先搜索 之 CODE[VS] 1116 四色问题的相关文章

深度优先搜索 之 CODE[VS] 1295 N皇后问题

/* dfs,关键:检查皇后会发生攻击的状态. 两种方法:(第二种,速度更快) (1)void Solve(int row, int colUsed); 常规办法,判断状态是否非法的方法:列上通过位运算:左斜.右斜通过遍历. (2)void SolveBitOperation(unsigned col, unsigned tiltLeft, unsigned tiltRight); 完全位运算,具体参考代码注释. 另外可参考Matrix67的博客(有图,很形象):http://www.matri

POJ 1979 Red and Black 深度优先搜索上手题

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21738   Accepted: 11656 Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a

sicily 1024 邻接矩阵与深度优先搜索解题

Description There are N cities and N-1 roads in Magic-Island. You can go from one city to any other. One road only connects two cities. One day, The king of magic-island want to visit the island from the capital. No road is visited twice. Do you know

[复习]深度优先搜索

深度优先搜索(dfs)是利用递归完成的以搜索深度优先的搜索 通常大概是这样的: 1 void search(int vi){ 2 if( 达到目标 ){ //边界 3 if( 不满足要求 ) return ; 4 //(和最优解比较) 5 //当比当前最优解更优时 6 //更新当前最优解 7 return ;//必须返回 8 } 9 for( int i = ...; i <= ... ; i++ ){ //枚举 10 //保存结果 11 search(vi); 12 //清除刚刚保存的结果 1

【图论】广度优先搜索和深度优先搜索

写在最前面的 这篇文章并没有非常详细的算法证明过程.导论里面有非常详细的证明过程.本文只阐述“广度优先和深度优先搜索的思路以及一些简单应用”. 两种图的遍历算法在其他图的算法当中都有应用,并且是基本的图论算法. 广度优先搜索 广度优先搜索(BFS),可以被形象的描述为“浅尝辄止”,具体一点就是每个顶点只访问它的邻接节点(如果它的邻接节点没有被访问)并且记录这个邻接节点,当访问完它的邻接节点之后就结束这个顶点的访问. 广度优先用到了“先进先出”队列,通过这个队列来存储第一次发现的节点,以便下一次的

十大基础实用算法之深度优先搜索和广度优先搜索

深度优先搜索算法(Depth-First-Search),是搜索算法的一种.它沿着树的深度遍历树的节点,尽可能深的搜索树的分支.当节点v的所有边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点.这一过程一直进行到已发现从源节点可达的所有节点为止.如果还存在未被发现的节点,则选择其中一个作为源节点并重复以上过程,整个进程反复进行直到所有节点都被访问为止.DFS属于盲目搜索. 深度优先搜索是图论中的经典算法,利用深度优先搜索算法可以产生目标图的相应拓扑排序表,利用拓扑排序表可以方便的解决很多相

迷宫问题 - 堆栈与深度优先搜索

堆栈的访问规则被限制为Push和Pop两种操作,Push(入栈或压栈)向栈顶添加元素,Pop(出栈或弹出)则取出当前栈顶的元素,也就是说,只能访问栈顶元素而不能访问栈中其它元素. 现在我们用堆栈解决一个有意思的问题,定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走

小橙书阅读指南(十二)——无向图、深度优先搜索和路径查找算法

在计算机应用中,我们把一系列相连接的节点组成的数据结构,叫做图.今天我们将要介绍它的一种形式--无向图,以及针对这种结构的深度优先搜索和路径查找算法. 一.无向图数据结构 接口: /** * 图论接口 */ public interface Graph { /** * 顶点数 * * @return */ int vertexNum(); /** * 边数 * * @return */ int edgeNum(); /** * 向图中添加一条v-w的边 * * @param v * @param

ZOJ1002 —— 深度优先搜索

ZOJ1002 —— Fire net Time Limit: 2000 ms Memory Limit: 65536 KB Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a sma