骨牌无非两种放法,横着或竖着放,每次检查最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. Spring入门第九课

    使用外部属性文件 在配置文件里面配置Bean时,有时需要在Bean的配置里面混入系统部署的细节信息(例如:文件路径,数据源配置信息等)而这些部署细节实际上需要和Bean配置相分离. Spring提供了 ...

  2. textarea的style="resize:none;"

    <textarea class="form-control" id="gryj" rows="3" maxlength="3 ...

  3. 二十五种网页加速方法和seo优化技巧

    一.使用良好的结构 可扩展 HTML (XHTML) 具有许多优势,但是其缺点也很明显.XHTML 可能使您的页面更加符合标准,但是它大量使用标记(强制性的 <start> 和 <e ...

  4. 51nod1282(最小表示法&&枚举)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1282 题意:中文题目诶- 思路:指针不可转,刻盘可转,显然,对 ...

  5. [Xcode 实际操作]六、媒体与动画-(5)使用CoreImage框架给图片添加马赛克效果

    目录:[Swift]Xcode实际操作 本文将演示如何使用CoreImage图像处理框架,给图片添加像素化的滤镜效果. 在项目导航区,打开视图控制器的代码文件[ViewController.swift ...

  6. jQuery EasyUI/TopJUI创建树形表格下拉框

    jQuery EasyUI/TopJUI创建树形表格下拉框 第一种方法(纯HTML创建) <div class="topjui-row"> <div class= ...

  7. java CDI

    Scope声明周期 http://www.cnblogs.com/yjmyzz/p/javaee-cdi-bean-scope.html

  8. java 性能调优和GC

    JAVA 性能调优和GC http://blog.csdn.net/gzh0222/article/details/7663181 JAVA GC调优手记 http://blog.csdn.net/f ...

  9. C - Distinct Substrings (模板)

    https://vjudge.net/problem/SPOJ-DISUBSTR 有两种方式来求去除重读的子串 #include <bits/stdc++.h> using namespa ...

  10. mongodb-win

    http://www.cnblogs.com/clsn/p/8214194.html#auto_id_22 Mongodb的作用: 用于对象及JSON数据的存储,它存的都是对象比如字典, 数据库,无论 ...