牛客假日团队赛7 A:Oh Those Rollers (暴力)

链接:https://ac.nowcoder.com/acm/contest/997/A
 

题目描述

Farmer John has installed a new winch that gives him mechanical advantage when lifting bales of hay into the barn. The winch was manufactured by the Rube Goldberg Winch Company and has way too many rollers to make any sense at all. The winch has a huge steel plate with a number of rollers whose ultimate source of power is the drive-roller whose location FJ has denoted as the origin (0,0). This roller drives a roller that drives another roller, etc. etc. until the final roller is driven. FJ is trying to find that final roller and wants to know which one it is.
FJ has recorded the xi,yi (-5,000 <= xi <= 5,000; -5,000 <= yi <= 5,000) coordinates and the radius ri (3 <= ri <= 1024) of each of the N (2 <= N <= 1080) rollers. Tell him the x,y coordinate of the last roller in the chain (the roller that is driven but drives no other roller). Every roller except the drive-roller is driven by exactly one other roller.

输入描述:

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 describes roller i with three space separated integers: xi, yi, and ri

输出描述:

* Line 1: A single line with two space-separated integers that are respectively the x,y coordinates of the last roller in the chain of driven rollers.

示例1

输入

复制

3
0 0 30
30 40 20
-15 100 55

输出

复制

-15 100

说明

Three rollers. First is at the origin with radius 30. It drives the roller at (30,40) whose radius is 20. That roller in turn drives the third roller located at (-15,100) with radius 55.

题意分析:

有n个齿轮,其中位于0 0的是主齿轮,除了主齿轮外其他每个齿轮都要依靠其他齿轮才能转动,求最后一个齿轮的位置。

解题思路:

暴力找只有一个相邻齿轮且不是主齿轮的位置。

#include <stdio.h>
#include <queue>
#define N 2000
using namespace std;
struct date {
	int x;
	int y;
	int r;
}a[N];
bool book[N];
int main()
{
	int i, n, u, ans, s;
	scanf("%d", &n);
	queue<int>q;
	for (i = 1; i <= n; i++)
	{
		scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].r);
		if (a[i].x == 0 && a[i].y == 0)
		{
			s = i;
			q.push(i);
			book[i] = true;
		}	
	}
	ans = 0;
	while (!q.empty())
	{
		u = q.front();
		q.pop();
		int t = 0;
		for (i = 1; i <= n; i++)
		{
			if ((a[u].x - a[i].x) * (a[u].x - a[i].x) + (a[u].y - a[i].y) * (a[u].y - a[i].y) == (a[i].r + a[u].r)*(a[i].r + a[u].r))
			{
				t++;
				if (book[i] == false)
				{
					book[i] = true;
					q.push(i);
				}
			}
		}
		if (t == 1 && u!=s)
		{
			ans = u;
			break;
		}
	}
	printf("%d %d
", a[ans].x, a[ans].y);

}
原文地址:https://www.cnblogs.com/zyq1758043090/p/11852561.html