SCU-4445 Right turn

探讨一个迷宫逃脱问题,一个人遵循右转法则,在无限大的迷宫中寻找出路,遇到障碍物则转向,目标是计算其成功逃脱所需的转弯次数,若无法逃脱则输出特殊值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题目大意

一个迷宫有n块石头,一个人从(0,0)出发,一开始是面向右边,如果他遇到石头就要向右转,问这个人走出迷宫要转几次弯,如果会无限次转弯就输出-1

思路

直接模拟,遇到障碍就标记一下此时到这个障碍的方向,然后右转,如果以后又以同样的方向碰到这个障碍,就直接跳出循环。否则肯定可以出去。

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<math.h>
#include<bitset>
#include<stdlib.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const double pi=acos(-1.0);
const int N=1e5+10;
struct note
{
    int x,y;
} node[1010];
map<int,map<int,map<int,int> > >xiao;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        xiao.clear();
        for(int i=0; i<n; i++)
            scanf("%d%d",&node[i].x,&node[i].y);
        int mov=1;
        int now_x=0,now_y=0;
        int flag=0;
        int cnt=0;
        while(1)
        {
            int xx=inf;
            int yy=-inf;
            int xx2=-inf;
            int yy2=inf;
            if(mov==1)
            {
                for(int i=0; i<n; i++)
                {
                    if(node[i].y==now_y&&node[i].x>now_x)
                    {
                        if(node[i].x<xx)
                            xx=node[i].x;
                    }
                }
                if(xx==inf)
                {
                    flag=1;
                    break;
                }
                else
                {
                    cnt++;
                    mov=2;
                    now_x=xx-1;
                    if(xiao[xx][now_y][1]==1)
                        break;
                    xiao[xx][now_y][1]=1;
                }
            }
            else if(mov==2)
            {
                for(int i=0; i<n; i++)
                {
                    if(now_x==node[i].x&&now_y>node[i].y)
                    {
                        if(node[i].y>yy)
                            yy=node[i].y;
                    }
                }
                if(yy==-inf)
                {
                    flag=1;
                    break;
                }
                else
                {
                    cnt++;
                    mov=3;
                    now_y=yy+1;
                    if(xiao[now_x][yy][2]==1)
                        break;
                    xiao[now_x][yy][2]=1;
                }
            }
            else if(mov==3)
            {
                for(int i=0; i<n; i++)
                {
                    if(node[i].y==now_y&&now_x>node[i].x)
                    {
                        if(node[i].x>xx2)
                            xx2=node[i].x;
                    }
                }
                if(xx2==-inf)
                {

                    flag=1;
                    break;
                }
                else
                {
                    cnt++;
                    mov=4;
                    now_x=xx2+1;
                    if(xiao[xx2][now_y][3]==1)
                        break;
                    xiao[xx2][now_y][3]=1;
                }
            }
            else
            {
                for(int i=0; i<n; i++)
                {
                    if(node[i].x==now_x&&now_y<node[i].y)
                    {
                        if(node[i].y<yy2)
                            yy2=node[i].y;
                    }
                }
                if(yy2==inf)
                {
                    flag=1;
                    break;
                }
                else
                {
                    cnt++;
                    mov=1;
                    now_y=yy2-1;
                    if(xiao[now_x][yy2][4]==1)
                        break;
                    xiao[now_x][yy2][4]=1;
                }
            }
        }
        if(flag)
            printf("%d\n",cnt);
        else
            printf("-1\n");
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值