canJump&&jumpII

来源:互联网 发布:mmd一骑当千动作数据 编辑:程序博客网 时间:2024/05/15 11:20
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool canJump(int a[], int n)
{
int reach = 1;
for (int i = 0;i < reach&&reach < n;++i)
{
reach = max(reach, i + 1 + a[i]);
}
return reach >= n;
}
bool canJump1(int a[], int n)
{
if (n == 0)
return true;
int left_most = n - 1;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
for (int i = n - 2;i >= 0;--i)
if (i + a[i] >= left_most)
left_most = i;
return left_most == 0;
}
bool canJump2(int a[], int n)
{
vector<int>f(n, 0);
f[0] = 0;
for (int i = 1;i < n;++i)
{
f[i] = max(f[i - 1], a[i - 1])-1;
if (f[i] < 0)
return false;
}
return f[n - 1] >= 0;


}
int jump(int A[], int n)
{
int left = 0;
int right = 0;
int step = 0;
if (n == 1)
return 0;
while (left <= right)
{
++step;
int old_right = right;
for (int i = left;i <=old_right;++i)
{
if (i + A[i] >= n - 1)
return step;
if (i + A[i]>right)
right = i + A[i];
}
left = old_right + 1;
}
}
int main()
{
int a[] = { 2,3,1,1,4 };
int n = sizeof(a) / sizeof(int);
int b[] = { 3,2,1,0,4 };
int m = sizeof(b) / sizeof(int);
cout << canJump2(a, n) << endl;
cout << canJump2(b, m) << endl;
cout << jump(a, n) << endl;
}
0 0
原创粉丝点击