SCU 4445 Right turn(dfs)题解
思路:离散化之后,直接模拟就行,标记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)题解的更多相关文章
- 模拟+贪心 SCU 4445 Right turn
题目传送门 /* 题意:从原点出发,四个方向,碰到一个点向右转,问多少次才能走出,若不能输出-1 模拟:碰到的点横坐标相等或纵坐标相等,然而要先满足碰到点最近, 当没有转向或走到之前走过的点结束循环. ...
- SCU 4445 Right turn
模拟. 每次找一下即将要遇到的那个点,这个数据范围可以暴力找,自己的写的时候二分了一下.如果步数大于$4*n$一定是$-1$. #include<bits/stdc++.h> using ...
- [USACO10OCT]Lake Counting(DFS)
很水的DFS. 为什么放上来主要是为了让自己的博客有一道DFS题解,,, #include<bits/stdc++.h> using namespace std; ][],ans,flag ...
- 记 2020蓝桥杯校内预选赛(JAVA组) 赛后总结
目录 引言 结果填空 1. 签到题 2. 概念题 3. 签到题 4. 签到题 程序题 5. 递增三元组[遍历] 6. 小明的hello[循环] 7. 数位递增[数位dp] 8. 小明家的草地[bfs] ...
- NOI 题库 6266
6266 取石子游戏 描述 有两堆石子,两个人轮流去取.每次取的时候,只能从较多的那堆石子里取,并且取的数目必须是较少的那堆石子数目的整数倍.最后谁能够把一堆石子取空谁就算赢. 比如初始的时候两堆石 ...
- 【HDOJ6224】Legends of the Three Kingdoms(概率DP)
题意:三国杀,给定4个白板武将的血量,4个角色轮流行动,每回合行动时如果该人存活则可以选择使阵营不同的角色血量-1,血量为0则死亡.每个人按自己获胜概率最大化行动,如果有多种方案概率相同则等概率选择这 ...
- hdu4778 Gems Fight!
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others) Total Submis ...
- 【题解】 bzoj2115: [Wc2011] Xor (线性基+dfs)
bzoj2115,戳我戳我 Solution: 看得题解(逃,我太菜了,想不出这种做法 那么丢个链接 Attention: 板子别写错了 又写错了这次 \(long long\)是左移63位,多了会溢 ...
- 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 ...
随机推荐
- Core Java Fundation
http://www.cnblogs.com/cmfwm/p/7671188.html http://blog.csdn.net/fuckluy/article/details/50614983 ht ...
- hdu4778 状态压缩
#include <iostream> #include <algorithm> #include <cstdio> #include <vector> ...
- Java将对象保存到文件中/从文件中读取对象
1.保存对象到文件中 Java语言只能将实现了Serializable接口的类的对象保存到文件中,利用如下方法即可: public static void writeObjectToFile(Obje ...
- 正确把mysql数据库从windows迁移到linux系统上的方法
(一)用mysqldump命令导出数据库文件: 在windows下cd到Mysql的bin目录: c:/data.txt这个目录和导出的文本名可以自己随便取,-B 后面的是表名,我要导出的表明叫use ...
- ★★★kalinux 常用命令
1.修改密码: sudo passwd root 2.重启:reboot ====================================== arch 显示机器的处理器架构(1) una ...
- impala与hive的比较以及impala的有缺点
最近读的几篇关于impala的文章,这篇良心不错:https://www.biaodianfu.com/impala.html(本文截取部分内容) Impala是Cloudera公司主导开发的新型查询 ...
- Django框架----路由系统(详细)
Django的路由系统 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表. ...
- springboot+mybaties
1. 开发工具:Intellij idea2018.3 2. file----->new----->project 3. 选择Spring Initializr,点击next 4. 点击n ...
- Spring MVC前传递和后端接收的参数名不一致处理方式
前端传递的变量和后端接收的变量名字不一致时,用注解@RequestParam来实现数据的传递 例如:@RequestParam(value="id") //实现商品的分类目录展现 ...
- 怎样从外网访问内网SQLServer数据库?
本地安装了一个SQLServer数据库,只能在局域网内访问到,怎样从外网也能访问到本地的SQLServer数据库呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动SQLServer数据 ...