哈密顿绕行世界问题、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) ...
随机推荐
- 非递归遍历N-ary树Java实现
2019-03-25 14:10:51 非递归遍历二叉树的Java版本实现之前已经进行了总结,这次做的是非递归遍历多叉树的Java版本实现. 在非递归遍历二叉树的问题中我个人比较推荐的是使用双whil ...
- HTML:foreach
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- Excel中针对IP地址的排序方法
新建一个辅助排序列,用辅助列来扩展,辅助列公式如下: =TRIM(TEXT(LEFT(SUBSTITUTE(A1,".",REPT(" ",99)),100), ...
- Win10系列:C#应用控件基础12
TextBlock控件 TextBlock控件是应用程序开发过程中经常使用的控件之一,它的主要功能是显示一段只读的文本内容.开发者可以使用TextBlock控件来显示提示信息,还可以根据需求将显示的提 ...
- jsp九大内置对象 ,三大指令,四大作用域,七大动作
九大内置对象: application:应用程序对象 对整个web工程都有效 request:对当前请求的封装 pageConfig:只对当前页面有效,里面封装了基本request和session的对 ...
- redis缓存工具类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis ...
- 移动端H5拍照代码实现及外网部署
最近的工作中,遇到了一个需求:对于无APP登陆权限的人员,提供拍照上传功能,以便生成更完善的出工记录.经研究讨论,决定实现的机制为:由合法的人员登陆APP认领相关工作任务,并生成当天当工作的唯一二维码 ...
- ADO.NET之SqlConnection、sqlcommand的应用(学习笔记)
一.知识描述点 1.SqlConnection (1)使用SqlConnection类可以连接到SQL Server数据库.SqlConnection对象的主要属性和方法如下: ——属性:Connec ...
- JAVA集合接口及类
各接口及类关系图 Iterable 所有集合的初始接口,实现该接口可进行foreach操作,只有一个iterator()方法,并返回iterator类型: Iterable在java.lang下,It ...
- vue Axios 封装与配置项
import axios from "axios"; import qs from "qs"; import { Message } from "el ...