I always get stuck at this point where dp states depend on both forward and some backward state.In this question : http://codeforces.com/problemset/problem/626/B

I came up with this recurrence relation:

dp[r][g][b] denotes if its is possible to reach this state containing r ‘R’,g ‘G’,b ‘B’.

and relation is dp[r][g][b]=dp[r][g][b] or dp[r+1][g][b] or dp[r][g+1][b] or dp[r][g][b+1] or dp[r-1][g+1][b+1] or dp[r+1][g-1][b+1] or dp[r+1][g+1][b-1].

I want to solve this iteratively without recursion,however i dont know how to iterate,and what to calculate first.

Plz any help is appreciated.