听听 艾薇儿 girlfriend

toj 2802 Tom's Game

2802.   Tom's Game
Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 376   Accepted Runs: 117    Multiple test files



Tom is playing the following game on a computer. There are 101 squares connected in a line, with the ith square adjacent to the (i-1)th one on its left and to the (i+1)th on its right, for 1 ≤ i ≤ 99, and the 0th and 100th square are the starting and finishing square, respectively.

Initially Tom's character is at Square 0. At each turn the computer throws a dice and produces a number k between 1 and 6, inclusive. Tom's character will then go k steps forward, i.e. from Square i to Square i + k. Once the character reaches the finishing square 100, the game ends and Tom wins. If the character goes more than needed steps toward the finishing square, he will be wrapped around. For example, if the character is at 98 and computer throws '5', then he will end up at 97(98->99->100->99->98->97).

In addition, some squares are special. There are 3 types of special squares:

1) Advance k. When the character reaches this type of square, he will go k more steps forward from it. Note he will still be wrapped around toward the end of the path. For example, if Square 98 is 'Advance 3' , then if the character reaches this square, he will end up at 99(98->99->100->99).

2) Back k. When the character reaches this type of square, he will go k steps back from it, i.e. from i to i - k. if i - k < 0, the character will end up at 0.

3) Goto k. When the character reaches this type of square, he will go to Square k.

In a single turn the character may encounter more than one special squares, and they will take effect one after another.

After playing the game for a while without victory, Tom begins to suspect that the computer cheats on the dice throwing so as to not let him win. So he writes a program and hacks into the random number generating module of the game. Now Tom can control in each turn what number the dice throwing will produce. Of course this number is still in the range [1..6]. To surprise his friends on how well he can play the game, Tom decides to control the dice throwing in each turn such that he can win the game in as few turns as possible.

Note that the special squares may be ill-designed such that Tom will never win the game no matter how he controls the output of the dice throwing.

Input

The first line is an integer N, number of special squares. Following N lines each contain three integers, s, t and k. s is the index of the special square. t is type of the square and is in the range [0..2], with 0, 1 and 2 stands for "Advance", "Back" and "Goto" respectively. k is the argument for this type, as described above.

Output

Output on a single line the minimum number of turns Tom needs to win the game, if he can control the output of the dice throwing in each turn. If Tom can never win the game, output -1.

Constraints

  • 1 ≤ s ≤ 99, i.e. the staring and finishing squares will never be special.
  • Each square can be at most one of the three special types.
  • The k for 'Advance' and 'Back' squares is in the range [1..6].
  • The k for 'Goto' squares is in the range [0..100].
  • If Square i is 'Goto k', then k will not be i.
  • Sample Input 1

    0
    

    Sample Output 1

    17
    

    Sample Input/Output 1 Clarification

    With no special squares, Tom can't play too many tricks. The best strategy for him is to throw '6' in each turn except the last, where he should throw '4'.

    Sample Input 2

    1
    2 2 99
    

    Sample Output 2

    2
    

    Sample Input/Output 2 Clarification

    Square 2 is special; it is 'Goto 99'. Tom can throw 2 in the first turn, which takes him to Square 99. Then he throws 1 and wins the game.

    Sample Input 3

    6
    1 0 1
    2 0 1
    3 0 1
    4 0 1
    5 0 1
    6 1 5
    

    Sample Output 3

    -1
    

    Sample Input/Output 3 Clarification

    No matter what Tom throws in the first turn, his character will be trapped into an infinite loop(1->2->3->4->5->6->1). So he can never win the game.



    Source: The 5th UESTC Programming Contest


    Submit   List    Runs   Forum   Statistics

    //638914 2009-05-16 08:34:33 Accepted 2802 C++ 1.6K 0'00.00" 1208K forever4444 
    //638700 2009-05-15 20:59:01 Wrong Answer 2802 C++ 1.8K 0'00.00" 1208K forever4444
    //638719 2009-05-15 21:10:36 Wrong Answer 2802 C++ 1.9K 0'00.00" 1208K forever4444  
    #include <iostream>
    #include 
    <queue>
    #define MAX 101
    using namespace std;
    typedef 
    struct node
    {
        
    int step;
        
    int num;
        node(){};
        node(
    int ss,int nn)
        {
            step
    =ss;
            num
    =nn;
        }
        friend 
    bool operator<(node a,node b)
        {
            
    return a.step>b.step;
        }
    }Point;
    priority_queue
    <Point>Q;
    Point p,temp;
    int n,MM;
    struct node1
    {
        
    int step;
        
    int mark;
        
    int to;
    }data[MAX];
    void Init()
    {
        
    int i;
        
    int a,b,s;
        
    for(i=0;i<MAX;i++)
        {
            data[i].step
    =-1;
            data[i].mark
    =-1;
            data[i].to
    =0;
        }
        
    for(i=0;i<n;i++)
        {
            scanf(
    "%d%d%d",&a,&b,&s);
            data[a].mark
    =b;
            data[a].to
    =s;
            
    if(b==1)
                data[a].to
    *=-1;
        }
    }
    void BFS()
    {
        
    int i;
        
    while(!Q.empty())
            Q.pop();
        Q.push(node(
    0,0));
        data[
    0].step=0;
    //    MM=-1;
        while(!Q.empty())
        {
            p
    =Q.top();
            Q.pop();
            
    if(p.num==100)  //在这里第一次到达的就是花费最少的
            {
                
    return ;
            }
            
    if(data[p.num].mark==-1)
            {
                temp.step
    =p.step+1;
                
    for(i=1;i<=6;i++)
                {
                    temp.num
    =p.num+i;
                    
    if(temp.num>100)
                        temp.num
    =200-temp.num;
                    
    if(data[temp.num].step==-1||temp.step<data[temp.num].step)
                    {
                        data[temp.num].step
    =temp.step;
                        Q.push(temp);
                    }
                }
            }
            
    else 
            {
                temp.step
    =p.step;
                
    if(data[p.num].mark==2)
                {
                    temp.num
    =data[p.num].to;
                }
                
    else
                {
                    temp.num
    =p.num+data[p.num].to;
                    
    if(temp.num>100)
                        temp.num
    =200-temp.num;
                    
    if(temp.num<0)
                        temp.num
    =0;
                }
                
    if(data[temp.num].step==-1||temp.step<data[temp.num].step)
                {
                    data[temp.num].step
    =temp.step;
                    Q.push(temp);
                }
            }
        }
    }
    int main()
    {
        
    while(scanf("%d",&n)!=EOF)
        {
            Init();
            BFS();
            printf(
    "%d\n",data[100].step);
        }
        
    return 0;
    }
    posted @ 2009-05-16 08:36  往往  阅读(296)  评论(0编辑  收藏  举报