Ultraviolet A 208-firetruck, two-way search for pruning

Source: Internet
Author: User

208-firetruck 6675 16.87% 1534 54.76%

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 108 & page = show_problem & problem = 144

Type: backtracking


Original question:

The Center City Fire Department collaborates with the transportation department to maintain maps of the city which reflects the current status of the city streets. On any given day, several streets are
Closed for repairs or construction. Firefighters need to be able to select routes from the firestations to fires that do not use closed streets.

Central City is divided into non-overlapping fire districts, each containing a single firestation. When a fire is reported, a central dispatcher alerts the firestation of the district where the fire is
Located and gives a list of possible routes from the firestation to the fire. You must write a program that the central dispatcher can use to generate routes from the district firestations to the fires.

Input

The city has a separate map for each fire district. streetcorners of each map are identified by positive integers less than 21, with the firestation always on corner #1. The input file contains several
Test cases representing different fires in different districts.

  • The first line of a test case consists of a single Integer Which is the number of the streetcorner closest to the fire.
  • The next several lines consist of pairs of positive integers separated by blanks which are the adjacent streetcorners of open streets. (For example, if the pair 4 7 is on a line in the file, then the Street between streetcorners 4 and 7 is open. there are
    No other streetcorners between 4 and 7 on that section of the street .)
  • The final line of each test case consists of a pair of 0's.
Output

For each test case, your output must identify the case by number (Case #1,Case #2, Etc). It must list each route on a separate line, with the streetcorners written in the order in which
They appear on the route. and it must give the total number routes from firestation to the fire. include only routes which do not pass through any streetcorner more than once. (For obvious reasons, the fire department doesn't want its trucks driving around
In circles .)

Output from separate cases must appear on separate lines.

The following sample input and corresponding correct output represents two test cases.

Question:

The city fire Center cooperates with the transportation department to obtain the real-time status of city streets.

Every day, some streets will be closed due to repair or construction. To save time and avoid waste, firefighters need to select a route from the closed Street to the place where the fire occurred.

The city is divided into non-overlapping fire zones. Each fire zone is under the responsibility of a fire station.

When a fire is triggered, the center dispatcher mm sends an alarm to the fire station in the region, and provides multiple routes from the fire station to the place where the fire occurred.

You want to chase the dispatcher mm, so you decided to write a program to help the dispatcher mm generate the route from the fire station to the disaster area.

Each fire zone in the city has a separate map.

The street intersection on the map is marked as an integer of no more than 21 (the number of street intersections on each map cannot exceed 21), and the fire station on each map

All numbers are 1 (on corner #1 ).

Analysis and Summary:

When I saw this question, I thought it was very difficult to see the question rate so low, but I thought it was very easy to see the question. I wrote it soon, but it was TLE after I submitted it...

Here tle...

Again tle...

So I asked Gu Ge and learned something new.

The reason for timeout is that there may be a lot of roads that cannot reach the target. For example, Nanjing has to go north to Beijing, but there are many roads,

In addition to the road to the north, there are also the road to the south, the West, the east, and the road to go in these directions is undoubtedly the north of the South, and it is impossible to go to Beijing even if it is around the earth

The end is reached, but the daylily is already cold (the dispatcher mm is waiting for you ~~). The reason for timeout lies in this.

Therefore, before you start searching from the start point, it is necessary to determine which paths can reach the target.

How can we determine which paths can reach the goal? We only need to start searching from the target point and mark all the searched paths.

Then, search from the start point. Before searching, you must first determine whether the path has been marked before. If it is not marked, it means it is impossible.

Go to the target. In this way, it will not blindly go, but also greatly improve the efficiency.

Code:

# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # define maxn 22 using namespace STD; int G [maxn] [maxn], route [maxn], edge [maxn], CNT, end, numroute; bool vis [maxn], used [maxn], occur [maxn]; void search (INT cur, int U) {If (u = END) {++ numroute; printf ("% d", route [0]); For (INT I = 1; I <cur; + + I) printf ("% d", route [I]); printf ("\ n"); return;} int V; For (INT I = 1; I <CNT; ++ I) if (used [E DGE [I]) {// determines whether this route can reach the end point int v = edge [I]; If (G [u] [v] &! Vis [v]) {vis [v] = true; route [cur] = V; search (cur + 1, V); vis [v] = false ;}}} int que [10000]; // pre-processing of BFS. You can also use DFS to void BFS (int u) {int front = 0, rear = 0; que [rear ++] = u; while (front <rear) {int T = que [Front ++]; for (INT I = 0; I <CNT; ++ I) if (! Used [edge [I] & G [T] [edge [I]) {used [edge [I] = true; // The flag is true que [rear ++] = edge [I] ;}} int main () {# ifdef local freopen ("input.txt", "r ", stdin); # endif int U, V, CAS = 1; while (scanf ("% d", & End )! = EOF) {memset (G, 0, sizeof (g); memset (occur, 0, sizeof (occur); While (scanf ("% d ", & U, & V )! = EOF) {If (! U &&! V) break; occur [u] = occur [v] = true; ++ G [u] [v]; ++ G [v] [u];} CNT = 0; For (INT I = 1; I <maxn; ++ I) if (occur [I]) {edge [CNT ++] = I ;} memset (VIS, 0, sizeof (VIS); memset (used, 0, sizeof (used); vis [end] = true; used [end] = true; BFS (end); printf ("case % d: \ n", CAS ++); memset (VIS, 0, sizeof (VIS); route [0] = 1; vis [1] = true; numroute = 0; search (1, 1); printf ("there are % d routes from the firestation to streetcorner % d. \ n ", numroute, end);} return 0 ;}

-- The meaning of life is to give it meaning.

 

Original Http://blog.csdn.net/shuangde800 , By d_double(For details, refer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.