Codeforces Round #335 (Div. 2) B. Testing Robots 水题
B. Testing Robots
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://www.codeforces.com/contest/606/problem/B
Description
The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x × y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly x·y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same.
After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code.
Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it.
The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.
Input
The first line of the input contains four integers x, y, x0, y0 (1 ≤ x, y ≤ 500, 1 ≤ x0 ≤ x, 1 ≤ y0 ≤ y) — the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right.
The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.
Output
Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.
Sample Input
3 4 2 2
UURDRDRL
Sample Output
1 1 0 1 1 1 1 0 6
HINT
题意
题意是给一个x*y的棋盘以及机器人的初始位置,给一个操作序列,对每个k,求有多少个地雷的位置使得走了机器人恰好k步的时候会炸掉(可以是因为操作序列结束也可以是因为踩到地雷),注意到假如走了k步之后的格子在之前已经被访问过,那么机器人即使要炸也会在之前就炸掉,那么对于k=0,1,2,...,len,如果机器人当前所在位置之前未被访问过,输出1,否则输出0,对于k=len+1,记前len个答案之和为tot,输出x*y-tot
题解:
读懂就能AC,前提是你能读懂。。。
代码:
#include<iostream> #include<cstring> #include<stdio.h> using namespace std; #define maxn 1000110 int vis[550][550]; int ans[maxn]; string s; int dx[4]={-1,0,0,1}; int dy[4]={0,1,-1,0}; int n,m,x,y; int check(char c) { if(c=='U')return 0; if(c=='R')return 1; if(c=='L')return 2; if(c=='D')return 3; } int check2(int xx,int yy) { if(xx>n||xx<1)return 0; if(yy>m||yy<1)return 0; return 1; } int flag = 1; int main() { cin>>n>>m>>x>>y; cin>>s; vis[x][y]=1; ans[0]=1; for(int i=0;i<s.size()-1;i++) { int xx = dx[check(s[i])]; int yy = dy[check(s[i])]; if(check2(x+xx,y+yy)) { x = x+xx; y = y+yy; } if(vis[x][y]) ans[i+1]=0; else { ans[i+1]=1; vis[x][y]=1; flag++; } //cout<<xx<<" "<<yy<<endl; //cout<<x<<" "<<y<<endl; } for(int i=0;i<s.size();i++) printf("%d ",ans[i]); printf("%d\n",n*m-flag); }