B. Belted Rooms

题目:
In the snake exhibition, there are n rooms (numbered 0 to n−1) arranged in a circle, with a snake in each room. The rooms are connected by n conveyor belts, and the i-th conveyor belt connects the rooms i and (i+1)modn. In the other words, rooms 0 and 1, 1 and 2, …, n−2 and n−1, n−1 and 0 are connected with conveyor belts.

The i-th conveyor belt is in one of three states:

If it is clockwise, snakes can only go from room i to (i+1)modn.
If it is anticlockwise, snakes can only go from room (i+1)modn to i.
If it is off, snakes can travel in either direction.

https://codeforces.com/contest/1428/problem/B

题意:
给出n个点,从0到n-1都有边相连,n-1与0也有边相连。每条边都有方向,>表示顺时针可达,<表示逆时针可达,-表示双向可达。问共有几个点可以离开之后又回到该点。

思路:
可能有的人会以为是图,其实就是个思维题。因为双向可达是万能的,所以我们应重点关注两种单向可达。如果只有一种方向的单向可达,那说明这是一个环,任意一个点都可以走一圈回到原点。如果两种方向都有的话,那说明只有双向可达边连接的两个点才是满足条件的。、

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 300010;
int t;
int num;
int a[MAXN];
int v[MAXN];
int x , y , z;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		x = 0;//���� 
		y = 0;//��� 
		z = 0;//˫�� 
		num = 0;
		getchar();
		char s;
		for(int i = 0  ;i < n ; i++)
		{
			scanf("%c",&s);
			if(s=='>')
			{
				a[i] = 1;
				x++;
			}
			if(s=='<')
			{
				a[i] = 2;
				y++;
			}
			if(s=='-')
			{
				a[i] = 3;
				z++;
			}
		}
		if(x==0||y==0)
		{
			num = n; 
		}
		else
		{
			for(int i = 0 ; i < n ; i++)
			{
				if(i==0)
				{
					if(a[i]==3||a[n-1]==3)
					{
						num = num + 1;
					}
				}
				else if(a[i-1]==3 || a[i]==3)
				{
					num++;
				}
			}
		}
		cout << num << endl;
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值