首页 > 代码库 > Right turn(四川省第七届)

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 (0n103). 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个障碍,遇到障碍时只能右拐,没有障碍只能直走,问能否走出去,若能走出需要右拐几下,若不能输出-1,起点为(0,0)。
由此可见是一个dfs问题,四个单方向,当走上重复的道路时即进入死循环时无结果,剩下需要四个方向单独考虑。

技术分享

所以障碍前换方向,向右拐时x=node[pos].x;y=node[pos].y-1;;向下拐时,x=node[pos].x-1;y=node[pos].y;;向左拐时,x=node[pos].x;y=node[pos].y+1;;向上拐时x=node[pos].x=1;
y=node[pos].y;
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int inf=0x3f3f3f3f;
int dir[4][3]={{1,0},{0,-1},{-1,0},{0,1}};//控制方向,顺序不可以乱
int a[1008][4];
int mark,ans,n;
struct Node
{
    int x,y;
}node[1008];
void dfs(int cnt)
{
    if(mark) return;
    int dis=ans%4;
    for(int i=0;i<4;i++)
    {
        if(a[cnt][i]==dis)//死循环
        {
            mark=2;
            return;
        }
        if(a[cnt][i]==-1)
        {
            a[cnt][i]=dis;//更新节点
            break;
        }
    }
    int k=-1;
    if(dir[dis][1]==0)
    {
        if(dir[dis][0]==1)//1,0右拐
        {
            int x=node[cnt].x;
            int y=node[cnt].y-1;
            int xx=inf;
            for(int i=1;i<=n;i++)
            {
                if(node[i].x>x && node[i].x<xx && node[i].y==y)
                {
                    xx=node[i].x;
                    k=i;
                }
            }
            if(k==-1)
            {
                mark=1;
                return;
            }
            else
            {
                ans++;
                dfs(k);
            }
        }
        else//-1,0左拐
        {
            int x=node[cnt].x;
            int y=node[cnt].y+1;
            int xx=-inf;
            for(int i=1;i<=n;i++)
            {
                if(node[i].x<x && node[i].x>xx && node[i].y==y)
                {
                    xx=node[i].x;
                    k=i;
                }
            }
            if(k==-1)
            {
                mark=1;
                return;
            }
            else
            {
                ans++;
                dfs(k);
            }
        }
    }
    else
    {
        if(dir[dis][1]==-1)//0,-1下拐
        {
            int x=node[cnt].x-1;
            int y=node[cnt].y;
            int yy=-inf;
            for(int i=1;i<=n;i++)
            {
                if(node[i].y<y && node[i].y>yy && node[i].x==x)
                {
                    yy=node[i].y;
                    k=i;
                }
            }
            if(k==-1)
            {
                mark=1;
                return;
            }
            else
            {
                ans++;
                dfs(k);
            }
        }
        else//0,1上拐
        {
            int x=node[cnt].x+1;
            int y=node[cnt].y;
            int yy=inf;
            for(int i=1;i<=n;i++)
            {
                if(node[i].y>y && node[i].y<yy && node[i].x==x)
                {
                    yy=node[i].y;
                    k=i;
                }
            }
            if(k==-1)
            {
                mark=1;
                return;
            }
            else
            {
                ans++;
                dfs(k);
            }
        }
    }
    return;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        mark=0;
        ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&node[i].x,&node[i].y);//储存障碍点
        }
        memset(a,-1,sizeof(a));
        node[0].x=0;//初始化(0,1)
        node[0].y=1;
        dfs(0);
        if(mark==2) puts("-1");
        else printf("%d\n",ans);
    }
} 

 

Right turn(四川省第七届)