UVA 211 The Domino Effect 多米诺效应 (回溯)
骨牌无非两种放法,横着或竖着放,每次检查最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 多米诺效应 (回溯)的更多相关文章
- UVA - 211 The Domino Effect(多米诺效应)(dfs回溯)
题意:根据多米诺骨牌的编号的7*8矩阵,每个点可以和相邻的点组成的骨牌对应一个编号,问能形成多少种由编号组成的图. 分析:dfs,组成的图必须有1~28所有编号. #pragma comment(li ...
- uva 211(dfs)
211 - The Domino Effect Time limit: 3.000 seconds A standard set of Double Six dominoes contains 28 ...
- POJ 1135 -- Domino Effect(单源最短路径)
POJ 1135 -- Domino Effect(单源最短路径) 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两 ...
- CF 405B Domino Effect(想法题)
题目链接: 传送门 Domino Effect time limit per test:1 second memory limit per test:256 megabytes Descrip ...
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- POJ 1135 Domino Effect(Dijkstra)
点我看题目 题意 : 一个新的多米诺骨牌游戏,就是这个多米诺骨中有许多关键牌,他们之间由一行普通的骨牌相连接,当一张关键牌倒下的时候,连接这个关键牌的每一行都会倒下,当倒下的行到达没有倒下的关键牌时, ...
- 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 ...
- UVA211-The Domino Effect(dfs)
Problem UVA211-The Domino Effect Accept:536 Submit:2504 Time Limit: 3000 mSec Problem Description ...
- POJ 1135 Domino Effect (Dijkstra 最短路)
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9335 Accepted: 2325 Des ...
随机推荐
- C语言学习总结
输出加法程序 #include<stdio.h> int main() { printf("#include<stdio.h>\n\n"); printf( ...
- Unite 2017 | Unity引擎发展四大方向
Unite 2017 Shanghai已落幕,今天为大家分享本次大会备受关注的Keynote主题演讲.本次大会Keynote主题演讲聚焦了Unity全球领导团队,包括Unity创始人David Hel ...
- 2017-9-20 NOIP模拟赛
A 约数之和 (count.pas/c/cpp)TL:1S ML:128MB[Description]我们用 D(x)表示正整数 x 的约数的个数.给定一个正整数 N,求 D(1)+D(2)+…+D( ...
- 原生js 的ajax封装
/** * 封装ajax函数(包括跨域) * @method ajax * @param option :{type:"post" or "get" 请求方式, ...
- Vue实现选项卡效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...
- 剑指Offer的学习笔记(C#篇)-- 从上往下打印二叉树
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 一 . 题目解析 了解过二叉树就应该知道,二叉树存在三种遍历方法:前序遍历(根→左→右).中序遍历(左→根→右).后续遍历(左→右→根 ...
- 剑指Offer的学习笔记(C#篇)-- 反转链表
题目描述 输入一个链表,反转链表后,输出新链表的表头. 一 . 概念普及 关于线性表等相关概念请点击这里. 二 . 实现方法 目前,可以有两种方法实现该要求. 方法一:借助外部空间实现.这里可以将单链 ...
- JS高级学习历程-2
1.dom操作,利用dom创建无序列表.并追加到body里边,里面要求至少有四个项目. <!DOCTYPE html> <html lang="en"> & ...
- swift5 正则简单使用
/* 判断是否价格 */ let money = "100.98" let parrern = "^\\d+(\\.\\d{0,2})?$" if NSPred ...
- Codeforces 1C(外接圆与正多边形)
要点 各点肯定都在外接圆上,边越多越接近圆面积,所以要最小面积应当取可能的最少边数. 给三角形求外接圆半径公式:\(R=\frac{abc}{4S}\). 三个角度对应的圆心角取gcd即是要求的正多边 ...