P3083 [USACO13OPEN]豪华游船Luxury River Cruise

Luxury River Cruise

Description
Farmer John is taking Bessie and the cows on a cruise! They are sailing on a network of rivers with N ports (1 ≤ N ≤ 1,000) labeled 1…N, and Bessie starts at port 1. Each port has exactly two rivers leading out of it which lead directly to other ports, and rivers can only be sailed one way.

At each port, the tour guides choose either the “left” river or the “right” river to sail down next, but they keep repeating the same choices over and over. More specifically, the tour guides have chosen a short sequence of M directions (1 ≤ M ≤ 500), each either “left” or “right”, and have repeated it K times (1 ≤ K ≤ 1,000,000,000). Bessie thinks she is going in circles – help her figure out where she ends up!

Input

  • Line 1: Three space-separated integers N, M, and K.

  • Lines 2…N+1: Line i+1 has two space-separated integers, representing the number of the ports that port i’s left and right rivers lead to, respectively.

  • Line N+2: M space-separated characters, either ‘L’ or ‘R’. ‘L’ represents a choice of ‘left’ and ‘R’ represents a choice of ‘right’.

Output

  • Line 1: A single integer giving the number of the port where Bessie’s cruise ends.

Sample Input
4 3 3
2 4
3 1
4 2
1 3
L L R

Sample Output
4

Hint
INPUT DETAILS:

The port numbers are arranged clockwise in a circle, with ‘L’ being a clockwise rotation and ‘R’ being a counterclockwise rotation. The sequence taken is LLRLLRLLR.

OUTPUT DETAILS:

After the first iteration of the sequence of directions, Bessie is at port 2 (1 -> 2 -> 3 -> 2); after the second, she is at port 3 (2 -> 3 -> 4 -> 3), and at the end she is at port 4 (3 -> 4 -> 1 -> 4).

翻译:
农民约翰带着Bessie和奶牛在邮轮上!他们在网格上的N条河流航行(1≤N≤1000)标记为1到N,一开始他们在开始在河口1。每一个港口都有两条河流直通,直接通往其他港口,河流只能通过一条路航行。

在每一个港口,导游选择左边的河或右边的河向下航行,但他们不断重复相同的选择一遍又一遍。更具体地说,导游选择了一个m方向(1 < =m= 500),每一个向左或向右,并重复它K次(1 < = K = 1000000000)。Bessie认为她是在兜圈子,帮她找出结束的位置!
输入格式:

  • Line 1: Three space-separated integers N, M, and K.

  • Lines 2…N+1: Line i+1 has two space-separated integers,

representing the number of the ports that port i’s left and right rivers lead to, respectively.

  • Line N+2: M space-separated characters, either ‘L’ or ‘R’. ‘L’ represents a choice of ‘left’ and ‘R’ represents a choice of ‘right’.

输出格式:

  • Line 1: A single integer giving the number of the port where Bessie’s cruise ends.
    输入样例
4 3 3 
2 4 
3 1 
4 2 
1 3 
L L R 

输出样例

4

原题:洛谷P3083

思路:我们用一个m次操作为单位,每单位循环得到一个新的点,最多1000次就可以出现重复(指新点以前出现过),那么当出现重复的时候就出现循环节,那么接下来就是利用循环节取避免k次循环。
参考于其中一个题解:https://www.luogu.org/blog/CSGO/solution-p3083
代码如下:

#include<bits/stdc++.h>
using namespace std;
struct river{
	int l;
	int r;
}a[1005];
char q[505];
int low[1005];
int main(void)
{
	int n,m,k;
	scanf("%d %d %d",&n,&m,&k);
	for(int i=1;i<=n;i++){
		scanf("%d %d",&a[i].l ,&a[i].r );
	}
	//getchar();
	for(int i=1;i<=m;i++){
		//getchar();
		//scanf("%c",&q[i]);//我也不知道为什么这样输入会错 
		cin>>q[i];
	}
	//for(int i=1;i<=m;i++){
    //	printf("%c",q[i]);
    //}
    
	//循环节
	int now=1,top=0;
	while(!low[now])//看做多少次m模拟now会重复 
	{
		low[now]=++top;//一个m后的节点now 
		for(int i=1;i<=m;i++){
			if(q[i]=='L')
			{
				now=a[now].l ;
			}
			else
			now=a[now].r ;
		}
	}
	int x=top-low[now]+1;
	int y=low[now]-1;
	k-=y;//换成now开始 
	k%=x;
	while(k--)
	{
		for(int i=1;i<=m;i++)
		if(q[i]=='L')
			{
				now=a[now].l ;
			}
			else
			now=a[now].r ;
	}
	printf("%d\n",now);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值