本文主要是介绍【Good Bye 2014A】【水题 dfs】New Year Transportation 可否传送到t点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.
So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1positive integers a1, a2, ..., an - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ ai ≤ n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + ai), and one can travel from cell i to cell(i + ai) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + ai) to celli using the i-th portal. It is easy to see that because of condition 1 ≤ ai ≤ n - i one can't leave the Line World using portals.
Currently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.
The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to.
The second line contains n - 1 space-separated integers a1, a2, ..., an - 1 (1 ≤ ai ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.
If I can go to cell t using the transportation system, print "YES". Otherwise, print "NO".
8 4 1 2 1 2 1 2 1
YES
8 5 1 2 1 2 1 1 1
NO
In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.
In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 3e4+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, t, x;
vector<int>a[N];
bool vis[N];
void dfs(int x)
{vis[x] = 1;for (int i = a[x].size() - 1; ~i; --i){int y = a[x][i];if (!vis[y])dfs(y);}
}
int main()
{while (~scanf("%d%d", &n, &t)){for (int i = 1; i < n; ++i){scanf("%d", &x);a[i].push_back(i + x);}dfs(1);puts(vis[t] ? "YES" : "NO");}return 0;
}
/*
【题意】
有n(3e4)个点,排成一排。
对于点i,可以传送到i+a[i]。
问你我们可否传到t点【类型】
bfs or dfs【分析】
扫一下就好啦【时间复杂度&&优化】
O(n)*/
这篇关于【Good Bye 2014A】【水题 dfs】New Year Transportation 可否传送到t点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!