这是一道很简单的图论题,只要使用宽度优先搜索(BFS)标记节点间距离即可。
我的解题代码如下:
#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]; //为正时表示各点到s的距离,为-1时表示该点还未被bfs遇到
int SP(int s,int t)
{//使用BFS,返回t到s的最短距离
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; //j到s的距离为u到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 %d",&A,&B);
printf("%2d to %2d: %d\n",A,B,SP(A-1,B-1));
}
printf("\n");
}
return 0;
}
附上题目如下:
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 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 I th 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 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.
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 (left-justified starting in column 11).
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 right-justified in columns 1 and 2; the string `` to " in columns 3 to 6; the destination country code B right-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 country A to countryB in the test set left-justified starting in column 11. Following all result lines of each input set, your program should print a single blank line.
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 2 3 5 6
1 4
3 4 10 5
5 10 11 12 19 18
2 6 7
2 7 8
2 9 10
1 9
1 10
2 11 14
3 12 13 14
3 18 17 13
4 14 15 16 17
0
0
0
2 18 20
1 19
1 20
6
1 20
8 20
15 16
11 4
7 13
2 16
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
Test Set #2
1 to 20: 4
8 to 20: 5
15 to 16: 2
11 to 4: 1
7 to 13: 3
2 to 16: 4