【笔记】《算法竞赛入门》习题7-3 UVa211_多米诺效应
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_多米诺效应的更多相关文章
- 【笔记】《算法竞赛入门》习题7-6 UVa12113_重叠的正方形
title: 习题7-6 UVa12113_重叠的正方形 date: 2021-01-31 19:08:00 categories: 算法竞赛入门 tags: 数据结构 算法 UVa <算法竞赛 ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- [刷题]算法竞赛入门经典 3-12/UVa11809
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...
- [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...
- [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...
- [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...
- [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...
- 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth
A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...
随机推荐
- 教你玩转CSS border(边框)
边框样式 边框样式属性指定要显示什么样的边界. border-style属性用来定义边框的样式 border-style的值 代码演示: <!DOCTYPE html> <html ...
- react性能提升
1.把.bind(this)提升到constructor里面 2.在生命周期函数里面shouldComponentupdate里面做父组件改变重新渲染以致于子组件重新渲染的禁止 3.在setstate ...
- 微信小程序(二)-语法学习
语法学习 一 模板语法 https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxml/ 1.数据代码 // pages/bl ...
- 谈一下hashMap中put是如何实现的?
源码: Hash(key):计算出key的hash值. put方法详解: 1.如果table数组为null或者table数组的长度为0,则调用resize()方法扩容并返回table数组.数组的长度为 ...
- 翻译:《实用的Python编程》02_06_List_comprehension
目录 | 上一节 (2.5 collections模块) | 下一节 (2.7 对象模型) 2.6 列表推导式 一个常见的任务是处理列表中的项(译注:元素).本节介绍列表推导式,完成此任务的强大工具. ...
- 树莓派4B安装官方Ubuntu20 Server版(64位)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- Snort + Barbyard2 + Snorby环境搭建
1.环境 ubuntu-14.04.5 daq-2.0.7 Snort-2.9.15.1 Barbyard2 snorby Mysql Docker 2.架构 3.安装步骤 Ubuntu配置 如果是刚 ...
- Java跨平台原理与Java虚拟机(JVM)
Java跨平台原理(字节码文件.虚拟机) C/C++语言都直接编译成针对特定平台机器码.如果要跨平台,需要使用相应的编译器重新编译. Java源程序(.java)要先编译成与平台无关的字节码文件(.c ...
- 通达OA 越权访问-2013/2015版本
漏洞参考 http://wiki.0-sec.org/0day/%E9%80%9A%E8%BE%BEoa/9.html 复现 根据⽹上的通达 OA的源码找这些敏感地址,如: /general/syst ...
- CVE-2019-12409-Apache Solr JMX服务远程代码执行
漏洞分析 https://www.freebuf.com/vuls/218730.html 漏洞介绍 该漏洞源于默认配置文件solr.in.sh中的ENABLE_REMOTE_JMX_OPTS配置选项 ...