UVA - 211 The Domino Effect(多米诺效应)(dfs回溯)
题意:根据多米诺骨牌的编号的7*8矩阵,每个点可以和相邻的点组成的骨牌对应一个编号,问能形成多少种由编号组成的图。
分析:dfs,组成的图必须有1~28所有编号。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, };
const int dc[] = {, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int a[][];
int ans[][];
int id[][];//多米诺骨牌的编号
int vis[];
int mark[][];//是否访问过该点
int cnt;
void init(){
int k = ;
for(int i = ; i < ; ++i){
for(int j = i; j < ; ++j){
id[i][j] = id[j][i] = ++k;
}
}
}
bool judge1(int x, int y){
return x >= && x <= && y >= && y <= ;
}
bool judge2(int x){
for(int i = ; i < ; ++i){
if(!mark[x][i]) return true;
}
return false;
}
void dfs(int x, int y, int cur){
if(cur == ){
++cnt;
for(int i = ; i < ; ++i){
for(int j = ; j < ; ++j){
printf("%4d", ans[i][j]);
}
printf("\n");
}
printf("\n");
return;
}
if(x == ) return;
if(y == ){
if(judge2(x)) return;//如果这一行有未访问的
dfs(x + , , cur);
return;
}
if(mark[x][y]){
dfs(x, y + , cur);
return;
}
for(int i = ; i < ; ++i){//向右或向下
int tx = x + dr[i];
int ty = y + dc[i];
if(judge1(tx, ty) && !mark[tx][ty] && !vis[id[a[x][y]][a[tx][ty]]]){//下标合法、未被访问过,编号未用过
ans[x][y] = ans[tx][ty] = id[a[x][y]][a[tx][ty]];
mark[x][y] = mark[tx][ty] = vis[id[a[x][y]][a[tx][ty]]] = ;
dfs(x, y + , cur + );
mark[x][y] = mark[tx][ty] = vis[id[a[x][y]][a[tx][ty]]] = ;
}
}
}
int main(){
init();
int x;
int kase = ;
while(scanf("%d", &x) == ){
if(kase) printf("\n\n\n");
cnt = ;
memset(vis, , sizeof vis);
memset(ans, , sizeof ans);
memset(mark, , sizeof mark);
a[][] = x;
for(int i = ; i < ; ++i){
for(int j = ; j < ; ++j){
if(!i && !j) continue;
scanf("%d", &a[i][j]);
}
}
printf("Layout #%d:\n\n", ++kase);
for(int i = ; i < ; ++i){
for(int j = ; j < ; ++j){
printf("%4d", a[i][j]);
}
printf("\n");
}
printf("\nMaps resulting from layout #%d are:\n\n", kase);
dfs(, , );
printf("There are %d solution(s) for layout #%d.\n", cnt, kase);
}
return ;
}
UVA - 211 The Domino Effect(多米诺效应)(dfs回溯)的更多相关文章
- UVA 211 The Domino Effect 多米诺效应 (回溯)
骨牌无非两种放法,横着或竖着放,每次检查最r,c最小的没访问过的点即可.如果不能放就回溯. 最外面加一层认为已经访问过的位置,方便判断. #include<bits/stdc++.h> ; ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- UVa 524 Prime Ring Problem(DFS , 回溯)
题意 把1到n这n个数以1为首位围成一圈 输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的 n最大为16 利用回溯法 边生成边推断 就要快非常多了 #inc ...
- 「POJ 1135」Domino Effect(dfs)
BUPT 2017 Summer Training (for 16) #3G 题意 摆好的多米诺牌中有n个关键牌,两个关键牌之间有边代表它们之间有一排多米诺牌.从1号关键牌开始推倒,问最后倒下的牌在哪 ...
- uva 211(dfs)
211 - The Domino Effect Time limit: 3.000 seconds A standard set of Double Six dominoes contains 28 ...
- UVA211-The Domino Effect(dfs)
Problem UVA211-The Domino Effect Accept:536 Submit:2504 Time Limit: 3000 mSec Problem Description ...
- uva 193 Graph Coloring(图染色 dfs回溯)
Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...
- poj1270Following Orders(拓扑排序+dfs回溯)
题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...
- POJ 1135 -- Domino Effect(单源最短路径)
POJ 1135 -- Domino Effect(单源最短路径) 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两 ...
随机推荐
- 搭建PXE实现自动化安装系统
一.PXE工作原理 Ø Client向PXE Server上的DHCP发送IP地址请求消息,DHCP检测Client是否合法(主要是检测Client的网卡MAC地址),如果合法则返回Client的I ...
- 2月送书福利:ASP.NET Core开发实战
大家都知道我有一个公众号“恰童鞋骚年”,在公众号2020年第一天发布的推文<2020年,请让我重新介绍我自己>中,我曾说到我会在2020年中每个月为所有关注“恰童鞋骚年”公众号的童鞋们送一 ...
- Linux centos VMware Nginx防盗链、Nginx访问控制、Nginx解析php相关配置、Nginx代理
一.Nginx防盗链 配置如下,可以和上面的配置结合起来 location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|x ...
- 一步一步配置docker(tomcat+jenkins+phpmyadmin+nginx)
经过半个月的docker学习实践,今天对自己的学习成果做个总结. 貌似官方推荐的是docker compose使用DockerFile 来配置,但目前还没学习使用docker compose,先学习通 ...
- centos7一步一步搭建docker nginx 及重点讲解
系统环境:centos7.7 (VMware中) images版本:nginx:latest (截止2020.01.10最新版) 1.拉取镜像 docker pull nginx 2.启动nginx容 ...
- linux查漏补缺-linux命令行安装mysql
apt安装 sudo apt-get update sudo apt-get install mysql-server root@192:/sys/fs/cgroup# apt-get install ...
- 常用的UI控件
关于本文:作为一名iOS软件工程师,熟练规范的使用常用的UI控件是必备的基础技能. 指示器(UIActivityIndicatorView)----转动的等待小菊花 提醒对话框(UIAlertView ...
- web应用基础架构
1.web中间件 中间件是一类连接软件组件和应用的计算机软件,它包括一组服务.以便运行在一台或多台服务器上的多个软件通过网络进行交互.该技术所提供的互操作性,推动了一致分布式体系架构的演进,该架构通常 ...
- SystemVerilog基本语法总结(下)
2018年IC设计企业笔试题解析-(验证方向) 1.请简述:定宽数组,动态数组,关联数组,队列四种数据类型的各自特点.解析:(1)定宽数组:其宽度在声明的时候就指定了,故其宽度在编译时就确定了.(2) ...
- 解决Eclipse Debug 断点调试的source not found问题
写完代码进行调试的时候,经常会用到断点调试,一步步检测问题,但有时候eclipse有时候无法进入断点,这样就失去了断点的意义,原因是debug无法找到该项目的源代码,解决方法如下 1,打开debug ...