题意:根据多米诺骨牌的编号的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 ;
}

UVA - 211 The Domino Effect(多米诺效应)(dfs回溯)的更多相关文章

  1. UVA 211 The Domino Effect 多米诺效应 (回溯)

    骨牌无非两种放法,横着或竖着放,每次检查最r,c最小的没访问过的点即可.如果不能放就回溯. 最外面加一层认为已经访问过的位置,方便判断. #include<bits/stdc++.h> ; ...

  2. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  3. UVa 524 Prime Ring Problem(DFS , 回溯)

    题意  把1到n这n个数以1为首位围成一圈  输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边推断  就要快非常多了 #inc ...

  4. 「POJ 1135」Domino Effect(dfs)

    BUPT 2017 Summer Training (for 16) #3G 题意 摆好的多米诺牌中有n个关键牌,两个关键牌之间有边代表它们之间有一排多米诺牌.从1号关键牌开始推倒,问最后倒下的牌在哪 ...

  5. uva 211(dfs)

    211 - The Domino Effect Time limit: 3.000 seconds A standard set of Double Six dominoes contains 28 ...

  6. UVA211-The Domino Effect(dfs)

    Problem UVA211-The Domino Effect Accept:536  Submit:2504 Time Limit: 3000 mSec  Problem Description ...

  7. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  8. poj1270Following Orders(拓扑排序+dfs回溯)

    题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...

  9. POJ 1135 -- Domino Effect(单源最短路径)

     POJ 1135 -- Domino Effect(单源最短路径) 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两 ...

随机推荐

  1. Codeforces Round #586 (Div. 1 + Div. 2)E(拓扑排序,思维)

    #include<bits/stdc++.h>using namespace std;int n,m,s; vector<int>edge[200007];queue<i ...

  2. java.lang.ClassCastException: android.app.Application cannot be cast to

    出这个异常的原因是在项目中添加了新lication类(public class Application extends lication)之后,没有在AndroidManifest.xml中添加该类的 ...

  3. C++11通过拷贝构造器拷贝const字段(待解决)

    问题梗概如标题所述. 在今天实现Token类的时候,遇到的问题. 我希望将Token类设定为immutable属性的,这样实现的方式是将这个类内的所有字段均设置为const型,同时每个字段均为publ ...

  4. 131、Java面向对象之static关键字三(使用static定义方法)

    01.代码如下: package TIANPAN; class Book { // 描述的是同一个出版社的信息 private String title; private double price; ...

  5. 109、Java中String类之截取部分子字符串

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  6. sqlserver链接字符串

    string connectionStr = "Data Source=LJJ-FF\\LJJ;Initial Catalog=TestDapper;User ID=sa;Password= ...

  7. 解压Assets.car获取App中的图片资源

    iOS开发时图片资源(png)是放置在Images.xcassets文件夹中进行管理的. 项目归档后就是ipa文件,在ipa文件中,Images.xcassets文件夹的内容放置在了Assets.ca ...

  8. 十四 Spring的AOP的基于AspectJ的注解开发

    Spring的AOP的基于AspectJ的注解开发 创建项目,引入jar包 编写目标类.切面类 配置目标类.切面类 在注解文件里开启AOP的开发 <?xml version="1.0& ...

  9. SqlParameter 类

    SqlParameter 类 表示 SqlCommand 的参数,也可以是它到 DataSet 列的映射.无法继承此类. 命名空间: System.Data.SqlClient 程序集: System ...

  10. java异常处理动手动脑问题解决和课后总结

    动手动脑 一.问题:请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. 1.源代码 import javax.swing.*; cl ...