Hi I tried to solve this problem based on bfs https://codeforces.net/problemset/problem/198/B . Also I have implemented it well and it passes for 28 test cases butgives MEMORY LIMIT EXCEE. on test 29 . May be there is something to learn here . please anybody hep if you can figure out whats wrong . Here is my submission https://codeforces.net/contest/198/submission/85799856 . THANKS
Auto comment: topic has been updated by adyyy (previous revision, new revision, compare).
Consider marking the state visited while pushing them in the queue. 85803410
okay but what difference does that make ??
It is possible that many states lead to the same sub-state. In that case you get multiple instances of same sub-state in the queue. Notice that the sub-state is marked visited only when it is processed. But before it is processed there can be copies of it in the queue which unnecessarily increases the queue size, giving you MLE.
oh okay i got it thanks ..