题意:根据多米诺骨牌的编号的7*8矩阵,每个点可以和相邻的点组成的骨牌对应一个编号,问能形成多少种由编号组成的图。
分析:dfs,组成的图必须有1~28所有编号。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, };
const int dc[] = {, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int a[][];
int ans[][];
int id[][];//多米诺骨牌的编号
int vis[];
int mark[][];//是否访问过该点
int cnt;
void init(){
int k = ;
for(int i = ; i < ; ++i){
for(int j = i; j < ; ++j){
id[i][j] = id[j][i] = ++k;
}
}
}
bool judge1(int x, int y){
return x >= && x <= && y >= && y <= ;
}
bool judge2(int x){
for(int i = ; i < ; ++i){
if(!mark[x][i]) return true;
}
return false;
}
void dfs(int x, int y, int cur){
if(cur == ){
++cnt;
for(int i = ; i < ; ++i){
for(int j = ; j < ; ++j){
printf("%4d", ans[i][j]);
}
printf("\n");
}
printf("\n");
return;
}
if(x == ) return;
if(y == ){
if(judge2(x)) return;//如果这一行有未访问的
dfs(x + , , cur);
return;
}
if(mark[x][y]){
dfs(x, y + , cur);
return;
}
for(int i = ; i < ; ++i){//向右或向下
int tx = x + dr[i];
int ty = y + dc[i];
if(judge1(tx, ty) && !mark[tx][ty] && !vis[id[a[x][y]][a[tx][ty]]]){//下标合法、未被访问过,编号未用过
ans[x][y] = ans[tx][ty] = id[a[x][y]][a[tx][ty]];
mark[x][y] = mark[tx][ty] = vis[id[a[x][y]][a[tx][ty]]] = ;
dfs(x, y + , cur + );
mark[x][y] = mark[tx][ty] = vis[id[a[x][y]][a[tx][ty]]] = ;
}
}
}
int main(){
init();
int x;
int kase = ;
while(scanf("%d", &x) == ){
if(kase) printf("\n\n\n");
cnt = ;
memset(vis, , sizeof vis);
memset(ans, , sizeof ans);
memset(mark, , sizeof mark);
a[][] = x;
for(int i = ; i < ; ++i){
for(int j = ; j < ; ++j){
if(!i && !j) continue;
scanf("%d", &a[i][j]);
}
}
printf("Layout #%d:\n\n", ++kase);
for(int i = ; i < ; ++i){
for(int j = ; j < ; ++j){
printf("%4d", a[i][j]);
}
printf("\n");
}
printf("\nMaps resulting from layout #%d are:\n\n", kase);
dfs(, , );
printf("There are %d solution(s) for layout #%d.\n", cnt, kase);
}
return ;
}