转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove
#include<iostream> #include<cstdio> #include<map> #include<cstring> #include<cmath> #include<vector> #include<stack> #include<algorithm> #include<set> #include<string> #include<queue> #define inf 10000000005LL #define M 1000005 #define N 1000005 #define maxn 2000005 #define eps 1e-7 #define zero(a) fabs(a)<eps #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) #define pb(a) push_back(a) #define mp(a,b) make_pair(a,b) #define mem(a,b) memset(a,b,sizeof(a)) #define LL long long #define MOD 1000000007 #define lson step<<1 #define rson step<<1|1 #define sqr(a) ((a)*(a)) #define Key_value ch[ch[root][1]][0] #define test puts("OK"); #define pi acos(-1.0) #define lowbit(x) ((x)&(-(x))) #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; struct Node{ int x1,x2,y1,y2; Node(){} Node(int _x1,int _y1,int _x2,int _y2):x1(_x1),y1(_y1),x2(_x2),y2(_y2){} }rec[1005]; int way[4][2]={0,1,0,-1,1,0,-1,0}; int m; int vis[2005][2005]={0}; vector<LL>x,y; queue<pair<LL,LL> >que; int id(char ch){ if(ch=='R') return 0; if(ch=='L') return 1; if(ch=='U') return 3; return 2; } int Bin(vector<LL>v,LL num){ return find(v.begin(),v.end(),num)-v.begin(); } int main(){ int x1=0,y1=0,x2,y2; scanf("%d",&m); x.pb(-inf);x.pb(inf); y.pb(-inf);y.pb(inf); for(int i=0;i<m;i++){ char str[5];int val; scanf("%s%d",&str,&val); int idx=id(str[0]); x2=x1+way[idx][0]*val;y2=y1+way[idx][1]*val; x.pb(min(x1,x2));x.pb(max(x1,x2)+1); y.pb(min(y1,y2));y.pb(max(y1,y2)+1); rec[i]=Node(min(x1,x2),min(y1,y2),max(x1,x2)+1,max(y1,y2)+1); x1=x2;y1=y2; } sort(x.begin(), x.end()); sort(y.begin(), y.end()); x.resize(unique(x.begin(), x.end())-x.begin()); y.resize(unique(y.begin(), y.end())-y.begin()); for(int i=0;i<m;i++){ x1=Bin(x,rec[i].x1);x2=Bin(x,rec[i].x2)-1; y1=Bin(y,rec[i].y1);y2=Bin(y,rec[i].y2)-1; for(int j=x1;j<=x2;j++) for(int k=y1;k<=y2;k++) vis[j][k]=1; } que.push(mp(0,0)); vis[0][0]=2; while(!que.empty()){ pair<LL,LL>u,v; u=que.front(); que.pop(); for(int i=0;i<4;i++){ v=u; v.first+=way[i][0]; v.second+=way[i][1]; if(v.first>=0&&v.first<x.size()&&v.second>=0&&v.second<y.size()&&vis[v.first][v.second]==0){ vis[v.first][v.second]=2; que.push(v); } } } LL ans=0; for(int i=0;i<x.size()-1;i++) for(int j=0;j<y.size()-1;j++) if(vis[i][j]!=2) ans+=(LL)(x[i+1]-x[i])*(y[j+1]-y[j]); printf("%I64d\n",ans); return 0; }