哈密顿绕行世界问题、n皇后问题
哈密顿绕行世界问题
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8325 Accepted Submission(s): 4892
Problem Description
Input
Sample Input
1 3 12
2 4 10
3 5 8
1 4 6
5 7 19
6 8 17
4 7 9
8 10 16
3 9 11
10 12 15
2 11 13
12 14 20
13 15 18
11 14 16
9 15 17
7 16 18
14 17 19
6 18 20
1 13 19
5
0
Sample Output
#include<stdio.h>
using namespace std; int map[][]; //记录每个城市的相邻的三个城市
int visited[]; //记录某个城市是否被访问过
int route[]; //记录某条路线经过的城市顺序 int cnt = ; //某一次探路
void dfs(int a, int len, int origin) //三个参数分别为此次探路的起始城市, 距离最初起点的距离, 和最初的起点
{
visited[a] = ; //将a城市标记为访问过
route[len] = a; //并将a放入路线中 for (int i = ; i < ; i++)
{
int t = map[a][i];
if (t == origin && len == ) //如果邻接点是原点且从原点出发经过19段路程,说明找到了一条路径
{
printf("%d: ", ++cnt);
for (int j = ; j <= len; j++) //打印这条路线
printf("%d ", route[j]);
printf("%d\n", origin);
} if (!visited[t])
dfs(t, len + , origin);
} visited[a] = ; //将a城市重新标记为未访问,方便下次探路
} int main()
{
int city; for (int i = ; i <= ; i++)
scanf_s("%d %d %d", &map[i][], &map[i][], &map[i][]);
while (scanf_s("%d", &city), city)
{
dfs(city, , city);
} }
以上转自:https://blog.csdn.net/libin56842/article/details/15028427
N皇后问题
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 38709 Accepted Submission(s): 16447
Problem Description
你的任务是,对于给定的N,求出有多少种合法的放置方法。
Output
Sample Input
8
5
0
92
10
#include <iostream>
#include <algorithm> using namespace std; int column[]; //每行的皇后的列号
int total = ; /*假设前k个皇后已经放好位置,放入第k+ 1个皇后*/
void Queue(int k, int N)
{
if (k == N)
total++; //排放次数加一 for (int i = ; i <= N; i++) //尝试每个列号
{
int j = ;
for (; j <= k; j++)
{
if (column[j] == i || (k + - j) == abs(i - column[j])) //k + 1的列号可能大于k的列号,所以要加绝对值
break;
} if (j == k + && k + <= N) //说明没有冲突
{
column[k + ] = i; //记录下第k + 1个皇后的列号
Queue(k + , N); //
}
} } int main()
{
int N; int chess[];
for (int i = ; i < ; i++)
{
total = ;
Queue(, i); //前0个皇后位置已摆好
chess[i] = total;
} while (cin >> N && N != )
{
total = chess[N];
cout << total << endl;
} return ;
}
哈密顿绕行世界问题、n皇后问题的更多相关文章
- HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDOJ 2181 哈密顿绕行世界问题
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 2181 哈密顿绕行世界问题 (DFS)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU2181:哈密顿绕行世界问题(DFS)
哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- 哈密顿绕行世界问题(dfs+记录路径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) ...
- 哈密顿绕行世界问题(hdu2181)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Hdu2181 哈密顿绕行世界问题 2017-01-18 14:46 45人阅读 评论(0) 收藏
哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- HDU - 2181 哈密顿绕行世界问题 dfs图的遍历
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU2181 哈密顿绕行世界问题 —— DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181 哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) ...
随机推荐
- map的循环删除操作
1.错误示例 Map<String,InterfaceOutParam> outCodes1 = outParamList.stream().collect(Collectors.toMa ...
- [MySQL]配置多个MySQL服务(Windows)
配置多个MySQL服务 1.复制原解压好的MySQL文件到本目录下,且改名为MySQL2 2.修改MySQL2文件夹中的my.ini 修改my.ini文件中的以下内容,并保存文件: [client] ...
- js日期的格式化
我们看控制台打印的关于Date这个类 我们这里可以看到内置方法没有类似format这种方法,所以需要自己定义. 内置的方法: var myDate = new Date();myDate.getYea ...
- Asp.net core 学习笔记 (AutoMapper)
参考 : http://www.cnblogs.com/xishuai/p/3700052.html http://www.cnblogs.com/xishuai/p/3704435.html htt ...
- php 安装 redis扩展
https://segmentfault.com/a/1190000009422920 wget 源码编译
- 『计算机视觉』Mask-RCNN_训练网络其一:数据集与Dataset类
Github地址:Mask_RCNN 『计算机视觉』Mask-RCNN_论文学习 『计算机视觉』Mask-RCNN_项目文档翻译 『计算机视觉』Mask-RCNN_推断网络其一:总览 『计算机视觉』M ...
- 解决Docker容器时区不一致的问题
查看linux系统时区和docker容器时区 date -R //查看linux主机时间和时区 date exec [container] date -R // 查看容器时间和时区 2者的时间差了8个 ...
- unity5.x中的关节和布料
关节 布料 关节 铰链关节(Hinge Joint):将两个物体以链条的形式绑在一起,当力量过大超过链条的固定力矩时,两个物体就会产生相互的拉力. 固定关节(Fixed Joint): ...
- CentOS 使用yum命令安装出现错误提示”could not retrieve mirrorlist http://mirrorlist.centos.org
CentOS 使用yum命令安装出现错误提示"could not retrieve mirrorlist http://mirrorlist.centos.org这个错误, 在网上找了好多, ...
- <airsim文档学习> Street View Image, Pose, and 3D Cities Dataset
原文地址: https://github.com/amir32002/3D_Street_View 说明:个人学习笔记,翻译整理自github/airsim. 简介 该存储库共享包含6DOF相机姿态 ...