1. 程式人生 > 其它 >WOJ 51 Bus Route Problem題解

WOJ 51 Bus Route Problem題解

技術標籤:c++演算法backtrack

題目

Dongfang, A hardworking student, last week, he made the software named Query bus route system in WuHan, but he met a very intractability
problem At first, he tried to solve it by algorithm he had learned last summer, but it’s not appropriate for the MIS (Management
Information System). However, the function of the system has declined a lot if he used the database initial function. Now, you are

an excellent programmer, it is your task to help DongFang to improve the function of the software.
Assuming that there are n stations, whose names are the first n letters of the set{A, B, C, ? , Z, a, b, c, ?, z}. And in daily life,
to pay less fee, we often choose a bus route which contains D (1<=D<=4) buses at the most.

輸入格式
An integer T (0 < T < 20) at the first line of the file represents the number of cases. In each case, it begins with three integers m(1<=m<=50),
n(2<=n<=52), D, representing that there is m buses, n stations and the maximum number of buses that a route can contains. Then m lines,

each line begins with two integers k(1<=k<=m) and l, followed by l letters; each letter means the bus k can pass this station.
In the last line, there are two letters x and y, denoting the starting station and the destination.

輸出格式
Each case begins with (case i:),?i? is counted from 1,and each case ends with ?There are P routes in total!?, P is the number of
total routes from starting station to end station.
All of the P routes should be output in the following rules. First, the routes should be ordered by the number of stations. Second, if number of buses in two routes is the same, you should output the routes in the lexical order. Third, if two route?s stations are the same, the route whose first bus number is smaller should be output before the other one, if it is also the same, then see the next bus number??
The route?s first station must be the starting one, and the last station must be the end station. And the stations of the route should be different from each other.
The format of the route should as follows: A 1 B 2 C It means from starting station A, you take bus 1, on arriving at the station B, you take the bus 2, finally arrive at the destination C. There is a blank line between two neighboring case, in each case, each route should be output in separated line.

樣例輸入
1
3 3 4
1 2 A B
2 3 B A C
3 1 A
A C
樣例輸出
case 1:
A 2 C
A 1 B 2 C
A 2 B 2 C
There are 3 routes in total!

解析

注意兩個case輸出之間有個空行。。。我就是忘了這個一直沒提交通過