Round A APAC Test 2016 Problem D. gSnake 贪吃蛇 stl应用

Problem D. gSnake

This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-Start Guide to get started.
Small input
13 points
Solve D-small
Large input
19 points
Solve D-large

Problem

Alex is a huge fan of the Snake game.

Note: This Google Doodle does not exactly match the rules of the Snake game we will describe below. It is only intended to give you a general idea of what the game looks like.

Alex just learned how to program and wants to develop his own version of Snake, with the following rules:

  • The game board has R rows and C columns. The top left cell of the board has coordinates (1, 1), and the bottom right cell has coordinates (RC).
  • At the start of the game, in every cell with coordinates (r, c) such that r + c is odd, there is one piece of food. No other cells have food.
  • The snake's body is always an ordered, connected sequence of one or more cells on the board. The first cell of the sequence is called the "head" of the snake. The second cell (if any) shares an edge (not just a corner) with the first cell, and so on. The last cell in the sequence is called the "tail" of the snake.
  • The snake's head is always facing in one of four directions: left, up, right, or down.
  • At the start of the game, the snake is at cell (1, 1) and has a length of one (that is, the snake consists of only a head), and the head faces right.
  • At each integer time (1 second, 2 seconds, etc.), the head of the snake will move into the adjacent cell that its head is facing toward. The board is cyclic, i.e., trying to move off an edge will cause the head to appear on the opposite edge of the board. For example, if the snake is at (1, C) and its head is facing right, the head will next move to (1, 1). If the snake is at (1, C) and its head is facing up, the head will next move to (RC).
  • When the snake's head moves into a cell with no food, the snake does not grow. The snake's second cell (if any) moves to the place where the snake's head was, the snake's third cell (if any) moves to the place where the second cell was, and so on.
  • When the snake's head moves into a cell with a piece of food, it eats the food (meaning that cell no longer has food), and grows its body. A new head is created in the cell where the food was. The cell that was the snake's head becomes the snake's second cell, the cell that was the snake's second cell (if any) becomes the snake's third cell, and so on.
  • If, after a move is complete, the snake's head is in the same place as one of another of its cells, the snake dies and the game ends immediately. (Note that if the snake's head moves toward a cell where its tail was, the game will not end, because the tail will move out of the way before the move is complete.)
  • In the game, the player can let the snake perform some turn actions. Each action Ai will happen between the Tith and Ti+1 th seconds. There are two possible actions: "L" and "R". An "L" action will turn the head 90 degrees to the left, so, for example, if the snake had been facing down before, it would face right after. An "R" action will turn the head 90 degrees to the right, so, for example, if the snake had been facing down before, it would face left after.
  • The game has a time limit: it will end after the move on the 109th second is complete (if the game has even gone on that long!)

To test the game, Alex has written a series of TURN actions. Your task is to simulate that series of actions, and tell Alex the final length of the snake when the game is over. Remember that the game can end either because the snake's head and another cell of its body are in the same place after a move is complete, or because time runs out. In the former case, you should count both the head and the overlapping cell of its body as two separate cells, for the purpose of determining length.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test cases starts with three integers SR, and C, where S gives the number of turn actions and R and C represent the number of rows and columns of the board. S lines follow; the ith of these lines has an integer Xi, then a character Ai that is either L or R. Each of these lines corresponds to performing an action between Xith and Xi+1 th seconds. It's guaranteed that the actions are given in time order and there will never be more than one action between the same two seconds. However, you should note that the game may end before the snake gets to execute all of these actions.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the length of the snake when the game is over.

Limits

1 ≤ T ≤ 10.

Small dataset

1 ≤ R, C ≤ 100;
1 ≤ S ≤ 100;
1 ≤ Xi ≤ 2000.

Large dataset

1 ≤ R, C ≤ 100000;
1 ≤ S ≤ 100000;
1 ≤ Xi ≤ 1000000.

Sample


Input 
 

Output 
 
2
3 3 3
1 R
2 L
3 R
5 3 3
2 R
4 R
6 R
7 R
8 R

Case #1: 3
Case #2: 5

贪吃蛇 不错的模拟题。要细心才能在有限的时间内写出来。

题意要求,有n,m的方格,左上角为1,1右下角为n,m,蛇起始长度为1,在1,1位置。在坐标相加和为奇数的方格内有一块食物,蛇走到这一块会长大一格,给出控制动作,可以向左 向右走。方格是首尾相接的,也就是说在1,1 向上走,会走到n,1。蛇死的条件是,自已撞上了自已,撞上尾巴不算,因为,尾巴在撞上前,也会向前走一格。要求也就是这么多,问,最终,蛇会有多长。有一个坑就是,如果在指令走完仍没死,蛇可能还会走,还会长长。

由于,n,m太大,所以图是不可能存下来的。所以,用map 或set存,食物 和存蛇,这样,就可以了。代码还算好写,细节要注意点。复杂度为o(n * log(n));

#define N 205
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,m,T,q,a,sd;
char str[N];
int sdir[4][2] = {{3,1},{0,2},{1,3},{2,0}};
int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
list<pii> snake;
list<pii>::iterator it;
set<pii> fMap,sMap;
bool Check(pii head,pii tail){
    if(head == tail) return true;
    return !sMap.count(head);
}
bool MoveOn(){
    int sx = snake.front().first,sy = snake.front().second;
    sx = sx + dir[sd][0];sy = sy + dir[sd][1];
    if(sx <= 0) sx = n;
    if(sx >= n+1) sx = 1;
    if(sy <= 0) sy = m;
    if(sy >= m+1) sy = 1;
    bool flag = true;
    pii tail = snake.back();
    if(((sx + sy) & 1) && !fMap.count(mp(sx,sy))){
        fMap.insert(mp(sx,sy));
        snake.push_front(mp(sx,sy));
        flag = Check(mp(sx,sy),tail);
        sMap.insert(mp(sx,sy));
    }
    else{
        flag = Check(mp(sx,sy),tail);
        snake.push_front(mp(sx,sy));
        sMap.insert(mp(sx,sy));
        snake.pop_back();
        sMap.erase(tail);
    }
    return flag;
}
int main()
{
    freopen("D-large-practice.in", "r", stdin);
    freopen("D-large-practice.out", "w", stdout);
     while(S(T)!=EOF)
    {
        For(ta,1,T+1){
            fMap.clear();
            sMap.clear();
            snake.clear();
            S(q);S2(n,m);
            int clock = 1;
            bool flag = true;
            sd = 1;
            sMap.insert(mp(1,1));
            snake.push_front(mp(1,1));
            while(q--){
                S(a);
                SS(str);
                if(flag){
                    for(;clock <= a && flag;clock++){
                        flag = MoveOn();
                    }
                    sd = sdir[sd][(str[0] == 'L'? 0:1)];
                }
            }
            if(flag){
                int maxx = max(n,m);
                for(int i = 0;i<maxx && flag;i++){
                    flag = MoveOn();
                }
            }
            printf("Case #%d: %d\n",ta,snake.size());
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}



你可能感兴趣的:(Round A APAC Test 2016 Problem D. gSnake 贪吃蛇 stl应用)