|
发表于 2013-11-8 14:31:07
|
显示全部楼层
- private class Node
- {
- public string id;
- public string path;
- public Node(string id, string path)
- {
- this.id = id; // this是Node类的对象
- this.path = path;
- }
- }
- static void Main(string[] args)
- {
- string idA = "849";
- string idB = "1048";
- DateTime start = DateTime.Now;
- string sql = "select * from exits where roomb is not null and roomb!=''";
- DataTable exits = F.getDT(sql);
- Queue PathNode = new Queue();
- PathNode.Enqueue(new Node(idA, ""));
- foreach (DataRow row in exits.Select("roomB=" + idA))
- exits.Rows.Remove(row);
- bool notfinded = true;
- while (exits.Rows.Count > 0 && PathNode.Count>0 && notfinded)
- {
- Node node = PathNode.Dequeue();//取出首部节点A
- foreach (DataRow row in exits.Select("roomA=" + node.id))
- {
- if (row["roomB"].ToString() == idB)
- {
- Console.WriteLine(node.path + row["dicAB"] + ":" + node.path.Split(';').Count());
- notfinded = false;
- break;
- }
- PathNode.Enqueue(new Node(row["roomB"].ToString(), node.path + row["dicAB"] + ";"));
- foreach (DataRow r in exits.Select("roomB=" + row["roomB"]))
- exits.Rows.Remove(r);
- }
- }
- Console.WriteLine((DateTime.Now-start).TotalMilliseconds);
- Console.ReadLine();
- }
复制代码
广度优先搜索 |
|