B. Little Artem and Grasshopper
2 seconds
256 megabytes
Little Artem found a grasshopper. He brought it to his house and constructed a jumping area for him.
The area looks like a strip of cells 1 × n. Each cell contains the direction for the next jump and the length of that jump. Grasshopper starts in the first cell and follows the instructions written on the cells. Grasshopper stops immediately if it jumps out of the strip. Now Artem wants to find out if this will ever happen.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — length of the strip.
Next line contains a string of length n which consists of characters "<" and ">" only, that provide the direction of the jump from the corresponding cell. Next line contains n integers di (1 ≤ di ≤ 109) — the length of the jump from the i-th cell.
Print "INFINITE" (without quotes) if grasshopper will continue his jumps forever. Otherwise print "FINITE" (without quotes).
2
><
1 2
FINITE
3
>><
2 1 1
INFINITE
Note
In the first sample grasshopper starts from the first cell and jumps to the right on the next cell. When he is in the second cell he needs to jump two cells left so he will jump out of the strip.
Second sample grasshopper path is 1 - 3 - 2 - 3 - 2 - 3 and so on. The path is infinite.
很水的一个题
题意:
有一条通道,是由一个一个格子组成,每个格子上分别标记着下一步要走的方向和对应的步数,出口是最左端或者最右端。问你能不能走出去。
思路:
如果某一步和之前走过的某一步重复了,那么就陷入死循环中无法出去,注意判断是否陷入死循环。
还有,不要忽略这种情况 >>>>>
1 1 1 1 1 这样子也可以走出去奥~~~
AC代码:
#include<iostream>
#include<stdio.h>
using namespace std;
struct state
{
char a;
int b;
}time[100004];//定义一个结构体数组来存放格子的规则
int main()
{
int n;
int step = 0;
while (~scanf("%d", &n))
{
getchar();//吃掉回车
for (int i = 0; i < n; i++)
{
scanf("%c", &time[i].a);
}
//getchar();
for (int i = 0; i < n; i++)
{
scanf("%d", &time[i].b);
}
int num = 0;
int ans = 0;
while(1)
{
if (time[num].a == '>')
{
ans = num;
num += time[num].b;//此时此刻timr[num]这个已经用过了,num用来记录走到哪里了。
time[ans].b = 0;//所以在这里给他归零
}
else
{
ans = num;
num -= time[num].b;
time[ans].b = 0;
}
if (num<0 || num>=n)//注意这个等号很重要,我就没写结果漏掉了一种情况
{
printf("FINITE\n");
break;
}
else if (time[num].b == 0 && num >= 0 && num <n)//判断说不是陷入了死循环,咦嘻嘻
{
printf("INFINITE\n");
break;
}
}
}
return 0;
}
今天也是元气满满的一天,good luck!