Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <iostream>
- # include <cstdio>
- # include <set>
- # include <queue>
- using namespace std;
- # define FOR(C, F, L) for ( int C = F; C <= L; C++ )
- # define MP(X,Y) make_pair((X),(Y))
- typedef pair<int, int> pi;
- typedef pair<pi, pi>ppii;
- typedef pair<pi,int> ppi;
- queue < ppii > Q;
- int C, M, R, level=0;
- set <pair<pi,int> > S;
- int elementFound ( ppi node)
- {
- set < ppi > :: iterator it;
- it = S.find(node);
- if ( it == S.end() )return 0;
- else return 1;
- }
- void generatecases ( ppii X )
- {
- int x = X.first.first;
- int y = X.first.second;
- int b = X.second.first;
- int dist = X.second.second;
- if (b)
- {
- b += 1; b %= 2;
- FOR(I,0,R)
- FOR(J,0,R)
- {
- if (!(I+J));
- else {
- if ( (y-J>=0) && (I+J <=R))
- if ((x-I==0) || (x-I==M) ||
- ((x-I==y-J) &&(M-(x-I) == C-(y-J)&&(x-I>=0))))
- {
- ppi temp = MP(MP(x-I,y-J),b);
- if ( !elementFound(temp))
- {
- Q.push(MP(MP(x-I,y-J),MP(b,dist+1)));
- S.insert(temp);
- }
- }
- }
- }
- }
- else {
- b += 1; b %= 2;
- FOR(I,0,R)
- FOR(J,0,R)
- {
- if (!(I+J));
- else {
- if ((y+J<=C) && (I+J<=R))
- if ((x+I==M) || (x+I== 0) ||
- (x+I==y+J && M-(x+I)==C-(y+J)&&(x+I<=M)) )
- {
- ppi temp = MP(MP(x+I,y+J),b);
- if ( !elementFound(temp) )
- {
- Q.push(MP(MP(x+I,y+J),MP(b, dist+1)));
- S.insert( temp );
- }
- }
- }
- }
- }
- }
- int main()
- {
- int t;cin >> t;
- FOR(U,1,t){
- scanf("%d%d%d",&M,&C,&R);
- while(!Q.empty())Q.pop();
- S.erase(S.begin(),S.end());
- Q.push(MP(MP(M,C),MP(1,0)));
- S.insert(MP(MP(M,C),1));
- int flag=1;
- while (!Q.empty())
- {
- ppii top = Q.front();
- Q.pop();
- if (top.first.first==0 && top.first.second==0)
- {
- cout << top.second.second <<endl;flag=0;break;
- }
- else generatecases(top);
- }
- if(flag){
- int impossible = -1;
- cout << impossible<<endl;}
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment