zoukankan      html  css  js  c++  java
  • 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
    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)的 比当前位置y(or x)  大(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
    ",turn);
        }
        return 0;
    }
  • 相关阅读:
    C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素
    C#对IQueryable<T>、IEnumerable<T>的扩展方法
    C#常用处理数据类型转换、数据源转换、数制转换、编码转换相关的扩展
    C#常用8种排序算法实现以及原理简介
    身份证号合法性验证,支持15位和18位身份证号,支持地址编码、出生日期、校验位验证
    C#检验参数合法性公用方法
    MVC中得到成员元数据的Description特性描述信息公用方法
    把对象类型转换成指定的类型帮助类方法
    C#操作图片帮助类
    C#执行Dos命令公用方法
  • 原文地址:https://www.cnblogs.com/ACMessi/p/4852278.html
Copyright © 2011-2022 走看看