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 ...
随机推荐
- Python记录2:数据类型
一Python的数据类型可以分为可变与不可变两种: 可变类型:值改变,但是id不变,证明就是在改变原值,就是可变类型 如list dict 列表和字典都是可变类型 不可变类型:值改变,id也跟着改 ...
- python ip代理
import random import urllib.request from bs4 import BeautifulSoup import time url ='http://www.whati ...
- 4.GDscript(2)关键字,运算符,字面量
(来源godot官方文档) 关键词 下面是该语言支持的关键字列表.由于关键字是保留字(令牌),它们不能用作标识符.操作符(如 in , not , and 或 or )以及下面列出的内置类型的名称也是 ...
- uva 12222 Mountain Road
题意: 有一个单行道,两个方向都有车在等待.给出每个车的方向以及到达的时间以及走完这段路所需要的时间. 为了防止车祸,同向两车通过任一点的时间间隔不得小于10s. 求最后一辆车离开时刻的最小值. 思路 ...
- MySql 应用语句
[1]MySQL基础语句 -- 查询mysql版本号 SELECT VERSION(); -- 创建数据库 DROP DATABASE IF EXISTS study; -- 如果存在先删除 CREA ...
- weblogic 乱码
1.找到weblogic安装目录,当前项目配置的domain 2.找到bin下的setDomainEnv.cmd文件 3.打开文件,从文件最后搜索第一个set JAVA_OPTIONS=%JAVA_O ...
- android dumpsys
dumpsys dumpsys is a tool that runs on Android devices and provides information about system service ...
- linux常用命令:cat 命令
cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...
- 20165215 2017-2018-2 《Java程序设计》第4周学习总结
20165215 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容总结 chapter5 子类与父类 子类的定义使用关键字extends 任何类都是Object类的 ...
- GUI常用对象的属性
%常用对象的属性 %.figure %hf=figure; %get(hf); %改变颜色 set Color %set(hf,'Color','w'); %去掉默认的菜单 Menubar %set( ...