#include using namespace std; typedef struct _status{ int lm, lc, rm, rc, steps; int m, c;//记录上次移动的,不返回上一步(简单剪枝) bool left; int pre; }status; class route{ public: static const int maxsize = 100000; status s1[maxsize];//状态队列 int front, rear;//队列的头尾指针 int n, steps;//记录船的容量 以及 最后的步数 bool find; //初始化,插入第一个状态节点 void init(int m, int n1){ rear = 0; front = 0; s1[0].lm = m; s1[0].lc = m; s1[0].rm = 0; s1[0].rc = 0; s1[0].m = 0; s1[0].c = 0; s1[0].steps = 0; s1[0].left = true; s1[0].pre = -1; steps = -1; n = n1; rear++; find = false; }//end of function init() void init(int m, int c, int n1){ rear = 0; front = 0; s1[0].lm = m; s1[0].lc = c; s1[0].rm = 0; s1[0].rc = 0; s1[0].m = 0; s1[0].c = 0; s1[0].steps = 0; s1[0].left = true; s1[0].pre = -1; steps = -1; n = n1; rear++; find = false; }//end of function init() void in(status a1){ if(rear >= maxsize - 1){//满队列,认为无解 cout << "Queue overflow" << endl; find = true; steps = -1; a1.steps = -1; } s1[rear] = a1; rear ++; }//end of function in() status out(){ front ++; return s1[front - 1]; }//end of function out() void dump(){//测试输出所有遍历过的状态节点 if(find){ cout << "Find = true" << endl; }else{ cout << "Find = false" << endl; } for(int i = 0; i < rear; i++){ cout << i << ". " << "Step=" << s1[i].steps << " " << s1[i].lm << " " << s1[i].lc; cout << " |"; if(s1[i].left){ cout << "<=> | "; }else{ cout << " <=>| "; } cout << s1[i].rm << " " << s1[i].rc << " , pre=" << s1[i].pre < | "; }else{ cout << " <=>| "; } cout << a1.rm << " " << a1.rc << " , pre=" << a1.pre < 0 && a1.lm < a1.lc || a1.lm < 0 || a1.lc < 0){//不符合条件 return; } if(a1.rm > 0 && a1.rm < a1.rc || a1.rm < 0 || a1.rc < 0){//不符合条件 return; } a1.pre = front - 1;//记录上一节点的位置 if(a1.lm == 0 && a1.lc == 0){//找到路径 //dump(a1); find = true; steps = a1.steps; } in(a1);//插入状态节点 }//end of function go() private: void find_route(int i){//递归输出路径 if(s1[i].pre != -1){ find_route(s1[i].pre); dump(s1[i], '~'); }else{ dump(s1[i], 'S'); } } public: void dump_route(){//供外部调用输出路径 find_route(rear - 1); } };//end of class route; int main(){ int cases; int m, c, n; route r1; scanf("%d", &cases); while(cases--){ status cur; //scanf("%d%d", &m, &n); //r1.init(m, n); scanf("%d%d%d", &m, &c, &n); r1.init(m, c, n); while(r1.front < r1.rear && r1.find == false){ cur = r1.out(); cur.steps ++; for(int m = n; m >= n / 2; m--){ for(int c = 1; c <= n; c++){ r1.go(cur, 0, c); if(r1.find)break; } for(int c = 0; c <= n - m; c++){ r1.go(cur, m, c); if(r1.find)break; } if(r1.find)break; } } if(r1.find && r1.steps != -1){ r1.dump_route();//输出路径 cout << r1.steps << endl; }else{ cout << "-1" << endl; } } return 0; }