Right turn

Right turn

Time Limit: 1000ms

Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

frog is trapped in a maze. The maze is infinitely large and divided into grids. It also consists of n obstacles, where the i-th obstacle lies in grid (xi,yi).

frog is initially in grid (0,0), heading grid (1,0). She moves according to The Law of Right Turn: she keeps moving forward, and turns right encountering a obstacle.

The maze is so large that frog has no chance to escape. Help her find out the number of turns she will make.

Input

The input consists of multiple tests. For each test:

The first line contains 1 integer n (0≤n≤103). Each of the following n lines contains 2 integers xi,yi. (|xi|,|yi|≤109,(xi,yi)≠(0,0), all (xi,yi) are distinct)

Output

For each test, write 1 integer which denotes the number of turns, or ‘‘-1′′ if she makes infinite turns.

Sample Input

2
1 0
0 -1
1
0 1
4
1 0
0 1
0 -1
-1 0

Sample Output

2
0
-1

4种情况的dfs:

    只要把所有情况用代码表达清楚就OK了。这里有几点较为关键:dfs()该传递什么值,如何判断重复,如何找到运动时遇到的第一个障碍物。

    首先,dfs()传递的a[].x,a[].y是障碍物的位置,而物体实际的位置,应该是(a[].x,a[].y-1),(a[].x-1,a[].y),(a[].x,a[].y+1),(a[].x+1,a[].y)这四个不同的状态;所以在判断turn%4后,要先处理一下x和y;所以最初放入dfs()的并不应该是(0,0),而是(0,1)。

    撞到同一个障碍物只有4种方向,并且这4种直接可以用turn%4来表示,判断dis[][]如若有相同,则必有重复。

    turn right 的要求 不过是 遇到一个 同x(or y)的 比当前位置 大(or 小) 的最小(or 最大) 值,找到则turn++,dfs();否则结束。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
const int INF = 0x3f3f3f3f;
using namespace std;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,-1,0,1};
int dis[1005][4];
int sign;
int turn;
int n;
struct node{
    int x,y;
}a[1005];
void dfs(int cnt){
    if(sign)return;
    int tu = turn%4;
    for(int i = 0; i < 4; i++){
        if(dis[cnt][i] == tu){
            sign = 2;return;
        }
        if(dis[cnt][i] == -1){
            dis[cnt][i] = tu;
            break;
        }
    }
    int j = -1;
    if(dy[tu] == 0){
        if(dx[tu] == 1){
            int x = a[cnt].x;
            int y = a[cnt].y-1;
            int xx = INF;
            for(int i = 1; i <= n; i++){
                if(a[i].x > x && a[i].x < xx && a[i].y == y){
                    xx = a[i].x;
                    j = i;
                }
            }
            if(j == -1){
                sign = 1;
                return;
            }
            else {
                turn++;
                dfs(j);
            }
        }
        else {
            int x = a[cnt].x;
            int y = a[cnt].y+1;
            int xx = -INF;
            for(int i = 1; i <= n; i++){
                if(a[i].x < x && a[i].x > xx && a[i].y == y){
                    xx = a[i].x;
                    j = i;
                }
            }
            if(j == -1){
                sign = 1;
                return;
            }
            else {
                turn++;
                dfs(j);
            }
        }
    }
    else {
        if(dy[tu] == -1){
            int x = a[cnt].x-1;
            int y = a[cnt].y;
            int yy = -INF;
            for(int i = 1; i <= n; i++){
                if(a[i].y < y && a[i].y > yy && a[i].x == x){
                    j = i;
                    yy = a[i].y;
                }
            }
            if(j == -1){
                sign = 1;
                return;
            }
            else {
                turn++;
                dfs(j);
            }
        }
        else {
            int x = a[cnt].x+1;
            int y = a[cnt].y;
            int yy = INF;
            for(int i = 1; i <= n; i++){
                if(a[i].y > y && a[i].y < yy && a[i].x == x){
                    yy = a[i].y;
                    j = i;
                }
            }
            if(j == -1){
                sign = 1;
                return;
            }
            else {
                turn++;
                dfs(j);
            }
        }
    }
    return;
}
int main(){
    while(~scanf("%d",&n)){
        for(int i = 1; i <= n; i++){
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        memset(dis,-1,sizeof(dis));
        sign = 0;
        turn = 0;
        a[0].x = 0,a[0].y = 1;
        dfs(0);
        if(sign == 2)puts("-1");
        else printf("%d\n",turn);
    }
    return 0;
}
时间: 2024-10-12 03:56:16

Right turn的相关文章

HDU 4869 Turn the pokers(推理)

HDU 4869 Turn the pokers 题目链接 题意:给定n个翻转扑克方式,每次方式相应能够选择当中xi张进行翻转.一共同拥有m张牌.问最后翻转之后的情况数 思路:对于每一些翻转,假设能确定终于正面向上张数的情况,那么全部的情况就是全部情况的C(m, 张数)之和.那么这个张数进行推理会发现,事实上会有一个上下界,每隔2个位置的数字就是能够的方案,由于在翻牌的时候,相应的肯定会有牌被翻转,而假设向上牌少翻一张,向下牌就要多翻一张.奇偶性是不变的,因此仅仅要每次输入张数,维护上下界,最后

HDU 2438 Turn the corner (计算几何 + 三分)

Turn the corner Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2059    Accepted Submission(s): 785 Problem Description Mr. West bought a new car! So he is travelling around the city. One day h

The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software

The Free Lunch Is Over A Fundamental Turn Toward Concurrency in Software By Herb Sutter The biggest sea change in software development since the OO revolution is knocking at the door, and its name is Concurrency. This article appeared in Dr. Dobb's J

hdu 2483 Turn the corner(三分)

Turn the corner Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1899    Accepted Submission(s): 719 Problem Description Mr. West bought a new car! So he is travelling around the city. One day h

hdu 4869 Turn the pokers(递推&amp;组合数学&amp;逆元)

Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1279    Accepted Submission(s): 466 Problem Description During summer vacation,Alice stay at home for a long time, with nothing t

HDOJ 4869 Turn the pokers

最后的结果中正面向上的奇偶性是一定的,计算出正面向上的范围low,up 结果即为 C(m,low)+ C(m,low+2) +.... + C(m,up) ,用逆元取模 Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 828    Accepted Submission(s): 302 Problem D

HDU 4869 Turn the pokers(思维+组合公式+快速幂)

Turn the pokers 大意:给出n次操作,给出m个扑克,然后给出n个操作的个数a[i],每个a[i]代表可以翻的扑克的个数,求最后可能出现的扑克的组合情况. Hint Sample Input: 3 3 3 2 3 For the this example: 0 express face down,1 express face up Initial state 000 The first result:000->111->001->110 The second result:0

HDU 4869 Turn the pokers

Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. She went out and bought m pokers, tending to play p

hdu 4869 Turn the pokers(数学)

题目链接:hdu 4869 Turn the pokers 题目大意:给定n和m,表示有n次翻牌的机会,m张牌,一开始所有的牌均背面朝上,每次翻牌可以选择xi张牌同时翻转.问说最后有多少种能. 解题思路:只要确定最后正面朝上的牌的个数即可.所以在读入xi的时候维护上下限即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long l

TURN Server Windows 安装程序

有了OfficeSIP TURN Server 安装包,记录一下. http://www.onlinedown.net/soft/94746.htm 开源代码(C#)和应用地址:https://sourceforge.net/projects/turnservernet/ svn源码下载命令: svn checkout https://svn.code.sf.net/p/turnservernet/code/trunk turnservernet-code