UVa 208:firetruck, bidirectional search for pruning

Source: Internet
Author: User
Tags cas integer printf

Topic Link:

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

Type: Backtracking method

Original title:

The Center City Fire Department collaborates and the Transportation Department to maintain maps of the city which s current status is the city streets. On no given day, several streets are closed for repairs or construction. Firefighters need to is able to select routes from firestations to fires.

The divided into Non-overlapping fire districts, and each containing a single firestation. When a fire is reported, a dispatcher alerts the firestation to the district where the fire is located and gives a List of possible routes from the firestation to the fire. You are must write a program that the "dispatcher can use" to generate routes from the district to the Firestations S.

Input

The city has a separate map for each fire district. Streetcorners of each map are identified by positive integers less and than firestation on always #1. The input file contains several test cases representing different fires in different.

The "the" 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 O Pen streets. (for example, if the pair 4 7 are on a line in the file, then the street between Streetcorners 4 and 7 are open. There are no other streetcorners between 4 and 7 on this 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). The It must list each route on a separate line, with the streetcorners written in the order of which they on the appear. And it must give the total number routes from firestation to the fire. Include only routes which don't pass through any streetcorner more than once. (For obvious reasons, the fire department doesn ' t want it 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.

The main effect of the topic:

The City Fire Center has cooperated with the Traffic department in order to obtain the real-time status of the city streets.

Every day there are some streets closed because of repairs or construction, so in order to save time and not go long way, firefighters need to choose a road without a closed street to reach the place of fire.

The city is divided into not overlapping fire zones, each of which is held by a fire station.

When a fire occurs, the center dispatcher, MM, sends an alert to a fire station that governs the area, and offers a number of routes from the fire station to the place of fire.

You want to chase dispatcher mm, so you decide to write a program to help center 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 (Streetcorner) on the map is marked as an integer not exceeding 21 (the number of street junctions per map does not exceed 21), and the fire station for each map

are numbered 1 (on corner #1).

Analysis and Summary:

See this problem, see the problem rate so low think difficult, but see finish the topic feel very easy, soon wrote, submitted but tle ...

In this tle ...

Once again tle ...

So he asked Google and learned something new.

The reason for the timeout is that there may be many ways to reach the target, for example, Nanjing to Beijing, need to go north, but the road is very much,

In addition to the north side of the road, and to the south, west, east of the road, and walk these directions is definitely the opposite, go to die can not reach Beijing, even if around the earth circle

Reach the end, but the day Lily has already been cold (dispatcher mm still waiting for you ~ ~). And the reason for the timeout is here.

So, before starting the search, it's important to make sure that there are those paths that can reach the target.

How do you determine which roads can be targeted? We only need to start from the target point of the search, all the search to get the path to mark.

Then, from the beginning of the search, before the search, to determine whether the path has been marked before, if not marked, then it is impossible

Go to the target place. In this way, it will not blindly go away, 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[edge[i]) {//backtracking to determine whether the path can reach the endpoint 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];  
    BFS preprocessing, with DFS can also 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 passed 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%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; }

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.