Old MacDonald have a farm and a large potato field, (1 +) x (ten+ 1) square meters in size. The field is divided to square garden beds and each bed takes up one square meter.
Old McDonald knows that the Colorado potato beetle are about to invade his farm and can destroy the entire harvest. To fight the insects, old McDonald wants to spray some beds with insecticides.
McDonald went to the field, stood at the center of the central field bed and sprayed this bed with insecticides. Now he's going to make a series of movements and spray a few more beds. During movement Old McDonald moves left, right, up or down the field some integer number of meters. As Old McDonald moves, he sprays all the beds he steps on. In other words, the beds that has any intersection at all with an old McDonald ' s trajectory, is sprayed with insecticides.
When the old McDonald finished spraying, he wrote out all his movements on a piece of paper. Now he wants to know how many beds won ' t being infected after the invasion of the Colorado beetles.
It is known the invasion of the Colorado beetles goes as follows. First some bed on the field border gets infected. Than any bed this hasn ' t been infected, hasn ' t been sprayed with insecticides and have a common side with an infected bed, Gets infected as well. Help old McDonald and determine the number of beds this won ' t be infected by the Colorado potato beetle.
Input
The first line contains a integer n (1≤ n ≤1000)-the number of old McDonald ' s movem Ents.
NextNLines contain the description of old McDonald ' s movements. TheI-th of these lines describes theI-th movement. Each movement was given in the format "D i x i ", where D i is the character that determines the direction of the movement ("l", "r", "U" or " x i (1≤ x i ≤106) is a integer that determines the number of meters in the movement.
Output
Print a single integer-the number of beds that won ' t is infected by the Colorado potato beetle.
%lld specifier to read or write 64-bit integers inс++. It is preferred to use the CIN, cout streams or the %i64dspecifier.
Sample Input
Input
5 R 8 U 9 L 9 D 8 L 2
Output
101
Input
7 R Ten D 2 L 7 U 9 d 2 R 3 d 10
Output
52
Test instructions: There is a very large field (you when it is infinite), each size is 1m*1m, the protagonist to spray insecticide, he starts from the beginning, you can go toward the U (Upper), D (lower), L (left), R (right) Four directions to a certain distance, then this section of the road is sprayed with pesticides,
Give n operations, enter a character and number each time, respectively, representing the direction and distance. The way a pest invades a field is from a perimeter, and if an adjacent lattice is not sprayed with pesticides, it can spread. The final requirement is the total area of the remaining not invaded.
Analysis: The field is too large, the area is too large, direct search is impossible, but n the maximum of only 1000, then you can consider the x-coordinate y-coordinate discretization, the point is thousands, here is a technique, that is, the dot expansion after the discretization twice times, in order to back convenient BFS, so I put
The periphery expands a circle, so the discrete coordinates are 1,3,5.....2*k+1. 0 and 2*k+2 are peripheral. I compressed the whole picture into one dimension, for example (x, y) corresponding subscript is x* column width +y, next is BFS, first to pass the place all marked 1, other places
Labeled as-1, at this point I just need to add (0,0) to the queue (you can think about why) and mark all the places you can go to 0. But one thing to be aware of, because I was enlarged twice times, if the current search direction is up or down, and the y-coordinate of this point is even
(not the boundary), you need to determine whether the difference between the left and right two number of discretization is 1, is the words can not go (blocked), because this point is the virtual point. If the current search direction is left or right, the x-coordinate is even (not the boundary) in the same way. The final calculation is the total
Area, if this point is not marked as 0 (can not be invaded by pests), then determine its x, y coordinates of the parity, X, y and the odd number is added 1,x to even, then the x-axis direction of the distance -1,y to even, then the y-axis direction of the distance-1, is an even number plus two direction distance-1
of the product. (Attention will explode int)
Code
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <iterator>
#include <stack>
using namespace Std;
typedef __int64 LL;
const int inf=1e9+7;
const double eps=1e-7;
const int maxn=2002;
int M,WX[MAXN],WY[MAXN];
int tempx[maxn],tempy[maxn],row,col;
int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int maze[2000005];
BOOL in (int x,int y) {return x>=0&&x<row*2+3&&y>=0&&y<col*2+3;}
int GetId (int x,int y) {return x* (col*2+3) +y;}
void Print (int a[],int N)
{
for (int i=0;i<=n;i++) printf ("%d", a[i]);
Puts ("");
}
void Init ()
{
for (int i=0;i<=m;i++) tempx[i]=wx[i],tempy[i]=wy[i];
Sort (tempx,tempx+m+1);
Sort (tempy,tempy+m+1);
row=col=0;
for (int i=1;i<=m;i++) if (Tempx[i]!=tempx[row]) tempx[++row]=tempx[i];
for (int i=1;i<=m;i++) if (Tempy[i]!=tempy[col]) tempy[++col]=tempy[i];
}
struct node
{
int x, y;
Node (int x=0,int y=0): X (x), Y (y) {}
};
Queue<node> que;
void BFs ()
{
memset (Maze,-1,sizeof (Maze));
for (int i=1;i<=m;i++)
{
int X1=lower_bound (tempx,tempx+row+1,wx[i-1])-tempx;
int Y1=lower_bound (tempy,tempy+col+1,wy[i-1])-tempy;
int X2=lower_bound (tempx,tempx+row+1,wx[i])-tempx;
int Y2=lower_bound (tempy,tempy+col+1,wy[i])-tempy;
x1=x1*2+1; y1=y1*2+1; x2=x2*2+1; y2=y2*2+1;
if (X1==X2)
{
if (y1>y2) swap (Y1,Y2);
for (int st=y1;st<=y2;st++) Maze[getid (x1,st)]=1;
}
else if (y1==y2)
{
if (x1>x2) swap (X1,X2);
for (int st=x1;st<=x2;st++) Maze[getid (st,y1)]=1;
}
}
while (!que.empty ()) Que.pop ();
Que.push (Node (0,0));
Maze[getid (0,0)]=-1;
while (!que.empty ())
{
node& T=que.front (); Que.pop ();
int x=t.x,y=t.y;
for (int i=0;i<4;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if (!in (Nx,ny)) continue;
int Id=getid (NX,NY);
if (ny%2==0&& (i==0| | i==2) &&in (nx,ny-1) &&in (nx,ny+1))
{
int a= (NY-1)/2;
int b= (ny+1)/2;
if (tempy[b]-tempy[a]-1<=0) continue;
}
if (nx%2==0&& (i==1| | i==3) &&in (nx-1,ny) &&in (Nx+1,ny))
{
int a= (NX-1)/2;
int b= (nx+1)/2;
if (tempx[b]-tempx[a]-1<=0) continue;
}
if (maze[id]==-1) {Que.push (node (nx,ny)); maze[id]=0;}
}
}
}
LL Solve ()
{
LL ret=0;
BFS ();
for (int i=0;i<row*2+3;i++)
for (int j=0;j<col*2+3;j++)
{
int P=getid (I,J);
if (maze[p]==0) continue;
int a= (j+1)/2,b= (j-1)/2;
int c= (i+1)/2,d= (i-1)/2;
if (i%2==1&&j%2==1) ret++;
else if (i%2==1) ret+= (LL) tempy[a]-tempy[b]-1;
else if (j%2==1) ret+= (LL) tempx[c]-tempx[d]-1;
else ret+= (LL) (tempx[c]-tempx[d]-1) * (tempy[a]-tempy[b]-1);
}
return ret;
}
int main ()
{
while (scanf ("%d", &m)!=eof)
{
wx[0]=0,wy[0]=0;
for (int i=1;i<=m;i++)
{
char c;
int D;
scanf ("%c%d", &c,&d);
WX[I]=WX[I-1]; WY[I]=WY[I-1];
if (c== ' U ') Wx[i]-=d;
else if (c== ' D ') Wx[i]+=d;
else if (c== ' L ') Wy[i]-=d;
else if (c== ' R ') Wy[i]+=d;
printf ("%d%d\n", wx[i],wy[i]);
}
Init ();
printf ("%i64d\n", Solve ());
}
return 0;
}
Codeforces243c-colorado Potato Beetle (discrete +bfs)