uva 567 Risk bfs

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
int ma[25][25];
int d[25];
int vis[25];

int fun(int x,int y){
	queue<int> que;
	que.push(x);
	vis[x] = 1;
	d[x] = 0;

	while(1){
		int tm = que.front();
		que.pop();

		for(int i = 1;i <= 20;i++){
			if(ma[tm][i] && !vis[i]){
				if(i == y) {
					return d[tm] + 1;
				}
				else{
					vis[i] = 1;
					d[i] = d[tm]+1;
					que.push(i);
				}
			}
		}
	}
}
int main(){

	int n;
	int q = 1;

	while(cin >> n){
		memset(ma,0,sizeof(ma));

		for(int i = 0;i < n;i++){
			int y;

			cin >> y;
			ma[1][y] = 1;
			ma[y][1] = 1;
		}
		for(int i = 2;i < 20;i++){
			cin >> n;

			for(int j = 0;j < n;j++){
				int y;
				cin >> y;
				ma[i][y] = 1;
				ma[y][i] = 1;
			}
		} 

		int m;
		cin >> m;
		printf("Test Set #%d\n",q++);
		for(int i = 0;i < m;i++){
			int x,y;
			cin >> x >> y;
			memset(vis,0,sizeof(vis));
			memset(d,0,sizeof(d));
			printf("%2d to %2d: %d\n",x,y,fun(x,y));
		}
		cout << endl;

	}
	return 0;
}

时间: 2024-08-10 02:02:51

uva 567 Risk bfs的相关文章

uva 567 - Risk

 Risk  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 attac

UVA 567 Risk【floyd】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=508 题意:20个点的任意最短路.floyd 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <string> #include <iter

UVA - 567 Risk(Floyd)

题目链接 题目大意:有20个城市,输入给19行,每行先给有几个数,然后接着给出这几个数,代表的是后面的城市编号和行编号(城市编号)有一条边,每条边的权值为1.接着m个查询任意两个城市之间的最短距离. 解题思路:求任意两个顶点之间的距离,用floyd. 代码: #include <cstdio> const int maxn = 21; const int INF = 0x3f3f3f3f; int G[maxn][maxn]; void Floyd () { for (int k = 1; k

uva oj 567 - Risk(Floyd算法)

1 /* 2 一张有20个顶点的图上. 3 依次输入每个点与哪些点直接相连. 4 并且多次询问两点间,最短需要经过几条路才能从一点到达另一点. 5 6 bfs 水过 7 */ 8 #include<iostream> 9 #include<cstring> 10 #include<vector> 11 #include<cstdio> 12 #include<queue> 13 using namespace std; 14 15 struct

UVA 12130 - Summits(BFS+贪心)

UVA 12130 - Summits 题目链接 题意:给定一个h * w的图,每个位置有一个值,现在要求出这个图上的峰顶有多少个.峰顶是这样定义的,有一个d值,如果一个位置是峰顶,那么它不能走到不大于该峰顶高度 - d的位置,如果满足这个条件下,并且无法走到更高的山峰,那么它就是峰顶 思路:利用贪心的策略,把所有点丢到优先队列,每次取出最高的峰值开始找,进行广搜,搜的过程中记录下最大值的点的个数,如果这个是峰顶,就加上这个数.判断是不是峰顶的方法为,如果广搜过程中,不会找到一个点的能到的最高峰

uva 12130 - Summits(BFS)

题目链接:uva 12130 - Summits 题目大意:给定一个N?M的图,每个位置有一个值.给定D,表示有节点值为G的位置作为起点的话,将不能移动到值小于等于G?D的节点.现在要求找到整个图中所有的峰值点,峰值点的定义是不能移动到比自己节点值大的位置. 解题思路:将每个位置按照权值排序,逐个作为起点进行移动,v[x][y]数组值可以到达x,y的节点的值的最大值,如果起始点可以移动到v[x][y]大于本身的点,则说明该起点不是峰值点. #include <cstdio> #include

UVa 11624 Fire!(BFS 逃离火灾)

题意   n*m的迷宫中有一些着火点  每个着火点上下左右相邻的非墙点下一秒也将成为一个着火点  Joe每秒能向相邻的点移动一步  给你所有着火点的位置和Joe的位置  问Joe逃离这个迷宫所需的最小时间 可以先一遍bfs把每个点的最早着火时间存起来   只有Joe到达该点的时间小于这个时间Joe才能走这个点   只需要对Joe所在的点为起点再来一次bfs就行了   需要注意的是开始可能有多个着火点  我开始以为只有一个着火点被坑了好久 v[i][j]第一遍bfs记录点i,j的最早着火时间  第

UVA 439-Knight Moves(bfs)

Knight Moves Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that

Colour Hash (Uva 704 双向bfs)

Colour Hash Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description This puzzle consists of two wheels. Both wheels can rotate both clock and counter-clockwise. They contain 21 coloured pieces, 10 of which