转载一个图数据结构题目
2009-03-31 17:11
204 查看
用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
采用二维数组定义图结构,最后的代码是:
import java.util.Iterator;
import java.util.TreeSet;
public class TestQuestion {
private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean
;
private int[][] a = new int
;
private String result = "";
private TreeSet set = new TreeSet();
public static void main(String[] args) {
new TestQuestion().start();
}
private void start() {
// Initial the map a[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}
// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;
// Begin to depth search.
for (int i = 0; i < n; i++) {
this.depthFirstSearch(i);
}
// Print result treeset.
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
// "4" can not be the third position.
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}
private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
// Filt the duplicate value.
set.add(result);
}
for(int j = 0; j < n; j++) {
if (a[startIndex][j] == 1 && visited[j] == false) {
depthFirstSearch(j);
} else {
continue;
}
}
// restore the result value and visited value after listing a node.
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}
转自 javaeye:http://www.javaeye.com/topic/55873
采用二维数组定义图结构,最后的代码是:
import java.util.Iterator;
import java.util.TreeSet;
public class TestQuestion {
private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean
;
private int[][] a = new int
;
private String result = "";
private TreeSet set = new TreeSet();
public static void main(String[] args) {
new TestQuestion().start();
}
private void start() {
// Initial the map a[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}
// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;
// Begin to depth search.
for (int i = 0; i < n; i++) {
this.depthFirstSearch(i);
}
// Print result treeset.
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
// "4" can not be the third position.
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}
private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
// Filt the duplicate value.
set.add(result);
}
for(int j = 0; j < n; j++) {
if (a[startIndex][j] == 1 && visited[j] == false) {
depthFirstSearch(j);
} else {
continue;
}
}
// restore the result value and visited value after listing a node.
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}
转自 javaeye:http://www.javaeye.com/topic/55873
相关文章推荐
- 转载 一个题目涉及到的50个Sql语句
- 转载一个C++题目
- 转载的pku题目分类(比较全的一个版本)
- 算法与数据结构题目的 PHP 实现:栈和队列 设计一个有 getMin 功能的栈
- 一个数据结构FOR循环题目
- 小蚂蚁学习数据结构(27)——题目——一个关于链表的题目
- 转载 一个题目涉及到的50个Sql语句
- 数据结构:题目(3)测试一个数组序列是否是二叉树的前序遍历或者后序遍历结果
- 【程序18】 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时 共有5个数相加),几个数相加有键盘控制
- 数据结构---设计一个栈,push, pop, min 时间复杂度都是 O(1)
- 一个有趣的题目[CF730G|2016ACM NEERC G] Car Repair Shop
- 第一道题目:一个美国人在菜市场上做生意。第一次,8美元买了一只鸡,9美元卖掉了;第二次,10美元买了同样的一只鸡,11美元又卖掉了。
- 转载:如何爱一个女人
- 算法和数据结构就是编程的一个重要部分,你若失掉了算法和数据结构,你就把一切都失掉了
- 一个女孩,自我陶醉的文章(转载)
- 【数据结构上机练习】考试题目 3
- 【引用】[转载]如何做一个出色的程序员
- 如何构造一个简单的USB过滤驱动程序(转载)
- 题目:已知一个完全二叉树的结点数为770,问完全二叉树的叶子结点数为多少?
- 一个数据结构与算法可视化的网站