Risk

http://acm.zju.edu.cn/show_problem.php?pid=1221

Time limit: 1 Seconds

Memory limit: 32768K

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 should return one.

The following connection diagram illustrates the sample input.

1221.gif

1. 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 (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.

There can be multiple test sets in the input; 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.

2. Output

For 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.

3. Sample Input

1 3
2 3 4
3 4 5 6
1 6
1 7
2 12 13
1 8
2 9 10
1 11
1 11
2 12 17
1 14
2 14 15
2 15 16
1 16
1 19
2 18 19
1 20
1 20
5
1 20
2 9
19 5
18 19
16 20

4. Sample Output

Test Set #1
1 to 20: 7
2 to 9: 5
19 to 5: 6
18 to 19: 2
16 to 20: 2

Problem Source: South Central USA 1997


   1 //using dijkstra
   2 //2007-01-10 19:59:33 Accepted 1221 C++ 00:00.00 388K 
   3 //by 曹高挺
   4 
   5 #include <stdio.h>
   6 #include <string.h>
   7 
   8 #define INFINITE        10000
   9 
  10 bool map[21][21];
  11 bool found[21];                         //是否找到到i的最段路径
  12 int dist[21];                           //到from 到 i的当前以知最段路径
  13 
  14 int Dijkstra_path(int from, int to){
  15         int i, j;
  16         int mindist;
  17         int nearest;
  18         memset(found, false, sizeof(bool)*21);
  19         found[from] = true;
  20         for(j=1; j<=20; j++){
  21                 if(map[from][j])
  22                         dist[j] = 1;
  23                 else    dist[j] = INFINITE;
  24         }
  25         for(i=2; i<=20 && !found[to]; i++){
  26         //找到不包含在S集中且距离from点最近的点
  27                 mindist = INFINITE;
  28                 for(j=1; j<=20; j++){
  29                         if( !found[j] && dist[j]<mindist ){
  30                                 mindist = dist[j];
  31                                 nearest = j;
  32                         }
  33                 }
  34                 //将该点包含在S集中
  35                 found[nearest] = true;
  36                 //观察该点到其余点的距离,并更新到其余点的距离
  37                 for(j=1; j<=20; j++){
  38                         if( !found[j] && map[nearest][j] && (mindist+1<dist[j]) ){
  39                                 dist[j] = mindist+1;
  40                         }
  41                 }
  42         }
  43         
  44         return dist[to];
  45 }
  46 
  47 bool GetInfo(){
  48         int i, cnt, n;
  49         int tem;
  50         if( scanf("%d", &n) == EOF ){
  51                 return false;
  52         }
  53         memset(map, false, sizeof(bool)*21*21);
  54         cnt = 1;                        //当前城市ID
  55         for(i=0; i<n; i++){
  56                 scanf("%d", &tem);
  57                 map[cnt][tem] = true;
  58                 map[tem][cnt] = true;
  59         }
  60 
  61         for(cnt=2; cnt<=19; cnt++){
  62                 scanf("%d", &n);
  63                 for(i=0; i<n; i++){
  64                         scanf("%d", &tem);
  65                         map[cnt][tem] = true;
  66                         map[tem][cnt] = true;
  67                 }
  68         }       
  69         return true;
  70 }
  71 
  72 int main(){
  73         int i, n;
  74         int from, to;
  75         int k = 0;
  76         while( GetInfo() ){
  77                 printf("Test Set #%d\n", ++k);
  78                 scanf("%d", &n);
  79                 for(i=0; i<n; i++){
  80                         scanf("%d %d", &from, &to);
  81                         printf("%d to %d: %d\n", from, to, Dijkstra_path(from, to));
  82                 }
  83                 printf("\n");
  84         }
  85 
  86         return 1;
  87 }

   1 //dfs 方法
   2 #include <iostream>
   3 #include <algorithm>
   4 using namespace std;
   5 
   6 class Country{
   7 public:
   8         int paths[21];
   9 public:
  10         Country(){
  11                 memset(paths, -1, sizeof(int)*21);
  12         }
  13 };
  14 
  15 //int resTable[21][21];
  16 int curPath[21];  //
  17 Country counList[21];
  18 
  19 int Min(int a, int b){
  20         return a<b?a:b;
  21 }
  22 
  23 int dist(int a, int b){
  24         if(a == b){
  25                 return 0;
  26         }
  27         //将其加入当前路径
  28           curPath[a]=1;
  29         //
  30         Country* pc;
  31         int minDist=100;
  32         pc=&counList[a];
  33         for(int i=1; i<=20; i++){
  34                 if(pc->paths[i]==1 && curPath[i]==-1){
  35                         minDist=Min(minDist, dist(i, b)+1);
  36                 }
  37         }
  38         //撤出当前路径
  39         curPath[a]=-1;
  40         return minDist;
  41 }
  42 
  43 bool GetInfo(){
  44         int num, i, j, tem1;
  45         //memset(resTable, -1, sizeof(int)*21*21);
  46         memset(curPath, -1, sizeof(int)*21);
  47         for(i=0; i<21; i++){
  48                 memset(counList[i].paths, -1, sizeof(int)*21);
  49         }
  50         if( !(cin >> num) )     return false;
  51         for(i=1; i<=num; i++){
  52                 cin>>tem1;
  53                 counList[1].paths[tem1]=1;
  54                 counList[tem1].paths[1]=1;
  55         }
  56         for(i=2; i<=19; i++){
  57                 cin>>num;
  58                 for(j=1; j<=num; j++){
  59                         cin>>tem1;
  60                         counList[i].paths[tem1]=1;
  61                         counList[tem1].paths[i]=1;
  62                 }
  63         }
  64 
  65         return true;
  66 }
  67 
  68 int main(){
  69         int i, tem1, tem2;
  70         int nCase, nCaseNum=0;
  71         //get node info   end by EOF
  72         while(GetInfo()==true){////
  73                 nCaseNum++;
  74                 cin>>nCase;
  75                 //output
  76                 cout<<"Test Set #"<<nCaseNum<<endl;
  77                 for(i=1; i<=nCase; i++){
  78                         cin>>tem1>>tem2;
  79                         cout<<tem1<<" to "<<tem2<<": "<<dist(tem1, tem2)<<endl;
  80                 }
  81                 cout<<endl;
  82         }
  83         return 1;
  84 }

zju1221 (2008-02-23 15:37:07由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.