题意:根据多米诺骨牌的编号的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. UCOS-III API函数

    附录:UCOS-III API函数 任务管理 就绪列表 挂起队列 时间管理 信号量 消息队列 内存管理

  2. luogu P2754 [CTSC1999]家园

    本题是分层图最大流问题,相当于按时间拆点,每个当前点向下一点的下一时间层连点,每一层有n+1个点 #include<bits/stdc++.h> using namespace std; ...

  3. Python 基础之循环结构for及break pass continue

    一.for 循环 #循环 变量 迭代 都是一个意思#把列表里面的元素意义的拿出来就是遍历listvar = ["one","two","three&q ...

  4. 从系统引导菜单禁用Hyper-V

    1, 从当前引导运行的系统复制一个新引导菜单项: bcdedit /copy {current} /d "Win10 NO_HV" 这时候cmd会输出新引导项的 guid, 复制出 ...

  5. ubuntu16.04 使用tensorflow object detection训练自己的模型

    一.构建自己的数据集 1.格式必须为jpg.jpeg或png. 2.在models/research/object_detection文件夹下创建images文件夹,在images文件夹下创建trai ...

  6. MAC Matlab 中文乱码

    环境:macOS High Sierra 10.13.4 问题:文件中文注释乱码(再次打开文件时) / 控制台输出中文乱码 解决方法: 官网下载补丁(https://ww2.mathworks.cn/ ...

  7. Broadcast机制(二)

    内容提纲: 在本节课当中,我会继续为大家讲解Android的广播机制,重点内容有以下的两个方面: a)         在应用程序当中注册BroadcastReceiver的方法 b)         ...

  8. 【替代语法】PHP中冒号、endif、endwhile、endfor这些都是什么

    我们经常在wordpress一类博客程序的模板里面看到很多奇怪的PHP语法,比如: <?php if(empty($GET_['a'])): ?><font color=" ...

  9. Vim学习1移动光标

    vim adventure上面做了大量练习,是个好软件只是需要收费 HJKLWBE 首先HJKL分别是移动上下左右的 H:光标左移 J:下移 K:上移 L:右移 W:跳转到下一个单词的第一个字,注意是 ...

  10. Vue项目中v-for无法渲染数据

    在Vue项目中,我们想要实现下面的布局效果 后端返回的数据格式如下,可以看出产品列表五张图的数据位于同一个数组中 而我的html结构如下: 我希望直接渲染左边一张大图,然后右边的四张小图通过v-for ...