(BFS)3414 Pots

来源:互联网 发布:飞思卡尔怎么样知乎 编辑:程序博客网 时间:2024/05/17 22:09
/* 3414 Pots */#include <iostream>#include <queue>#include <string>#include <string.h>using namespace std;const int MAX = 101;string FILL1 = "FILL(1)\n";string FILL2 = "FILL(2)\n";string POUR1 = "POUR(1,2)\n";string POUR2 = "POUR(2,1)\n";string DROP1 = "DROP(1)\n";string DROP2 = "DROP(2)\n";int visited[MAX][MAX];int a, b, c;struct Node{    int x;    int y;    int index;    string str;    Node(int _x, int _y, int _index, string _str): x(_x), y(_y), index(_index), str(_str) {}};void bfs(){    queue<Node> que;    Node start = Node(0, 0, 0, "");    que.push( start );    visited[0][0] = 1;    while( !que.empty() )    {Node node = que.front();if( node.x == 0 && node.y == 0 ){    string temp = FILL1;    Node next1 = Node(a, node.y, node.index + 1, temp);    if( visited[next1.x][next1.y] == 0 )    {if( next1.x == c || next1.y == c )    {    cout << next1.index << endl << next1.str;    return;    }    visited[next1.x][next1.y] = 1;que.push( next1 );    }    string temp2 = FILL2;    Node next2 = Node(node.x, b, node.index + 1, node.str + temp2);    if( visited[next2.x][next2.y] == 0 )    {if( next2.x == c || next2.y == c ){    cout << next2.index << endl << next2.str;    return;}visited[next2.x][next2.y] = 1;que.push( next2 );    }}else if( node.x == 0 && node.y == b ){    Node next = Node(0,0,0,"");    if( a >= b )    {next.x = b;next.y = 0;    }    else    {next.x = a;next.y = b - a;    }            next.index = node.index + 1;            string temp = node.str;    temp.append( POUR2 );    next.str = temp;    if( visited[next.x][next.y] == 0 )    {if( next.x == c || next.y == c ){    cout << next.index << endl << next.str;    return;}visited[next.x][next.y] = 1;que.push( next );    }}else if( node.x == a && node.y == 0 ){    Node next = Node(0,0,0,"");    if( a >= b )    {next.x = a - b;next.y = b;    }    else    {next.x = 0;next.y = a;    }    next.index = node.index + 1;    string temp = node.str;    temp.append( POUR1 );    next.str = temp;    if( visited[next.x][next.y] == 0 )    {if( next.x == c || next.y == c ){    cout << next.index << endl << next.str;    return;}visited[next.x][next.y] = 1;que.push( next );    }}else if( node.x == 0 && node.y < b && node.y > 0 ){    string temp = node.str;    temp.append( FILL1 );    Node next = Node(a, node.y, node.index + 1, temp);    if( visited[next.x][next.y] == 0 )    {if( next.x == c || next.y == c ){    cout << next.index << endl << next.str;    return;}visited[next.x][next.y] = 1;que.push( next );    }    Node next2 = Node(0,0,0,"");    if( a > node.y )    {next2.x = node.y;next2.y = 0;    }    else    {next2.x = a;next2.y = node.y - a;    }    next2.index = node.index + 1;    string temp2 = node.str;    temp2.append( POUR2 );    next2.str = temp2;    if( visited[next2.x][next2.y] == 0 )    {if( next2.x == c || next2.y == c ){    cout << next2.index << endl << next2.str;    return;}visited[next2.x][next2.y] = 1;que.push( next2 );    }}else if( node.x < a && node.x > 0 && node. y == 0 ){    string temp = node.str;    temp.append( FILL2 );    Node next = Node(node.x, b, node.index + 1, temp);    if( visited[next.x][next.y] == 0 )    {if( next.x == c || next.y == c ){    cout << next.index << endl << next.str;    return;}visited[next.x][next.y] = 1;que.push( next );    }    Node next2 = Node(0,0,0,"");    if( b > node.x )    {next2.x = 0;next2.y = node.x;    }    else    {next2.x = node.x - b;next2.y = b;    }    next2.index = node.index + 1;    string temp2 = node.str;    temp2.append( POUR1 );    next2.str = temp2;    if( visited[next2.x][next2.y] == 0 )    {if( next2.x == c || next2.y == c ){    cout << next2.index << endl << next2.str;    return;}visited[next2.x][next2.y] = 1;que.push( next2 );    }}else if( node.x == a && node.y > 0 && node.y < b ){    Node next = Node(0,0,0,"");    if( a >= b - node.y )    {next.x = a - b + node.y;next.y = b;    }    else    {next.x = 0;next.y = a + node.y;    }    next.index = node.index + 1;    string temp = node.str;    temp.append( POUR1 );    next.str = temp;    if( visited[next.x][next.y] == 0 )    {if( next.x == c || next.y == c ){    cout << next.index << endl << next.str;    return;}visited[next.x][next.y] = 1;que.push( next );    }    string temp2 = node.str;    temp2.append( DROP1 );    Node next2 = Node(0, node.y, node.index + 1, temp2);    if( visited[next2.x][next2.y] == 0 )    {if( next2.x == c || next2.y == c ){    cout << next2.index << endl << next2.str;    return;}visited[next2.x][next2.y] = 1;que.push( next2 );    }}else if( node.y == b && node.x > 0 && node.x < a ){    Node next = Node(0,0,0,"");    if( a - node.x >= b )    {next.x = node.x + b;next.y = 0;    }    else    {next.x = a;next.y = b - a + node.x;    }    next.index = node.index + 1;    string temp = node.str;    temp.append( POUR2 );    next.str = temp;    if( visited[next.x][next.y] == 0 )    {if( next.x == c || next.y == c ){    cout << next.index << endl << next.str;    return;}visited[next.x][next.y] = 1;que.push( next );    }    string temp2 = node.str;    temp2.append( DROP2 );    Node next2 = Node(node.x, 0, node.index + 1, temp2);    if( visited[next2.x][next2.y] == 0 )    {if( next2.x == c || next2.y == c ){    cout << next2.index << endl << next2.str;    return;}visited[next2.x][next2.y] = 1;que.push( next2 );    }}else if( node.x > 0 && node.y > 0 && node.x < a && node.y < b ){    string temp = node.str;    temp.append(DROP1);    Node next1 = Node(0, node.y, node.index + 1, temp);    if( visited[next1.x][next1.y] == 0 )    {visited[next1.x][next1.y] = 1;que.push( next1 );    }    string temp2 = node.str;    temp.append( DROP2 );    Node next2 = Node(node.x, 0, node.index + 1, temp2);    if( visited[next2.x][next2.y] == 0 )    {visited[next2.x][next2.y] = 1;que.push( next2 );    }    Node next3 =  Node(0,0,0,"");    if( a - node.x >= node.y )    {next3.x = node.x + node.y;next3.y = 0;    }     else    {next3.x = a;next3.y = node.y - a + node.x;    }    next3.index = node.index + 1;    string temp3 = node.str;    temp3.append( POUR2 );    next3.str = temp3;    if( visited[next3.x][next3.y] == 0 )    {if( next3.x == c || next3.y == c ){    cout << next3.index << endl << next3.str;    return;}visited[next3.x][next3.y] = 1;que.push( next3 );    }    Node next4 = Node(0,0,0,"");    if( node.x >= b - node.y )    {next4.x = node.x - b + node.y;next4.y = b;    }    else    {next4.x = 0;next4.y = node.x + node.y;    }    next4.index = node.index + 1;    string temp4 = node.str;    temp4.append( POUR1 );    next4.str = temp4;    if( visited[next4.x][next4.y] == 0 )    {if( next4.x == c || next4.y == c ){    cout << next4.index << endl << next4.str;    return;}visited[next4.x][next4.y] = 1;que.push( next4 );    }}que.pop();    }    cout << "impossible" << endl;    return;}int main(){    cin >> a >> b >> c;    memset( visited, 0, sizeof(visited) );    bfs();    return 0;}


原创粉丝点击