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;
    }
  • 相关阅读:
    安德鲁斯称三步系统相机
    Swift游戏开发实战教程(霸内部信息大学)
    ORA-00911:无效字符错误
    Java对多线程~~~Fork/Join同步和异步帧
    jquery跨域请求解决方案(我们寻找,我还没有添加验证)
    mysql 的load data infile要使用
    完整详细的说明GCD列(一)dispatch_async;dispatch_sync;dispatch_async_f;dispatch_sync_f
    Linux查看非root流程执行
    Android_显示器本身被卸载应用程序
    关于微软公有云Azure会计标准
  • 原文地址:https://www.cnblogs.com/ACMessi/p/4852278.html
Copyright © 2011-2022 走看看