哈密顿绕行世界问题、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) ...
随机推荐
- React项目中使用HighCharts
大家都知道BizCharts是基于react封装的一套图表工具,而HighCharts是基于jQuery的.但是由于本人对BizCharts甚是不熟,所以在react项目开发中选择了HighChart ...
- Vnpy二次开发应用所需图标
在针对Vnpy二次开发时,很多窗口中需要使用到“小图标” 给大家分享一个UI的专业图标网,上面资源齐全. https://www.iconfont.cn/collections?personal=1
- ubuntu 关闭 笔记本键盘背景灯
/etc/rc.local 加入 ' > /sys/class/leds/tpacpi::kbd_backlight/brightness
- linux安装redis ,mariadb
linux下安装软件方法 1 rpm (不推荐使用) 2 yum 安装(非常方便快捷) 3 编译安装(需要自定制的时候才使用) 安装mariadb(mysql) 1 使用官方源安装mariadb vi ...
- ES6 新加的类型Symbol()
Symbol()如果一个对象中的键已经存在,但目前我们目前不知道这个键是存在的,然后我们去给这个键赋值,Symbol()就不会改变这个键对应的值
- PAT 1092 To Buy or Not to Buy
1092 To Buy or Not to Buy (20 分) Eva would like to make a string of beads with her favorite colors ...
- html5(三)
body{ text-align:center;} #mainbox { display:block; width:500px; margin:50px auto; padding:15px; tex ...
- ceph Luminous版手动安装零散记录
1.安装必要的依赖包,关防火墙,向/etc/hosts内添加域名等 2.安装ceph 配置yum源 (如果嫌慢,可以配置cachedir=/home/yum/$basearch/$releasever ...
- Python 3.6.5 导入pymysql模块出错:No module named 'pymysql'
检查一下项目设置中的解释器. 查看是否添加PyMySQL模块,如果没有请添加PyMySQL模块
- 如何发布自己的node模块
一.注册自己的npm账户 在 npm 官网 https://www.npmjs.org 申请一个账号,并且进行必要的验证,需要填写用户名.密码和邮箱.并且注册成功后会向邮箱发一封邮件,需要进行验证. ...