codeforces B. Cow Program (记忆化搜索)

题目链接:

codeforces 283B

题目大意:

给出n个数,奇数次操作x,y都加上a[x],偶数次操作y加上a[x],x减去a[x],走出了范围就结束。
问结束时的y值,如果无法结束,那么输出-1

题目分析:

  • 记录状态dp[x][2]为在奇数次或偶数次到达x点时走完还会获得的权值。
  • 直接搜索,搜索到搜过的状态直接返回。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 200007

using namespace std;

typedef long long LL;

LL dp[MAX][2];
int vis[MAX][2];
int a[MAX],n;

void dfs ( int x , int d )
{
    if ( vis[x][d%2] ) return;
    vis[x][d%2] = 1;
    if ( d&1 )
    {
        int y = x-a[x];
        if ( y <= 0 ) 
        {
            dp[x][d%2] = a[x];
            return;
        }
        dfs ( y , d+1 );
        if ( dp[y][(d+1)%2] != -1 ) 
            dp[x][d%2] = dp[y][(d+1)%2] + a[x];
    }
    else
    {
        int y = x+a[x];
        if ( y > n ) 
        {
            dp[x][d%2] = a[x];
            return;
        }
        dfs ( y , d+1 );
        if ( dp[y][(d+1)%2] != -1 )
            dp[x][d%2] = dp[y][(d+1)%2] + a[x];
    }
}

int main ( )
{
    while ( ~scanf ( "%d" , &n ) )
    {
        for ( int i = 2 ; i <= n ; i++ )
            scanf ( "%d" , &a[i] );
        memset ( vis , 0 , sizeof ( vis ) );
        memset ( dp , -1, sizeof ( dp ) );
        dp[1][0] = -1;
        vis[1][0] = 1;
        dp[1][1] = 0;
        vis[1][1] = 1;
        for ( int i = 2 ; i <= n ; i++ )
            dfs ( i , 1 );
        for ( int i = 2 ; i <= n ; i++ )
            if ( dp[i][1] != -1 )
                dp[i][1] += i-1;
        for ( int i = 2 ; i <= n ; i++ )
            printf ( "%lld\n" , dp[i][1] );
    }
}

你可能感兴趣的:(dp,搜索,codeforces,记忆化搜索)