骨牌无非两种放法,横着或竖着放,每次检查最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. C++读取XML,tinyXml的使用

    前言: 最近在开发的过程中,有个需求是对xml进行格式转化,从一种格式转化到另外一种格式.因此,就需要读取xml进行处理.原本打算写成工具在linux下运行,不过后来考虑到和系统结合,最后也就使用了前 ...

  2. WCF大文件传输【转】

    http://www.cnblogs.com/happygx/archive/2013/10/29/3393973.html WCF大文件传输 WCF传输文件的时候可以设置每次文件的传输大小,如果是小 ...

  3. SQL SERVER动态列名

    在ms sql server实现动态呈现列的方法很多.下面Insus.NET解决也算是另外一种参考. 如: 准备实现功能的数据: ) NOT NULL PRIMARY KEY) INSERT INTO ...

  4. PHP AES/ECB 128加密

    class Security { public static function encrypt($input, $key) { $key=md5($key.md5($key)); $size = mc ...

  5. [Xcode 实际操作]五、使用表格-(10)插入UITableView单元格

    目录:[Swift]Xcode实际操作 本文将演示如何插入一行单元格. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先添加两个协 ...

  6. 用IDEA写出第一个java web

    今天学习到如何创建新的java web,笔者使用的是IDEA,以下有详细的参考链接,如下: 参考链接:https://blog.csdn.net/zhdkong/article/details/789 ...

  7. RobotFramework学习笔记-Web自动化

    一.窗口关键字使用 1.当前浏览器弹出新的窗口 使用Select Window和Close Window处理弹出窗口.实际使用中Select Window不一定会一次选中,通常会结合Wait Unti ...

  8. IOS字符串截取保留小数点后两位

    -(NSString*)getTheCorrectNum:(NSString*)tempString { //计算截取的长度 NSUInteger endLength = tempString.len ...

  9. eclipse for php 开发环境配置

    PHP有非常多相当不错的开发工具,如Zend Studio.NetBeans.phpdesigner等,但对于习惯Java编程的程序员们来说,下面介绍最经常使用的还要属Eclipse. > 我们 ...

  10. Codeforces Round #566 (Div. 2) A. Filling Shapes

    链接: https://codeforces.com/contest/1182/problem/A 题意: You have a given integer n. Find the number of ...