title: 习题7-3 UVa211_多米诺效应

date: 2021-01-29 19:08:00

categories:

  • 算法竞赛入门

tags:

  • 数据结构
  • 算法
  • UVa

题目: 使用28个多米诺骨牌拼凑出输入的二维数组

思路,使用一个二维数组标记每个多米诺骨牌的牌号,下标为Pips,一个典型的回溯

//
// Created by hsby on 2021/1/30.
// #include <iostream>
#include <cstring>
using namespace std; int Dir[2][2] = {{0, 1}, {1, 0}}; int dom[7][7] = {0};
int g[7][8];
int ans[7][8];
int t; bool judge(){
for (int i = 0; i < 7; ++i) {
for (int j = 0; j < 8; ++j) {
if (!ans[i][j]) return false;
}
}
return true;
} void dfs(int ai){
// 如果判断可行,输出结果
if (judge()){
for (int i = 0; i < 7; ++i) {
for (int j = 0; j < 8; ++j) {
printf("%2d ", ans[i][j]);
}
cout << endl;
}
cout << endl;
t++;
return;
}
// flag表示在ai行之后找到了可以继续遍历的起点
int flag = 1;
// 从ai行开始找
for (int i = ai; i < 7 && flag; ++i) {
for (int j = 0; j < 8 && flag; ++j) {
if (ans[i][j]) continue;
// 找到了,开始往右和下两个方向探索
for (int k = 0; k < 2; ++k) {
int ti = i + Dir[k][0];
int tj = j + Dir[k][1];
if (ans[ti][tj] || ti > 6 || ti < 0 || tj > 7 || tj < 0) continue;
flag = 0;
int a = min(g[i][j], g[ti][tj]);
int b = max(g[i][j], g[ti][tj]);
int d = dom[a][b];
ans[i][j] = d;
ans[ti][tj] = d;
dfs(i);
ans[i][j] = 0;
ans[ti][tj] = 0;
}
}
}
} int main(){
int cnt = 1;
for (int i = 0; i < 7; ++i) {
for (int j = i; j < 7; ++j) {
dom[i][j] = cnt++;
}
} int n = 1;
while (!cin.eof()){
memset(g, 0, sizeof(g));
memset(ans, 0, sizeof(ans));
t = 0;
cout << "Layout #" << n << ":" << endl << endl;
for (int i = 0; i < 7; ++i) {
for (int j = 0; j < 8; ++j) {
cin >> g[i][j];
cout << g[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << "Maps resulting from layout #" << n << " are:" << endl << endl;
dfs(0);
cout << "There are " << t++ << " solution(s) for layout #" << n++ << "." << endl << endl;
} return 0;
}

【笔记】《算法竞赛入门》习题7-3 UVa211_多米诺效应的更多相关文章

  1. 【笔记】《算法竞赛入门》习题7-6 UVa12113_重叠的正方形

    title: 习题7-6 UVa12113_重叠的正方形 date: 2021-01-31 19:08:00 categories: 算法竞赛入门 tags: 数据结构 算法 UVa <算法竞赛 ...

  2. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  3. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  4. [刷题]算法竞赛入门经典 3-12/UVa11809

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...

  5. [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...

  6. [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...

  7. [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...

  8. [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...

  9. 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth

    A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...

随机推荐

  1. 百度 Apollo无人车平台增加传感器

    https://github.com/ApolloAuto/apollo/issues/1649 如果想加入一个新的传感器不是百度官方推荐的传感器到Apollo平台做法: First you can ...

  2. java自学第5期——Object、Date、Calender、System、StringBuilder、基本类型包装类

    一.Object类 作用:对象操作 位置:java.lang.Object 方法: public String toString() :返回对象的字符串表示形式. public boolean equ ...

  3. EF多个表映射

    public class Media // One entity table { public int Id { get; set; } public string Name { get; set; ...

  4. c# 打印面单

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { ...

  5. linux系统解压命令总结

    原文链接:https://www.cnblogs.com/lhm166/articles/6604852.html tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追 ...

  6. list.add(int index, E element)和list.addAll(list1)

    List.add(int index, E element): 在列表的指定位置插入指定元素(可选操作).将当前处于该位置的元素(如果有的话)和所有后续元素向右移动(在其索引中加 1). 参数:ind ...

  7. 在js中如何将字符串类型的日期("2020-11-30T02:21:42.000+0000")进行格式化

    1.引入方法 import { formatDateNew } from '@/utils' 2.在方法中使用,注意要先将字符串进行new Date(),否则报错date.getFullYear is ...

  8. 百度AI api使用

    # *********************************baidu-api-通用文字识别******************************************** # im ...

  9. wxWidgets源码分析(5) - 窗口管理

    窗口管理 所有的窗口均继承自wxTopLevelWindows: WXDLLIMPEXP_DATA_CORE(wxWindowList) wxTopLevelWindows; wxTopLevelWi ...

  10. js一周时间表

    <div class="datetext"> <img class="dateLeft" src="./images/dateLef ...