思路:离散化之后,直接模拟就行,标记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. 阿里云ECS服务器 常见问题(1)

    无法在外网访问服务器的公网ip 解决方法: 在阿里云 云服务器ECS-安全组规则 添加端口 可更根据阿里的教程来 配置完成后即可访问!

  2. sitecore系统教程之默认收集数据库MongoDB注意事项

    MongoDB是一个高度可扩展的基于文档的NoSQL数据库解决方案,Sitecore体验数据库(xDB)用于收集数据库.在安装MongoDB之前,您应该考虑以下事项: 确定您是需要基于公共云的解决方案 ...

  3. Helter Skelter (扫描线 + 离散化 + 树状数组)

    扫描线:按照其中一个区间的标记为pos,然后左区间标记d为正影响,有区间标记d为负影响,然后根据所有的pos排序.pos从小扫到大,那么对于某一个区间一定会被扫过2次,那么经过2次之后就只剩下中间那一 ...

  4. MVC请求管道

    下面是请求管道中的19个事件. (1)BeginRequest: 开始处理请求 (2)AuthenticateRequest授权验证请求,获取用户授权信息 (3):PostAuthenticateRe ...

  5. GO slim

    1. GO slim简介 GO slims are cut-down versions of the GO ontologies containing a subset of the terms in ...

  6. pandas.io.common.CParserError: Error tokenizing data. C error: Expected 1 fields in line 526, saw 5

    pandas.io.common.CParserError: Error tokenizing data. C error: Expected 1 fields in line 526, saw 5 ...

  7. Saiku + Kylin 多维分析平台探索

    背景 为了应对各种数据需求,通常,我们的做法是这样的: 对于临时性的数据需求:写HQL到Hive里去查一遍,然后将结果转为excel发送给需求人员. 对于周期性的.长期性的数据需求:编写脚本,结合Hi ...

  8. JAVA基础3---运算符大全

    Java中的运算符有以下种类:算术运算符.关系运算符.位运算符.逻辑运算符.赋值运算符.其他的运算符 现在假设定义 int A = 10,B = 5: 一.算术运算符 运算符 描述 案例 + 等同于数 ...

  9. nextjs 服务端渲染请求参数

    Post.getInitialProps = async function (context) { const { id } = context.query const res = await fet ...

  10. bzoj3678 简单题

    题目链接 bitset #include<algorithm> #include<iostream> #include<cstdlib> #include<c ...