思路:离散化之后,直接模拟就行,标记vis开三维

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + ;
const ll mod = 1e8 + ;
struct node{
ll x, y;
}p[maxn];
int mp[maxn][maxn], turn_num, vis[maxn][maxn][], nn, mm;
ll x[maxn], y[maxn];
int dir[][] = {, , , -, -, , , };
//下,左,上,右
bool dfs(int xx, int yy, int turn){ //将要朝向turn
if(vis[xx][yy][turn]) return false;
vis[xx][yy][turn] = ;
int xxx = xx + dir[turn][], yyy = yy + dir[turn][];
if(xxx < || xxx > nn - || yyy < || yyy > mm - )
return true;
while(mp[xxx][yyy] != ){
if(vis[xxx][yyy][turn]) return false;
vis[xxx][yyy][turn] = ;
xxx += dir[turn][];
yyy += dir[turn][];
if(xxx < || xxx > nn - || yyy < || yyy > mm - )
return true;
} turn_num++;
return dfs(xxx - dir[turn][], yyy - dir[turn][], (turn + ) % );
} int main(){
int n, sx, sy;
while(scanf("%d", &n) != EOF){
turn_num = ;
memset(mp, , sizeof(mp));
memset(vis, , sizeof(vis));
for(int i = ; i < n; i++){
scanf("%lld%lld", &p[i].x, &p[i].y);
x[i] = p[i].x;
y[i] = p[i].y;
}
x[n] = , y[n] = , p[n].x = , p[n].y = ;
n++;
sort(x, x + n);
sort(y, y + n);
int num1 = unique(x, x + n) - x;
int num2 = unique(y, y + n) - y;
for(int i = ; i < n; i++){
int X, Y;
X = lower_bound(x, x + num1, p[i].x) - x;
Y = lower_bound(y, y + num2, p[i].y) - y;
if(p[i].x == && p[i].y == ){
sx = X, sy = Y;
}
else{
mp[X][Y] = ;
}
}
nn = num1, mm = num2;
bool flag = dfs(sx, sy, );
if(flag) printf("%d\n", turn_num);
else printf("-1\n");
}
return ;
}
/*
4
1 0
0 1
0 -1
-1 0
*/

SCU 4445 Right turn(dfs)题解的更多相关文章

  1. 模拟+贪心 SCU 4445 Right turn

    题目传送门 /* 题意:从原点出发,四个方向,碰到一个点向右转,问多少次才能走出,若不能输出-1 模拟:碰到的点横坐标相等或纵坐标相等,然而要先满足碰到点最近, 当没有转向或走到之前走过的点结束循环. ...

  2. SCU 4445 Right turn

    模拟. 每次找一下即将要遇到的那个点,这个数据范围可以暴力找,自己的写的时候二分了一下.如果步数大于$4*n$一定是$-1$. #include<bits/stdc++.h> using ...

  3. [USACO10OCT]Lake Counting(DFS)

    很水的DFS. 为什么放上来主要是为了让自己的博客有一道DFS题解,,, #include<bits/stdc++.h> using namespace std; ][],ans,flag ...

  4. 记 2020蓝桥杯校内预选赛(JAVA组) 赛后总结

    目录 引言 结果填空 1. 签到题 2. 概念题 3. 签到题 4. 签到题 程序题 5. 递增三元组[遍历] 6. 小明的hello[循环] 7. 数位递增[数位dp] 8. 小明家的草地[bfs] ...

  5. NOI 题库 6266

    6266  取石子游戏 描述 有两堆石子,两个人轮流去取.每次取的时候,只能从较多的那堆石子里取,并且取的数目必须是较少的那堆石子数目的整数倍.最后谁能够把一堆石子取空谁就算赢. 比如初始的时候两堆石 ...

  6. 【HDOJ6224】Legends of the Three Kingdoms(概率DP)

    题意:三国杀,给定4个白板武将的血量,4个角色轮流行动,每回合行动时如果该人存活则可以选择使阵营不同的角色血量-1,血量为0则死亡.每个人按自己获胜概率最大化行动,如果有多种方案概率相同则等概率选择这 ...

  7. hdu4778 Gems Fight!

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others) Total Submis ...

  8. 【题解】 bzoj2115: [Wc2011] Xor (线性基+dfs)

    bzoj2115,戳我戳我 Solution: 看得题解(逃,我太菜了,想不出这种做法 那么丢个链接 Attention: 板子别写错了 又写错了这次 \(long long\)是左移63位,多了会溢 ...

  9. HDU-3974 Assign the task题解报告【dfs序+线段树】

    There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...

随机推荐

  1. wordpress学习(四)---url伪静态简单了解

    在page-url.php页面做测试 <h2>url伪静态</h2> 接收到的pagename参数是:<? echo $wp_query->query_vars[' ...

  2. 将n的k位s置1

    实例四:将n的k位s置1 方法:result =n|(1<<k) 只使k位变为1,其他位为0,再进行或操作,1与任何数的或操作都是1. 解释: 原数 0000 1011 ---11 数值1 ...

  3. Yii2 Restful api搜索实现

  4. 排序(Sort)-----选择排序

       声明:文中动画转载自https://blog.csdn.net/qq_34374664/article/details/79545940    1.选择排序简介 选择排序(Select Sort ...

  5. Linux 运维测试及第三应用及测试工具

    一 .第三方应用及测试工具链接地址 https://pan.baidu.com/s/1rLQ5NCZvxcy93YQ4fGFaBQ 1.linux LSI系列raid卡监测工具 1)使用参数详解链接: ...

  6. Vue:将px转化为rem,适配移动端vant-UI等框架(px2rem-loader)

    转载:https://www.cnblogs.com/WQLong/p/7798822.html 1.下载lib-flexible 使用的是vue-cli+webpack,通过npm来安装的 npm ...

  7. 世界最顶级邮件服务器组合Linux + PMTA + OEMPRO,PowerMTA 安装

    世界最顶级邮件服务器组合Linux + PMTA + OEMPRO PowerMTA 安装 PMTA + OEMPRO  这个是发送的组合 PMTA提供的SMTP,OEMPRO是订阅管理以及邮件的过滤 ...

  8. multiple definition of qt_plugin_query_metadata

    dustije 5 years ago I have a project with several plugins i want to compile into one library. I get ...

  9. js 简易时钟

    html部分 <div id="clock"> </div> css部分 #clock{ width:600px ; text-align: center; ...

  10. win7使用问题解决

    1. VM和主机互相PING不通 问题:桥接模式,VM可以ping 通外网,可以ping 通局域网其它机子,就是ping 不通本地主机 解决:将 vm网卡和本地网连接网卡都共享出来