zoukankan      html  css  js  c++  java
  • 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 (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

    /*
       模拟题 机器人碰了就往右拐
       每次判断要与所有点判断并且是靠自己最近的点
    */
    #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
    ",index, step,dir);
        
          if(index == -1) {
                printf("%d
    ",step);
                return ;
            }
          if(vis[index][dir]){
              printf("-1
    ");
              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 
    ",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;
    }
    

      

  • 相关阅读:
    客户端请求页面的方式和提交数据的方式
    客户端请求页面的方式
    request session application cookie 保存数据
    jsp session(会话) 的使用 cookies,application 理解
    jsp内置对象
    jsp 实现数据传递
    最近戴着眼镜坐电脑前总是不自觉的眼痛就搜了下怎么保护眼睛无意中看到了这篇文章希望广大爱好编程的朋友多注意保护自己的眼睛!!
    选择排序(使用Python描述)
    归并排序(使用Python描述)
    二分查找的两种方法(使用Python描述)
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4647005.html
Copyright © 2011-2022 走看看