题目大意:通俗点讲就是,输出所有从m城市出发,便利所有城市之后又能回到m城市的序列......

解题思路:DFS

1)用map[][]来存储城市之间的连通情况.用used[]存储某个城市的使用情况(即某一个城市是否被访问过).用res[]保存路径

。如res[count] = dep ; 表示的是第count不访问的城市是dep。用cas表示目标城市的序号

代码如下:

/*
* 2181_2.cpp
*
* Created on: 2013年8月22日
* Author: Administrator
*/ #include <iostream> using namespace std; /**
* map[][] :用来保存城市之间的连通情况
* used[] :用来 保存城市的使用情况
* res[] :用来保存路径
* cas :目标城市的标号
*/
const int maxn = 21;
bool map[maxn][maxn];
bool used[maxn];
int res[maxn];
int cas;
int num = 1; /**
* dep :当前访问城市
* count :当前访问的城市数
*/
void dfs(int dep, int count) {
int i; //第count个所到达的城市是dep
res[count] = dep;
if (count == 19) { //19步能涉及20个城市
if (map[dep][cas]) {
printf("%d: ", num++);
for (i = 0; i < 20; ++i) {
printf(" %d", res[i]);
}
printf(" %d", cas);
printf("\n");
} return;
} for (i = 1; i <= 20; ++i) {
/**
* map[dep][j] :从城市dep到城市j的连通情况
*/
//如果城市dep与城市j连通&&城市j没有使用过
if (map[dep][i] && !used[i]) {
used[i] = true;
dfs(i, count + 1);
used[i] = false;
}
}
}
int main() {
memset(map, 0, sizeof(map));
memset(res, 0, sizeof(res));
memset(used, false, sizeof(used));
int i;
int a, b, c;
for (i = 1; i <= 20; ++i) {
scanf("%d%d%d", &a, &b, &c);
map[i][a] = true;
map[i][b] = true;
map[i][c] = true;
} while (scanf("%d", &cas) != EOF, cas) {
used[cas] = true;
dfs(cas, 0);
}
}

最新文章

  1. &quot;传成老树白茶&quot;献礼母亲节 邀市民品茗感受茶文化
  2. 回忆:#define的用法
  3. [.net 面向对象编程基础] (23) 结束语
  4. LigerUI学习使用
  5. Spring整合Junit4
  6. Codeforces Beta Round #5
  7. Java里的if else语句例子
  8. Linux下Tomcat重新启动
  9. Linux 封闭端口和安全
  10. Myeclipse2014 自带的报表功能 与 Eclipse BIRT
  11. Shell根据年月日创建文件夹
  12. MinGW 和 MSVC 下,使用 FILE 类型的一个奇怪的问题
  13. 非自定义和自定义Dialog的介绍!!!
  14. semver语义化版本号
  15. mysql 插入Emoji表情报错
  16. MySQL的JOIN(五):JOIN优化实践之排序
  17. CSS div阴影效果
  18. SpringBoot基础系列-SpringBoot配置
  19. 使用 gzexe 快速加密解密文件内容
  20. 完全关闭及再次启动cdh集群

热门文章

  1. RPM制作
  2. ASP.NET 微信支付
  3. List指定字段赋特定值(非循环) asp.net
  4. java simple check whether a file or directory.
  5. c# 连接oracle 读取数据
  6. 怎样在官网上下载xcode7.2
  7. js判断是否在iframe中
  8. 将日期和时间作为 struct tm型的值直接向二进制文件进行读写
  9. opencv 常用函数介绍
  10. saiku