题意:根据多米诺骨牌的编号的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. Uber为何会成为共享经济中全球市值最大的独角兽企业?

    自5月10日登陆纽交所以来,Uber的股价就像过山车一般起起伏伏,让无数投资者痛并快乐着.不过在经过半个月时间的试探后,如今Uber的股价已经稳定在40美元左右.截至美国东部时间5月24日股市收盘,U ...

  2. linux 部署java 项目命令

    1:服务器部署路径:/home/tomcat/tomcat/webapps  (用FTP工具链接服务器把包上传到此目录) 2:进入项目文件夹 cd /home/tomcat/tomcat/webapp ...

  3. Java-用星号打印菱形

    打印如图所示菱形9行9列(提示可以将菱形分成上下两个三角形,分析每行空格数和星号个数的关系) 代码如下: package com.homework.lhh; public class Ex20 { p ...

  4. Linux centosVMware Nginx负载均衡、ssl原理、生成ssl密钥对、Nginx配置ssl

    一.Nginx负载均衡 vim /usr/local/nginx/conf/vhost/load.conf // 写入如下内容 upstream qq_com { ip_hash; 同一个用户始终保持 ...

  5. DRF项目之序列化器和视图重写方法的区别

    我们,都知道,DRF框架是一款高度封装的框架. 我们可以通过重写一些方法来实现自定义的功能. 今天,就来说说在视图中重写和序列化器中重写方法的区别. 在视图中重写方法: 接收请求,处理数据(业务逻辑) ...

  6. List模拟栈

    import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main<E ...

  7. 如何使用ffmpeg进行音视频裁剪命令和音视频合成命令

    音视频剪裁命令 ffmpeg -i input.mp4 -ss 00:00:00 -t 10 out.ts -i : 指定视频 -ss : 开始时间 -t : 指定裁剪的秒数 音视频合并的命令 ffm ...

  8. C 如何判断编译器是否支持C90 C99?

    参考:<C Primer Plus>,Stephen Prata著,姜佑译. ANSI/ISO C标准 美国ANSI成立委员会X3J11,于89/90年,99年,11年,发布C标准:C89 ...

  9. Design and History FAQ for Python3

    Source : Design and History FAQ for Python3 Why is there no goto? 你可以通过异常来获得一个可以跨函数调用的 "goto 结构 ...

  10. 用python发送qq邮件

    一.需要开启smtp服务,获取授权密码. 在qq邮箱的设置里开启smtp 二.代码 # -*- coding:utf-8 -*- import smtplib from email.mime.text ...