TRIANGLEPATH 관련해서 질문합니다

  • User Avatar
    chatterboy

    반복문을 이용한 방법으로 풀어봤는데 어느 부분에서 왜 오답인지를 모르겠네요.
    P[1,1] = triangle[1][1]
    P[y,x] = max{P[y-1,x], P[y-1,x-1]} + triangle[y][x]
    를 토대로 구현했습니다.
    ~~~ c++
    int triangle[101][101];
    int dp[101][101];

    int path(int y, int x)
    {
    memset(dp, 0, sizeof(dp) );
    dp[1][1] = triangle[1][1];
    for (int j = 2; j <= y; j++)
    {
    for (int i = 2; i <= x; i++)
    {
    if (j >= i)
    dp[j][i] = triangle[j][i] + max(dp[j-1][i], dp[j-1][i-1]);
    }
    }
    return dp[y][x];
    }

    int MAIN()
    {
    vector res;
    int tc; cin >> tc;
    while (tc--)
    {
    int n; cin >> n;
    for (int j = 1; j <= n; j++)
    for (int i = 1; i <= j; i++)
    cin >> triangle[j][i];

        int maxi = numeric_limits<int>::min();
        for (int x = 1; x <= n; x++)
            maxi = max(maxi, path(n, x) );
    
        res.push_back(maxi);
    }
    for (size_t i = 0; i < res.size(); i++)
        cout << res[i] << endl;
    return 0;
    

    }
    ~~~


    12년 전
2개의 댓글이 있습니다.
  • User Avatar
    Kureyo

    dp[j]1이 적절히 초기화 안된거같네요


    12년 전 link
  • User Avatar
    chatterboy

    답변 감사합니다


    12년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.