Grid
时间限制: 1 Sec 内存限制: 128 MB提交: 318 解决: 40
[提交][状态][讨论版]
题目描述
Pong is wandering in the grids, and we take his initial position as (0,0). Each step he can move in the 4 directions of UP, DOWN, LEFT, RIGHT for one grid.
While we do know some of these steps, we are attacked by Pong’s birds (Pong have birds, maybe) so some steps remain unknown to us. Now we want to be informed how many possible ultimate position he might be.
While we do know some of these steps, we are attacked by Pong’s birds (Pong have birds, maybe) so some steps remain unknown to us. Now we want to be informed how many possible ultimate position he might be.
输入
For the first row there is an integer T(T ≤ 100), which is test cases.
For each case there are 2 rows. First row is an integer n(n ≤ 100000), next row is n operations consists of "UP","DOWN", "LEFT", "RIGHT", "?". Each operation is separated by a space.
For each case there are 2 rows. First row is an integer n(n ≤ 100000), next row is n operations consists of "UP","DOWN", "LEFT", "RIGHT", "?". Each operation is separated by a space.
输出
For each case you should output the answer.
样例输入
2
3
UP ? LEFT
4
? UP ? RIGHT
样例输出
4
9
提示
因为上下左右确定的就知道终点了,然后就剩下“?”了,所以重点是?的个数,然后就找规律就好了
#include<bits/stdc++.h> using namespace std; #define maxn 1000 #define maxm 100005 #define rd(x) scanf("%d", &x) #define rd2(x, y) scanf("%d%d", &x, &y) long long int pk[100500]; int main() { int t, n; char s[1000000]; pk[1] = 1; pk[0] = 0; for(int i = 2; i <= 100005; i++) pk[i] = i*4 - 4 + pk[i-2]; rd(t); while(t--){ rd(n); int k = 0; getchar();///收回车 gets(s); ///不能收回车,但是遇到回车结束 int len = strlen(s); for(int i = 0; i < len; i++){ if(s[i] == '?') k++; } printf("%lld ", pk[k+1]); } return 0; }