骨牌无非两种放法,横着或竖着放,每次检查最r,c最小的没访问过的点即可。如果不能放就回溯。

最外面加一层认为已经访问过的位置,方便判断。

#include<bits/stdc++.h>

const int MAXD = ;
const int MAXB = ;
const int MAXP = ; bool used[MAXB];// used Bone
int pip[MAXP][MAXP];// pip 2 Bone int tab[][];
int ans[][]; int cnt;
void dfs(int r,int c)
{
while(ans[r][c]) {
if(c == ) { r++; c = -; }
c++;
}
if(r == ) {
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
printf("%4d",ans[i][j]);
}
putchar('\n');
}
putchar('\n');
cnt++;
return ;
} int &p0 = tab[r][c];
if(!ans[r][c+]){ // 横着放
int &p1 = tab[r][c+];
int &bone = pip[p0][p1];
if(!used[bone]){
used[bone] = ;
ans[r][c+] = ans[r][c] = bone;
dfs(r,c+);
ans[r][c+] = ans[r][c] = ;
used[bone] = ;
}
} if(!ans[r+][c]) { //竖着放
int &p2 = tab[r+][c];
int &bone = pip[p0][p2];
if(!used[bone]){
used[bone] = ;
ans[r+][c] = ans[r][c] = bone;
dfs(r,c+);
ans[r+][c] = ans[r][c] = ;
used[bone] = ;
}
} } int main()
{
// freopen("in.txt","r",stdin);
// freopen("my.txt","w",stdout);
for(int h = ,c = ; h < ; h++)
for(int i = h; i < ; i++){
pip[i][h] = pip[h][i] = ++c;
}
int *layout = *tab;
int cas = ;
while(~scanf("%d",layout)){
if(cas) printf("\n\n\n");
memset(ans,,sizeof(ans));
memset(used,,sizeof(used));
cnt = ;
for(int i = ; i < ; i++) ans[i][] = ;
for(int i = ; i < ; i++) ans[][i] = ; for(int i = ; i < MAXD; i++)
scanf("%d",layout+i);
printf("Layout #%d:\n\n",++cas);
for(int i = ; i < ; i ++){
for(int j = ; j < ; j++)
printf("%4d",tab[i][j]);
putchar('\n');
}
printf("\nMaps resulting from layout #%d are:\n\n",cas);
dfs(,);
printf("There are %d solution(s) for layout #%d.\n",cnt,cas);
} return ;
}

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

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

    题意:根据多米诺骨牌的编号的7*8矩阵,每个点可以和相邻的点组成的骨牌对应一个编号,问能形成多少种由编号组成的图. 分析:dfs,组成的图必须有1~28所有编号. #pragma comment(li ...

  2. uva 211(dfs)

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

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

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

  4. CF 405B Domino Effect(想法题)

    题目链接: 传送门 Domino Effect time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  5. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  6. POJ 1135 Domino Effect(Dijkstra)

    点我看题目 题意 : 一个新的多米诺骨牌游戏,就是这个多米诺骨中有许多关键牌,他们之间由一行普通的骨牌相连接,当一张关键牌倒下的时候,连接这个关键牌的每一行都会倒下,当倒下的行到达没有倒下的关键牌时, ...

  7. POJ 1135 Domino Effect (spfa + 枚举)- from lanshui_Yang

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  8. UVA211-The Domino Effect(dfs)

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

  9. POJ 1135 Domino Effect (Dijkstra 最短路)

    Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9335   Accepted: 2325 Des ...

随机推荐

  1. ASP.NET中 TextBox 文本输入框控件的使用方法

    TextBox控件又称文本框控件,为用户提供输入文本的功能. 1.属性 TextBox控件的常用属性及说明如表1所示. 表1 TextBox控件常用属性及说明 属性 说明 AutoPostBack 获 ...

  2. Coreseek 安装问题

    Ubuntu下安装coreseek mmseg出现了cannot find input file: src/Makefile.in 解决方法如下 >autoheader >automake ...

  3. php 连接 memcached 并调用

    话不多说,上代码,自己看注释 <?php header("Content-type: text/html; charset=utf-8"); $mem = new Memca ...

  4. 浅淡Java多线程

    工作中一直忙着实现业务逻辑,多线程接触得不多.对多线程的认知,一直停留在Thread和Runnable上.最近心血来潮,找了几本多线程的书,不看不知道,一看吓一跳.原来我对多线程的理解是多么的肤浅.记 ...

  5. [Xcode 实际操作]六、媒体与动画-(6)使用UIBlurEffect给图片添加模糊效果

    目录:[Swift]Xcode实际操作 本文将演示如何给图像添加模糊效果. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class V ...

  6. [Xcode 实际操作]六、媒体与动画-(7)遍历系统提供的所有滤镜

    目录:[Swift]Xcode实际操作 本文将演示系统到底提供了多少滤镜供开发者使用,并了解每个滤镜都有哪些参数需要配置. 在项目导航区,打开视图控制器的代码文件[ViewController.swi ...

  7. 页面出现滚动条时,body里面的内容不能自动居中?

    弹窗后允许页面滚动 这种方式通常使用 position: absolute; ,可以看看我做的这个 Demo.主要用来应对弹窗内容很大很多的情况,超过了屏幕的宽高需要产生滚动条来方便浏览者查看.有一些 ...

  8. 单例设计模式singleton

    简单引入 单例设计模式作为最简单,最常用的设计模式.一般是这两中写法,这两种写法教科书所谓的标准写法,但是实际上存在不少问题.后面介绍标准写法,以规避这些问题. 1.懒汉式: /** * 问题在于,当 ...

  9. C# 线程同步计数存在的问题

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  10. poj3728The merchant树剖+线段树

    如果直接在一条直线上,那么就建线段树 考虑每一个区间维护最小值和最大值和答案,就符合了合并的条件,一个log轻松做 那么在树上只要套一个树剖就搞定了,多一个log也不是问题 注意考虑在树上的话每一条链 ...