深度优先搜索 之 CODE[VS] 1116 四色问题
2015-12-04 13:06
337 查看
/* dfs,需要注意输入的测试数据的格式。 */
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstddef> #include <iterator> #include <algorithm> #include <string> #include <locale> #include <cmath> #include <vector> #include <cstring> #include <map> #include <utility> #include <queue> #include <stack> #include <set> using namespace std; const int INF = -0x3f3f3f3f; const int MaxN = 55; const int modPrime = 3046721; int n; int colorArr[10]; char imap[10][10]; int answer = 0; bool isAdjoinColor(int node, int color) { for (int i = 0; i < n; ++i) { if ((imap[node][i] == '1') && (color == colorArr[i])) { return true; } } return false; } void Solve(int nodeNum) { if (nodeNum == n) { ++answer; return; } for (int color = 1; color <= 4; ++color) { if (!isAdjoinColor(nodeNum, color)) { colorArr[nodeNum] = color; Solve(nodeNum + 1); colorArr[nodeNum] = -1; } } } int main() { #ifdef HOME freopen("in", "r", stdin); //freopen("out", "w", stdout); #endif fill(colorArr, colorArr + 10, -1); cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> imap[i][j]; } } Solve(0); cout << answer << endl; #ifdef HOME cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; _CrtDumpMemoryLeaks(); #endif return 0; }
相关文章推荐
- 百度地图API-自定义Lushu
- Android 多态知识使用实例(一)
- 我国已有1.39亿司机领取10年有效驾照
- MyEclipse10采用links安装插件的方法
- CentOS6.6下安装VMware Tools
- Tsinsen-1487:分配游戏【树状数组】
- 【C语言】 动态开辟二维数组
- HDU5475(线段树)
- Android4.0-Fragment框架实现方式剖析(一)
- 关于java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream解决办法
- UVa 10651 Pebble Solitaire(状压DP)
- 【转】Android Studio Essential Training
- random类的使用
- 数据结构基础知识之结构体
- iOS-UIKit( UIVisualEffectView.h - -解读)
- matalb神经网络工具箱函数--不能编译
- 选城市还是越野 国产自由光对比昂科威
- 使用Word2013无法打开云端文件的解决办法
- 颜色查询
- 新浪微博爬虫手机版(附源码)