CF350C 题解
LiJinLin_AFO · · 题解
CF350C 题解
我写的足够屎山长
正文开始
拿到题目,观察一下输出的样例,我们会发现,机器人不会按照输入的顺序来寻找炸弹。所以,我们先排序。怎么排呢?很简单,用结构体存储坐标,排序函数就是比较两个坐标
#include<cmath>//abs()函数所在头文件
struct r{
int x,y;
}a[100005];//坐标结构体
bool cmp(r w,r e){
return abs(w.x)+abs(w.y)<abs(e.x)+abs(e.y);
}//排序函数
排完序,就可以分类枚举了。一共分为八种情况(见图):
(注:情况
(补充:
-
第一象限:
\displaystyle R \Rightarrow U \Rightarrow 2 \Rightarrow L \Rightarrow D \Rightarrow 3 -
第四象限:
\displaystyle R \Rightarrow D \Rightarrow 2 \Rightarrow L \Rightarrow U \Rightarrow 3 -
第二象限:
\displaystyle L \Rightarrow U \Rightarrow 2 \Rightarrow R \Rightarrow D \Rightarrow 3 -
第三象限:
\displaystyle L \Rightarrow D \Rightarrow 2 \Rightarrow R \Rightarrow U \Rightarrow 3 -
x$ 轴正半轴:$\displaystyle R \Rightarrow 2 \Rightarrow L \Rightarrow 3 -
x$ 轴负半轴:$\displaystyle L \Rightarrow 2 \Rightarrow R \Rightarrow 3 -
y$ 轴正半轴:$\displaystyle U \Rightarrow 2 \Rightarrow D \Rightarrow 3 -
y$ 轴负半轴:$\displaystyle D \Rightarrow 2 \Rightarrow U \Rightarrow 3
介绍完毕,上代码:
#include<stdio.h>
#include<cmath>
#include<algorithm>
using namespace std;
int n,ans,out;
struct r{
int x,y;
}a[100005];
struct answer{
int dir;
int first=0;
int first_num;
int second=0;
int second_num;
}kkk[1000005];//输出部分
bool cmp(r w,r e);
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++){
if(a[i].x>0&&a[i].y>0){
kkk[++ans].dir=1;
kkk[ans].first=2;
kkk[ans].first_num=a[i].x;
kkk[ans].second=1;
kkk[ans].second_num=a[i].y;
out+=2;
//-------------------
kkk[++ans].dir=2;
++out;
//-------------------
kkk[++ans].dir=1;
kkk[ans].first=4;
kkk[ans].first_num=a[i].x;
kkk[ans].second=3;
kkk[ans].second_num=a[i].y;
out+=2;
//-------------------
kkk[++ans].dir=3;
++out;//情况1
}
else if(a[i].x>0&&a[i].y<0){
kkk[++ans].dir=1;
kkk[ans].first=2;
kkk[ans].first_num=a[i].x;
kkk[ans].second=3;
kkk[ans].second_num=-a[i].y;
out+=2;
//-------------------
kkk[++ans].dir=2;
++out;
//-------------------
kkk[++ans].dir=1;
kkk[ans].first=4;
kkk[ans].first_num=a[i].x;
kkk[ans].second=1;
kkk[ans].second_num=-a[i].y;
out+=2;
//-------------------
kkk[++ans].dir=3;
++out;
}//情况2
else if(a[i].x<0&&a[i].y>0){
kkk[++ans].dir=1;
kkk[ans].first=4;
kkk[ans].first_num=-a[i].x;
kkk[ans].second=1;
kkk[ans].second_num=a[i].y;
out+=2;
//-------------------
kkk[++ans].dir=2;
++out;
//-------------------
kkk[++ans].dir=1;
kkk[ans].first=2;
kkk[ans].first_num=-a[i].x;
kkk[ans].second=3;
kkk[ans].second_num=a[i].y;
out+=2;
//-------------------
kkk[++ans].dir=3;
++out;
}//情况3
else if(a[i].x<0&&a[i].y<0){
kkk[++ans].dir=1;
kkk[ans].first=4;
kkk[ans].first_num=-a[i].x;
kkk[ans].second=3;
kkk[ans].second_num=-a[i].y;
out+=2;
//-------------------
kkk[++ans].dir=2;
++out;
//-------------------
kkk[++ans].dir=1;
kkk[ans].first=2;
kkk[ans].first_num=-a[i].x;
kkk[ans].second=1;
kkk[ans].second_num=-a[i].y;
out+=2;
//-------------------
kkk[++ans].dir=3;
++out;
}//情况4
else if(a[i].x>0&&!a[i].y){
kkk[++ans].dir=1;
kkk[ans].first=2;
kkk[ans].first_num=a[i].x;
++out;
//-------------------
kkk[++ans].dir=2;
++out;
//-------------------
kkk[++ans].dir=1;
kkk[ans].first=4;
kkk[ans].first_num=a[i].x;
++out;
//-------------------
kkk[++ans].dir=3;
++out;
}//情况5
else if(a[i].x<0&&!a[i].y){
kkk[++ans].dir=1;
kkk[ans].first=4;
kkk[ans].first_num=-a[i].x;
++out;
//-------------------
kkk[++ans].dir=2;
++out;
//-------------------
kkk[++ans].dir=1;
kkk[ans].first=2;
kkk[ans].first_num=-a[i].x;
++out;
//-------------------
kkk[++ans].dir=3;
++out;
}//情况6
else if(!a[i].x&&a[i].y>0){
kkk[++ans].dir=1;
kkk[ans].first=1;
kkk[ans].first_num=a[i].y;
++out;
//-------------------
kkk[++ans].dir=2;
++out;
//-------------------
kkk[++ans].dir=1;
kkk[ans].first=3;
kkk[ans].first_num=a[i].y;
++out;
//-------------------
kkk[++ans].dir=3;
++out;
}//情况7
else if(!a[i].x&&a[i].y<0){
kkk[++ans].dir=1;
kkk[ans].first=3;
kkk[ans].first_num=-a[i].y;
++out;
//-------------------
kkk[++ans].dir=2;
++out;
//-------------------
kkk[++ans].dir=1;
kkk[ans].first=1;
kkk[ans].first_num=-a[i].y;
++out;
//-------------------
kkk[++ans].dir=3;
++out;
}//情况8
}
printf("%d\n",out);//总步数
for(int i=1;i<=ans;i++){
printf("%d ",kkk[i].dir);
if(kkk[i].dir!=1){
puts("");
continue;
}//拾取或摧毁
printf("%d ",kkk[i].first_num);
if(kkk[i].first==1) puts("U");
if(kkk[i].first==2) puts("R");
if(kkk[i].first==3) puts("D");
if(kkk[i].first==4) puts("L");
if(!kkk[i].second) continue;
printf("%d ",kkk[i].dir);
printf("%d ",kkk[i].second_num);
if(kkk[i].second==1) puts("U");
if(kkk[i].second==2) puts("R");
if(kkk[i].second==3) puts("D");
if(kkk[i].second==4) puts("L");
//输出
}
return 0;
}
bool cmp(r w,r e){
return abs(w.x)+abs(w.y)<abs(e.x)+abs(e.y);
}//排序
注:请勿抄袭!