Ultraviolet A 567: Risk

Source: Internet
Author: User

This is a simple graph topic. You only need to use the width-first search (BFS) to mark the distance between nodes.

My problem-solving code is as follows:

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <string> # include <algorithm> # include <queue> using namespace STD; int adj [20] [20]; int dis [20]; // indicates the distance between each point and S, if the value is-1, it indicates that this point has not been met by BFs. Int Sp (int s, int t) {// If BFS is used, the shortest distance from t to S is returned for memset (DIS,-1, sizeof (DIS); queue <int> q; DIS [s] = 0; q. push (s); While (! Q. empty () {int u = Q. front (); q. pop (); If (u = T) break; For (Int J = 0; j <20; j ++) if (DIS [J] <0 & adj [u] [J]) {q. push (j); DIS [J] = dis [u] + 1; // The distance from J to S is the distance from u to S + 1} return dis [T];} int main () {int x, t = 0; while (scanf ("% d", & X) = 1) {int TMP; memset (adj, 0, sizeof (adj); For (Int J = 0; j <X; j ++) {scanf ("% d", & TMP); adj [0] [tmp-1] = adj [tmp-1] [0] = 1 ;} for (INT I = 1; I <19; I ++) {scanf ("% d", & X); For (Int J = 0; j <X; j ++) {scanf ("% d", & TMP); adj [I] [tmp-1] = adj [tmp-1] [I] = 1 ;}} scanf ("% d", & TMP); printf ("Test Set # % d \ n", ++ T); int A, B; For (INT I = 0; I <TMP; I ++) {scanf ("% d", & A, & B); printf ("% 2D to % 2D: % d \ n ", a, B, SP (A-1, B-1);} printf ("\ n");} return 0 ;}

The attached questions are as follows:

Risk is a board game in which several opposing players attempt to conquer the world. the gameboard consists of a world map broken up into hypothetical countries. during a player's turn, armies stationed in one country
Are only allowed to attack only countries with which they share a common border. Upon conquest of that country, the armies may move into the newly conquered country.

During the course of play, a player often engages in a sequence of conquests with the goal of transferring a large mass of armies from some starting country to a destination country. typically, one chooses the intervening countries so as to minimize the total
Number of countries that need to be conquered. given a description of the gameboard with 20 countries each with between 1 and 19 connections to other countries, your task is to write a function that takes a starting country and a destination country and computes
The minimum number of countries that must be conquered to reach the destination. you do not need to output the sequence of countries, just the number of countries to be conquered including the destination. for example, if starting and destination countries
Are neighbors, then your program shoshould return one.

The following connection dimo-strates the first sample input.

Input

Input to your program will consist of a series of country configuration test sets. Each test set will consist of a board description on lines 1 through 19. The representation avoids listing every national boundary
Twice by only listing the fact that country I borders country J when I <j.
Thus, the ith line, where I is less
Than 20, contains an integer x indicating how many ''higher-numbered" countries share borders with country I,
Then X distinct integers J greater
Than I and not exceeding 20, each describing a boundary between countries I and j.
Line 20 of the test set contains a single INTEGER ()
Indicating the number of country pairs that follow. The next n lines each contain exactly two integers ()
Indicating the starting and ending countries for a possible conquest.

There can be multiple test sets in the input file; your program shocould continue reading and processing until reaching the end of file. there will be at least one path between any two given countries in every country configuration.

Output

For each input set, your program shocould print the following message''Test Set #T "where T is
The number of the test set starting with 1 (left-justified starting in column 11 ).

The nextNTLines each will contain the result for the corresponding test in the test set-that is, the minimum number of countries to conquer. The test result line shoshould contain
Start Country CodeARight-justified in columns 1 and 2; the string''To"In columns 3 to 6; the destination country codeBRight-justified in columns 7 and 8; The string'':"In columns 9 and 10; and a single integer
Indicating the minimum number of moves required to traverse from countryATo countryBIn the test set left-justified starting in column 11. Following all result lines of each input set, your program shocould print a single blank line.

Sample Input

1 32 3 43 4 5 61 61 72 12 131 82 9 101 111 112 12 171 142 14 152 15 161 161 192 18 191 201 2051 202 919 518 1916 204 2 3 5 61 43 4 10 55 10 11 12 19 182 6 72 7 82 9 101 91 102 11 143 12 13 143 18 17 134 14 15 16 170002 18 201 191 2061 208 2015 1611 47 132 16

Sample output

Test Set #1 1 to 20: 7 2 to  9: 519 to  5: 618 to 19: 216 to 20: 2Test Set #2 1 to 20: 4 8 to 20: 515 to 16: 211 to  4: 1 7 to 13: 3 2 to 16: 4

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.