Codeforces243C-Colorado Potato Beetle(离散化+bfs)
Old MacDonald has a farm and a large potato field, (1010 + 1) × (1010 + 1) square meters in size. The field is divided into square garden beds, each bed takes up one square meter.
Old McDonald knows that the Colorado potato beetle is about to invade his farm and can destroy the entire harvest. To fight the insects, Old McDonald wants to spray some beds with insecticides.
So Old 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 each 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 have any intersection at all with Old McDonald's trajectory, are sprayed with insecticides.
When 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 be infected after the invasion of the Colorado beetles.
It is known that the invasion of the Colorado beetles goes as follows. First some bed on the field border gets infected. Than any bed that hasn't been infected, hasn't been sprayed with insecticides and has a common side with an infected bed, gets infected as well. Help Old McDonald and determine the number of beds that won't be infected by the Colorado potato beetle.
Input
The first line contains an integer n (1 ≤ n ≤ 1000) — the number of Old McDonald's movements.
Next n lines contain the description of Old McDonald's movements. The i-th of these lines describes the i-th movement. Each movement is given in the format "dixi", where di is the character that determines the direction of the movement ("L", "R", "U" or "D" for directions "left", "right", "up" and "down", correspondingly), and xi (1 ≤ xi ≤ 106) is an integer that determines the number of meters in the movement.
Output
Print a single integer — the number of beds that won't be infected by the Colorado potato beetle.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
Sample Input
5
R 8
U 9
L 9
D 8
L 2
101
7
R 10
D 2
L 7
U 9
D 2
R 3
D 10
52 题意:有一个非常大的田地(你当它是无穷大的),每一格大小是1m*1m,主人公要喷杀虫剂,他从起点开始,可以朝U(上),D(下),L(左),R(右)四个方向走一段距离,则这段路上的格子都被喷了杀虫剂,
给出N次操作,每次输入一个字符和数字,分别代表方向和路程。害虫入侵田地的方式是从外围入侵,如果某个相邻的格子没有被喷杀虫剂则可以蔓延。最后要求出剩下没被入侵的总面积。 解析:田地太大,面积太大,直接搜是不可能的,但是N最大只有1000,那么可以考虑把x坐标y坐标离散化,点最多也就上千,这里有一个技巧,就是离散化后的点扩大两倍,为了后面方便bfs,所以我把
最外围扩了一圈,那么离散的坐标就是1,3,5.....2*k+1。 0和2*k+2是外围。我把整个图压缩成了一维,比如(x,y)对应的下标就是x*列宽+y,接下来就是bfs,先把走过的地方全部标记为1,其他地方
标记为-1,此时我只需要把(0,0)加入队列搜即可(你可以仔细想一下为什么),把能够走的地方全部标记为0。但是要注意一点,因为我是扩大了两倍,如果当前搜的方向是上或下,且这个点的y坐标是偶数
(不是边界),需要判断一下离散化的左右两个数的差值是否为1,是的话是不能走的(被堵死了),因为这个点本就是虚拟的点。如果当前搜的方向是左或右,x坐标是偶数(不是边界)同理。最后就是计算总
面积,如果这个点不是标记为0(不能被害虫入侵),则判断它的x,y坐标的奇偶性,x,y同时为奇数则加1,x为偶数,则加x轴方向的距离-1,y为偶数,则加y轴方向的距离-1,都是偶数则加两个方向的距离-1
的乘积。(注意会爆int)
代码
#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+;
const double eps=1e-;
const int maxn=;
int M,wx[maxn],wy[maxn];
int tempx[maxn],tempy[maxn],row,col;
int dx[]={-,,,},dy[]={,-,,};
int maze[];
bool in(int x,int y){ return x>=&&x<row*+&&y>=&&y<col*+; }//是否在界内
int GetId(int x,int y){ return x*(col*+)+y; }
void Print(int A[],int N)
{
for(int i=;i<=N;i++) printf("%d ",A[i]);
puts("");
}
void init()
{
for(int i=;i<=M;i++) tempx[i]=wx[i],tempy[i]=wy[i];
sort(tempx,tempx+M+); //下面是离散化过程
sort(tempy,tempy+M+);
row=col=;
for(int i=;i<=M;i++) if(tempx[i]!=tempx[row]) tempx[++row]=tempx[i]; //去重
for(int i=;i<=M;i++) if(tempy[i]!=tempy[col]) tempy[++col]=tempy[i];
}
struct node
{
int x,y;
node(int x=,int y=):x(x),y(y){}
};
queue<node> que;
void bfs()
{
memset(maze,-,sizeof(maze));
for(int i=;i<=M;i++)
{
int x1=lower_bound(tempx,tempx+row+,wx[i-])-tempx; //找对应离散化后的值
int y1=lower_bound(tempy,tempy+col+,wy[i-])-tempy;
int x2=lower_bound(tempx,tempx+row+,wx[i])-tempx;
int y2=lower_bound(tempy,tempy+col+,wy[i])-tempy;
x1=x1*+; y1=y1*+; x2=x2*+; y2=y2*+; //扩大
if(x1==x2)
{
if(y1>y2) swap(y1,y2);
for(int st=y1;st<=y2;st++) maze[GetId(x1,st)]=; //做标记
}
else if(y1==y2) //同理
{
if(x1>x2) swap(x1,x2);
for(int st=x1;st<=x2;st++) maze[GetId(st,y1)]=;
}
}
while(!que.empty()) que.pop();
que.push(node(,)); //把(0,0)丢进队列
maze[GetId(,)]=;
while(!que.empty())
{
node& t=que.front(); que.pop();
int x=t.x,y=t.y;
for(int i=;i<;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(!in(nx,ny)) continue; //出界不管
int id=GetId(nx,ny);
if(ny%==&&(i==||i==)&&in(nx,ny-)&&in(nx,ny+))//y坐标为偶数且方向是上下,不是外围
{
int a=(ny-)/;
int b=(ny+)/;
if(tempy[b]-tempy[a]-<=) continue; //值差为1不能走
}
if(nx%==&&(i==||i==)&&in(nx-,ny)&&in(nx+,ny)) //同理
{
int a=(nx-)/;
int b=(nx+)/;
if(tempx[b]-tempx[a]-<=) continue;
}
if(maze[id]==-){ que.push(node(nx,ny)); maze[id]=; } //标记为0
}
}
}
LL solve()
{
LL ret=;
bfs();
for(int i=;i<row*+;i++)
for(int j=;j<col*+;j++)
{
int p=GetId(i,j);
if(maze[p]==) continue;
int a=(j+)/,b=(j-)/;
int c=(i+)/,d=(i-)/;
if(i%==&&j%==) ret++; //都是奇数
else if(i%==) ret+=(LL)tempy[a]-tempy[b]-; //x坐标为奇数,y坐标为偶数
else if(j%==) ret+=(LL)tempx[c]-tempx[d]-; //y坐标为奇数,x坐标为偶数
else ret+=(LL)(tempx[c]-tempx[d]-)*(tempy[a]-tempy[b]-); //均为偶数
}
return ret;
}
int main()
{
while(scanf("%d",&M)!=EOF)
{
wx[]=,wy[]=;
for(int i=;i<=M;i++)
{
char c;
int d;
scanf(" %c %d",&c,&d);
wx[i]=wx[i-]; wy[i]=wy[i-];
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 ;
}
Codeforces243C-Colorado Potato Beetle(离散化+bfs)的更多相关文章
- Colorado Potato Beetle(CF的某道) & 鬼畜宽搜
题意: 一个人在一张大图上走,给你路径与起点,求他走出的矩形面积并.(大概这个意思自行百度标题... SOL: 与其说这是一道图论题不如说是一道生动活泼的STL-vector教学.... 离散化宽搜, ...
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
Coconuts Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- 离散化+BFS HDOJ 4444 Walk
题目传送门 /* 题意:问一个点到另一个点的最少转向次数. 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 关键理解坐标离散化,BFS部分可参考HDOJ_1728 */ # ...
- hdu 4400 Mines(离散化+bfs+枚举)
Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all ...
- Codeforces Round #150 (Div. 2)
A. Dividing Orange 模拟. B. Undoubtedly Lucky Numbers 暴力枚举\(x.y\). C. The Brand New Function 固定左端点,右端点 ...
- FZU2235 国王的出游 水题
因为只有1e5个点,所以直接离散化bfs就好 #include <cstdio> #include <cstring> #include <queue> #incl ...
- uva 12171 hdu 1771 Sculpture
//这题从十一点开始写了四十分钟 然后查错一小时+ 要吐了 这题题意是给很多矩形的左下角(x,y,z最小的那个角)和三边的长(不是x,y,z最大的那个角T-T),为组成图形的面积与表面积(包在内部的之 ...
- UVA 12171 Sculpture
https://vjudge.net/problem/UVA-12171 题目 某人设计雕塑,用的是很扯的方法:把一堆长方体拼起来.给出长方体的坐标和长宽高,求外表面积.因为要将这雕塑进行酸洗,需要知 ...
- hdu 4444 Walk (离散化+建图+bfs+三维判重 好题)
Walk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
随机推荐
- hdu5024-Wang Xifeng's Little Plot
此题一开始用暴力做,后来发现斜着走的时候其实暴力不太好写,于是改用搜索写了 #include <iostream> #include <stdio.h> #include &l ...
- SRM 599 DIV1
A 首先发现对于2操作,每种素因子可以单独考虑,然后取出步数最多的计入答案,然后分别加上对每种素因子的1操作; 第二步我犯了个错误,以为最优方案是把素因子指数按二进制操作,在1的位置执行1操作,0的位 ...
- 学习phpcms模板方法:
1.改官方模板,读里面的代码,改改它,看看有什么变化,如果不明白,去官方论 坛.查手册.专业人士还可以看数据库.2.复制实例代码,整理笔记,到实战的时候,就直接复制,改改参数即可.
- Ubuntu mysql安装,还有可视化界面
安装mysql sudo apt-get install mysql-server sudo apt-get install mysql-client sudo apt-get install lib ...
- 第25讲 UI组件之 AlertDialog 的各种实现
第25讲 UI组件之AlertDialog 的各种实现 对话框(Dialog)是程序运行中的弹出窗口,例如当用户要删除一个联系方式时,会弹出一个对话框. Android提供了多种对话框:警告对话框(A ...
- 初识前端HTML
HTML 超文本标记语言 HTML的解析 顾名思义,HTML就是由一个个的标签组成的,组成后,HTML可被浏览器直接识别以及处理成我们想给用户展示的样子. 下面我们就来解析HTML的一个个标签. &l ...
- html中行内元素与块级元素的区别。
在标准文档流里面,块级元素具有以下特点 1.总是在新行上开始,占据一整行: 2.高度,行高以及外边框和内边距都可以控制: 3.宽度始终是与游览器宽度一样,与内容无关: 4.它可以容纳内联元素和其他块级 ...
- Visual Studio 2015 开发MVC4出现错误
在Visual Studio 2015(以下简称VS2015)中开发MVC4项目时,编译报错"当前上下文中不存在ViewBag",一直无法编译,这个是否是VS2015的Bug? 本 ...
- 网站全局js代码
这几天开始看公司的一套系统,整理的网站全局js代码 /*文件名:base.js功能说明:全站通用的全局变量及公用方法创建日期:2010-09-26*///引入jquery库文件document.wri ...
- PL/SQL中字符串变量的分割转化
在编写PL/SQL时,有时候我们需要处理这样一个输入的变量,它的格式是由多个值通过分隔符组成的字符串,如“1,2,3”,我们需要将这个变量加入到我们的SQL中,形成诸如in('1','2','3')的 ...