SCU4445——模拟——Right turn

Right turn

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
/*
   模拟题 机器人碰了就往右拐
   每次判断要与所有点判断并且是靠自己最近的点
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int inf = 0x3f3f3f3f;
int dx[] = { 1, 0, -1, 0};
int dy[] = { 0, -1, 0, 1};
int vis[1100][4];
int obx[1100], oby[1100];
int n;

void work()
{
    memset(vis, 0, sizeof(vis));
    int dir = 0;
    int nx , ny, dis;
    nx = ny = 0;
    for(int step = 0; ; step++){
        int mx = inf;
        dis = inf;
        int index = -1;
        for(int i = 1; i <= n ; i++){
            if(dir == 0 ){
                if(ny == oby[i] && nx < obx[i]){
                    dis = obx[i] - nx - 1;
                }
            }
            else if(dir == 1){
                if(nx == obx[i] && ny > oby[i]){
                //   printf("%d ",i);
                    dis = ny - oby[i] - 1;
                }
            }
            else if(dir == 2){
                if(ny == oby[i] && nx > obx[i]){
                    dis = nx - obx[i] - 1;
                }
            }
            else {
                if(nx == obx[i] && ny < oby[i]){
                    dis = oby[i] - ny - 1;
                }
            }
            if(dis < mx){
                mx = dis;
                index = i;
            }
        }
       // printf("%d %d %d\n",index, step,dir);

      if(index == -1) {
            printf("%d\n",step);
            return ;
        }
      if(vis[index][dir]){
          printf("-1\n");
          return ;
      }

        vis[index][dir] = 1;
            if(dir == 0){
                nx = obx[index] - 1; ny = oby[index];
            }
            else if(dir == 1){
                nx = obx[index]; ny = oby[index] + 1;
            }
            else if(dir == 2){
                nx = obx[index] + 1; ny = oby[index];
            }
            else {
                nx = obx[index]; ny = oby[index] - 1;
            }
            //  printf("%d %d %d \n",nx,ny, dir);
        dir = (dir + 1) % 4;
    }
}

int main()
{
    while(~scanf("%d", &n)){
        for(int i = 1; i <= n; i++){
            scanf("%d%d", &obx[i], &oby[i]);
        }
        work();
    }
    return 0;
}

  

时间: 2024-10-12 23:21:55

SCU4445——模拟——Right turn的相关文章

URAL 1787 Turn for MEGA (贪心 + 模拟)

Turn for MEGA Time limit: 1.0 second Memory limit: 64 MB A traffic light at the turn for the "MEGA" shopping center from the Novomoskovskiy highway works in such a way that k cars are able to take a turn in one minute. At weekends all the reside

scu oj 4445 Right turn 2015年四川省赛J题(模拟题)

一般的模拟题.对于出现过的每一个x建立一个set 集合,对于y也如此.然后查找要走到哪个点即可,主要要对状态记录,判断是否无线循环,否者出现无线循环的情况,就tle了. #include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<queue> #include<cmath> #include<map> #include&

poj 1696 Space Ant(模拟+叉积)

Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3840   Accepted: 2397 Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet Y19

UVA11988 Broken Keyboard (a.k.a. Beiju Text)【数组模拟链表】

Broken Keyboard (a.k.a. Beiju Text) You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "home" key or the "end" key gets automatically pressed (inter

POJ 3344 &amp; HDU 2414 Chessboard Dance(模拟)

题目链接: PKU:http://poj.org/problem?id=3344 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2414 Description Another boring Friday afternoon, Betty the Beetle thinks how to amuse herself. She goes out of her hiding place to take a walk around the living r

HDU 4121 Xiangqi 模拟

原题: http://acm.hdu.edu.cn/showproblem.php? pid=4121 题目: Xiangqi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4809 Accepted Submission(s): 1134 Problem Description Xiangqi is one of the most pop

【模拟+递归+位运算】POJ1753-Flip Game

由于数据规模不大,利用爆搜即可.第一次用位运算写的,但是转念一想应该用递归更加快,因为位运算没有剪枝啊(qДq ) [思路] 位运算:时间效率较低(172MS),有些辜负了位运算的初衷.首先将二维数组倒序看作一个二进制数num.我们假设1代表翻转,0代表不翻转,可以发现以下规律:0 xor 1=1,1 xor 1=0;0 xor 0=0,1 xor 0=1,恰巧满足异或运算.我们假设另一个二进制数i∈[0,2^16),通过异或运算就可以模拟出所有清形. 用check和i进行&操作可以求出以哪些位

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

webrtc学习: 部署stun和turn服务器

webrtc的P2P穿透部分是由libjingle实现的. 步骤顺序大概是这样的: 1. 尝试直连. 2. 通过stun服务器进行穿透 3. 无法穿透则通过turn服务器中转. stun 服务器比较简单. 网上也有很多公开的stun服务器可以用于测试. 例如 stun.ideasip.com 这里需要注意一下. 我在做android应用时. 在少数老旧的手机上出现过一个bug: PeerConnection close时非常慢. 大概需要50~80s. 后来反复检查, 才发现问题出在公用的stu