Small input 13 points |
Solve D-small
|
Large input 19 points |
Solve D-large
|
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:
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.
The first line of the input gives the number of test cases, T. T test cases follow. Each test cases starts with three integers S, R, 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.
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.
1 ≤ T ≤ 10.
1 ≤ R, C ≤ 100;
1 ≤ S ≤ 100;
1 ≤ Xi ≤ 2000.
1 ≤ R, C ≤ 100000;
1 ≤ S ≤ 100000;
1 ≤ Xi ≤ 1000000.
题意要求,有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; }