CF 243C Colorado Potato Beetle(BFS+离散化)

来源:互联网 发布:matlab k聚类算法 编辑:程序博客网 时间:2024/05/19 16:33

转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove

题意:给出一个非常大的农田,农夫从中心出发,走过的地方,都会撒农药。
害虫可以按4邻接扩散,问有多少块农田没有被破坏
http://codeforces.com/problemset/problem/243/C 
我会说我题目看了很久?
哎,这两天真心后悔六级没报上名,我急着要六级证书啊, 不过估计也过了。
虽然地图非常大,但是路径是有限的,而且相对于只是细细的长条,所以直接成了矩形面积并。
而且由于是细细的长条,那么直接离散化后暴力染色。
然后从边界开始,BFS搜索,把害虫能覆盖的染色
最后继续暴力统计,最后还挺快,呵呵,最多也就2000*2000吧。
#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;}


原创粉丝点击