您的位置:首页 > 其它

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






                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: