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. .NET 云原生架构师训练营(模块二 基础巩固 安全)--学习笔记

    2.8 安全 认证 VS 授权 ASP .NET Core 认证授权中间件 认证 JWT 认证 授权 认证 VS 授权 认证是一个识别用户是谁的过程 授权是一个决定用户可以干什么的过程 401 Una ...

  2. macOS命令行切换Python版本

    目录 brew安装anaconda3 anaconda3环境变量设置 安装双版本 命令后切换python环境 pip ide vscode set 参考 brew安装anaconda3 brew ca ...

  3. CCF(引水入城:60分):最大流+ISAP算法

    引水入城 201703-5 这从题目分析来看很像最大流的问题,只需要增加一个超级源点和一个超级汇点就可以按照题意连边再跑最大流算法. 因为数据量太大了,肯定会超时.但是没有想到可行的解决方法. #in ...

  4. 在windows上安装MySQL数据库注意点及Navicat Premium 15的破解

    在windows上安装MySQL数据库  跟随慕课网教程(http://www.imooc.com/wiki/mysqllesson/mysqlwindows.html)下载安装MySQL: 其中注意 ...

  5. JDBC 连接池 & Template

    数据库连接池 # 概念:其实就是一个容器(集合),存放数据库连接的容器. * 当系统初始化号以后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容其中获取连接对象,用户访问完之后,会将 ...

  6. #String类简述(小白理解,小白编写,欢迎大神指点,小白跪谢)

    @ 目录 一.前言(可忽略) 二.String变量的认知 三.String类的构造方法 四.String类的基本方法 4.1 toString()方法 4.2 equals()方法 4.3 equal ...

  7. ElasticSearch 进阶

    目录 ElasticSearch 进阶 SearchAPI 检索信息 Query DSL 基本语法格式 查询-match 查询-match_phrase 查询-multi_match 查询-bool复 ...

  8. C# 应用 - 使用 WepApp 接受 Http 请求

    库类: Owin.dll Owin.IAppBuilder Microsoft.Owin.dll Microsoft.Owin.OwinContext Microsoft.Owin.Hosting.d ...

  9. 一款适用于windows10的反间谍工具

    Free antispy tool for Windows 10 前言 看标题的话,可能觉得"我要这款工具能干啥?",我刚开始也有这种疑惑,但后来我对于这款软件仔细想了想,这款还是 ...

  10. HiveHA机制源码分析

    hive让大数据飞了起来,不再需要专人写MR.平常我们都可以用基于thrift的任意语言来调用hive. 不过爱恨各半,hive的thrift不稳定也是出了名的.很容易就出问题,让人无计可施.唯一的办 ...