DFS七巧板着色问题
作者:
恒_41
,
2024-05-20 11:41:40
,
所有人可见
,
阅读 3
#include <cstring>
#include <iostream>
#include <algorithm>
#include <deque>
#define x first
#define y second
using namespace std;
const int N = 7;
const int COLORS = 4;
int g[N][N];
int color[4] = { 0,1,2,3 };//四种颜色编号
int prices[7];//每个板子的颜色
bool check(int i,int color) {
for (int j = 0; j < N; j++)
if (prices[j] == color&&g[i][j]==1)
return false;
return true;
}
void dfs(int u) {
if (u == N) {
for (int i = 0; i < N; i++)
cout << prices[i]<<" ";
cout << endl;
return;
}
for (int i = 0; i < COLORS; i++) {//在这个板子上尝试填充每一种颜色
if ( check(u, i) && prices[u]==-1 ) {
prices[u] = color[i];
dfs(u + 1);
prices[u] = -1;
}
}
}
int main() {
memset(prices, -1, sizeof prices);
g[0][1]=g[1][0]= g[0][6] =g[6][0]=g[4][0]= g[0][4] = 1;//1表示直接相邻
g[1][3] = g[3][1] = g[1][5] = g[5][1] = 1;
g[2][3] = g[3][2] = g[4][2] = g[2][4] = g[2][5] = g[5][2] = g[2][6] = g[6][2]=1;
g[3][5] = g[3][6] = g[5][3] = g[6][3]=1;
g[4][6] = g[6][4] = 1;
dfs(0);//当前涂下标为0的板子
}