ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1225. Flags

problem in first test case
Posted by elafifi 5 Aug 2017 04:29
 i implemented this problem with dp correctly and the online judge give me wrong answer in test 1 but my program give the same answer as int the example

this is the code where is the problem
//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
using namespace std;
#define w 2
#define r 3
#define b 4
long long n;
long long dp[1010][10][10];

long long solve(int i=0,int prev = 1,int prev2=1){/// w===>2  ,,,, r====> 3 ,,,,b ===>4
    if(i==n-1)return 1;
    //long long &ret = dp[i][prev][prev2];
    if(dp[i][prev][prev2]!=-1)return dp[i][prev][prev2];
    dp[i][prev][prev2] = 0;
    if(prev != w&&!(prev == b&&prev2 == w))
        dp[i][prev][prev2] += solve(i+1,w,prev);
    if(prev != r&&!(prev == b&&prev2 == r))
        dp[i][prev][prev2] += solve(i+1,r,prev);
    if(prev != b&&!(prev == 1))
        dp[i][prev][prev2] += solve(i+1,b,prev);
    return dp[i][prev][prev2];
}
int main()
{
    memset(dp,-1,sizeof dp);
    cin>>n;
    long long ans = solve();
    cout<<ans<<endl;
    return 0;
}
Re: problem in first test case
Posted by Mahilewets 5 Aug 2017 09:36
I have HEAVILY edited your code.

Now it is ACCEPTED.
http://ideone.com/sLZnf2

I believe the problem was in your OVERCOMPLICATED dp.
It is easy to made a mistake when you have 3 dimensions.