POJ 1603 Risk
1
/**/
/*
2POJ 1603 Risk
3
4
5----问题描述:
6
7Risk 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.
8
9During 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 should return one.
10
11The following connection diagram illustrates the first sample input.
12
13
14----输入:
15
16Input 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 (1 <= N <= 100) indicating the number of country pairs that follow. The next N lines each contain exactly two integers (1 <= A,B <= 20; A!=B) indicating the starting and ending countries for a possible conquest.
17
18There can be multiple test sets in the input file; your program should 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.
19
20
21----输出:
22
23For each input set, your program should print the following message "Test Set #T" where T is the number of the test set starting with 1. The next NT lines 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 should contain the start country code A the string " to " the destination country code B ; the string ": " and a single integer indicating the minimum number of moves required to traverse from country A to country B in the test set. Following all result lines of each input set, your program should print a single blank line.
24
25
26----样例输入:
27
281 3
292 3 4
303 4 5 6
311 6
321 7
332 12 13
341 8
352 9 10
361 11
371 11
382 12 17
391 14
402 14 15
412 15 16
421 16
431 19
442 18 19
451 20
461 20
475
481 20
492 9
5019 5
5118 19
5216 20
53
54
55----样例输出:
56
57Test Set #1
581 to 20: 7
592 to 9: 5
6019 to 5: 6
6118 to 19: 2
6216 to 20: 2
63
64
65----分析:
66
67Floyd 算法。
68
69
70*/
71
72
73 #include < stdio.h >
74 #include < string .h >
75
76 #define N 23
77 #define INF 0x3F3F3F3F
78
79 int main() {
80 int n, w[ N ][ N ], i, j, k, td = 0;
81 while ( 1 == scanf( "%d", &k ) ) {
82 memset( w, 0x3F, sizeof(w) );
83 for ( i = 0; i < k; ++i ) {
84 scanf( "%d", &j );
85 w[ 1 ][ j ] = w[ j ][ 1 ] = 1;
86 }
87 for ( i = 2; i <= 19; ++i ) {
88 scanf( "%d", &k );
89 while ( 0 < k-- ) {
90 scanf( "%d", &j );
91 w[ i ][ j ] = w[ j ][ i ] = 1;
92 }
93 }
94
95 for ( k = 1; k <= 20; ++k ) {
96 for ( i = 1; i <= 20; ++i ) {
97 for ( j = 1; j <= 20; ++j ) {
98 if ( (i != j) && (i != k) && (k != j) ) {
99 if ( w[ i ][ j ] > w[ i ][ k ] + w[ k ][ j ] ) {
100 w[ i ][ j ] = w[ i ][ k ] + w[ k ][ j ];
101 }
102 }
103 }
104 }
105 }
106
107 scanf( "%d", &n );
108 printf( "Test Set #%d\n", ++td );
109 while ( 0 < n-- ) {
110 scanf( "%d%d", &i, &j );
111 printf( "%d to %d: %d\n", i, j, w[ i ][ j ] );
112 }
113 printf( "\n" );
114 }
115 return 0;
116}
117
2POJ 1603 Risk
3
4
5----问题描述:
6
7Risk 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.
8
9During 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 should return one.
10
11The following connection diagram illustrates the first sample input.
12
13
14----输入:
15
16Input 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 (1 <= N <= 100) indicating the number of country pairs that follow. The next N lines each contain exactly two integers (1 <= A,B <= 20; A!=B) indicating the starting and ending countries for a possible conquest.
17
18There can be multiple test sets in the input file; your program should 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.
19
20
21----输出:
22
23For each input set, your program should print the following message "Test Set #T" where T is the number of the test set starting with 1. The next NT lines 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 should contain the start country code A the string " to " the destination country code B ; the string ": " and a single integer indicating the minimum number of moves required to traverse from country A to country B in the test set. Following all result lines of each input set, your program should print a single blank line.
24
25
26----样例输入:
27
281 3
292 3 4
303 4 5 6
311 6
321 7
332 12 13
341 8
352 9 10
361 11
371 11
382 12 17
391 14
402 14 15
412 15 16
421 16
431 19
442 18 19
451 20
461 20
475
481 20
492 9
5019 5
5118 19
5216 20
53
54
55----样例输出:
56
57Test Set #1
581 to 20: 7
592 to 9: 5
6019 to 5: 6
6118 to 19: 2
6216 to 20: 2
63
64
65----分析:
66
67Floyd 算法。
68
69
70*/
71
72
73 #include < stdio.h >
74 #include < string .h >
75
76 #define N 23
77 #define INF 0x3F3F3F3F
78
79 int main() {
80 int n, w[ N ][ N ], i, j, k, td = 0;
81 while ( 1 == scanf( "%d", &k ) ) {
82 memset( w, 0x3F, sizeof(w) );
83 for ( i = 0; i < k; ++i ) {
84 scanf( "%d", &j );
85 w[ 1 ][ j ] = w[ j ][ 1 ] = 1;
86 }
87 for ( i = 2; i <= 19; ++i ) {
88 scanf( "%d", &k );
89 while ( 0 < k-- ) {
90 scanf( "%d", &j );
91 w[ i ][ j ] = w[ j ][ i ] = 1;
92 }
93 }
94
95 for ( k = 1; k <= 20; ++k ) {
96 for ( i = 1; i <= 20; ++i ) {
97 for ( j = 1; j <= 20; ++j ) {
98 if ( (i != j) && (i != k) && (k != j) ) {
99 if ( w[ i ][ j ] > w[ i ][ k ] + w[ k ][ j ] ) {
100 w[ i ][ j ] = w[ i ][ k ] + w[ k ][ j ];
101 }
102 }
103 }
104 }
105 }
106
107 scanf( "%d", &n );
108 printf( "Test Set #%d\n", ++td );
109 while ( 0 < n-- ) {
110 scanf( "%d%d", &i, &j );
111 printf( "%d to %d: %d\n", i, j, w[ i ][ j ] );
112 }
113 printf( "\n" );
114 }
115 return 0;
116}
117