• poj 3009


    指定题目版本:

    问题描述

    On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.

    Fig. 1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.


    Fig. 1: Example of board (S: start, G: goal)

    The movement of the stone obeys the following rules:

    • At the beginning, the stone stands still at the start square.
    • The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited.
    • When the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. 2(a)).
    • Once thrown, the stone keeps moving to the same direction until one of the following occurs:
      • The stone hits a block (Fig. 2(b), (c)).
        • The stone stops at the square next to the block it hit.
        • The block disappears.
      • The stone gets out of the board.
        • The game ends in failure.
      • The stone reaches the goal square.
        • The stone stops there and the game ends in success.
    • You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.


    Fig. 2: Stone movements

    Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.

    With the initial configuration shown in Fig. 1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. 3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. 3(b).


    Fig. 3: The solution for Fig. D-1 and the final board configuration

    输入

    The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.

    Each dataset is formatted as follows.

    the width(=w) and the height(=h) of the board
    First row of the board
    ... 
    h-th row of the board

    The width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20.

    Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.

    0 vacant square
    1 block
    2 start position
    3 goal position

    The dataset for Fig. D-1 is as follows:

    6 6 
    1 0 0 2 1 0 
    1 1 0 0 0 0 
    0 0 0 0 0 3 
    0 0 0 0 0 0 
    1 0 0 0 0 1 
    0 1 1 1 1 1

    输出

    For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.

    样例输入

    2 1
    3 2
    6 6
    1 0 0 2 1 0
    1 1 0 0 0 0
    0 0 0 0 0 3
    0 0 0 0 0 0
    1 0 0 0 0 1
    0 1 1 1 1 1
    6 1
    1 1 2 1 1 3
    6 1
    1 0 2 1 1 3
    12 1
    2 0 1 1 1 1 1 1 1 1 1 3
    13 1
    2 0 1 1 1 1 1 1 1 1 1 1 3
    0 0

    样例输出

    1
    4
    -1
    4
    10
    -1

     题意: 一个只包含0 1 2 3 的图  0 表示这个点是空地 1 表示障碍  2  起点  3 终点   求出能否从2到3 如果能求出最少的步数

    需注意 球会沿着一个方向一直走到不能继续走为止(出界 或遇到障碍 或到达终点) 球在遇到障碍时会返回上一步并且障碍所在的位置下次可以走

    #include <cstdio>
    #include <queue>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int oo = 0x3f3f3f3f;
    const int N = 200;
    const int M = 6000;
    typedef long long LL;
    char str[N][N];
    int maps[555][555], n, m, T, sx, sy, ex, ey, vis[555][555], ans, step;
    int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    int ok(int x, int y)///判断是否越界
    {
        if(x >= 0 && x < m && y >= 0 && y < n) return 1;
        return 0;
    }
    void dfs(int x, int y, int step)
    {
        int nx, ny;
        if(step >= 10) return ; /// 不能超过10步
        for(int i = 0; i < 4; i++)
        {
            nx = x + dir[i][0];
            ny = y + dir[i][1];
            if(maps[nx][ny] == 1)continue;///如果下个位置是石头 不能走
            while(!maps[nx][ny] && ok(nx, ny))///可行的路一直走到不是0的地方为止
            {
                nx += dir[i][0];
                ny += dir[i][1];
            }
            if(ok(nx, ny))///坐标合法
            {
                if(maps[nx][ny] == 1)///如果当前位置是石头则返回上个位置
                {
                    maps[nx][ny] = 0;
                    dfs(nx-dir[i][0], ny-dir[i][1], step+1);
                    maps[nx][ny] = 1;
                }
                if(maps[nx][ny] == 3)///如果当前位置是终点求出最小的步数
                {
                    if(step + 1 < ans)
                        ans = step+1;
                }
            }
        }
    }
    int main()
    {
        int i, j;
        while(scanf("%d %d", &n, &m), n+m)
        {
            memset(vis, 0, sizeof(vis));
            ans = oo;
            for(i = 0; i < m; i++)
            {
                for(j = 0; j < n; j++)
                {
                    scanf("%d", &maps[i][j]);
                    if(maps[i][j] == 2)
                    {
                        sx = i;
                        sy = j;
                        maps[i][j] = 0;
                    }
                    if(maps[i][j] == 3)
                    {
                        ex = i;
                        ey = j;
                    }
                }
            }
            step = 0;
            dfs(sx, sy, 0);
            if(ans != oo) printf("%d
    ", ans);
            else printf("-1
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    ESP8266 A0的使用
    电脑总是被乱装各种软件怎么办?那就设置一个密码吧!
    笔记本光驱位改装固态系统硬盘教程
    任务管理器无法呼出
    Python stomp 介绍与代码
    Power(x,y)
    旋转图像
    字符串相乘
    缺失的正数
    外观数列
  • 原文地址:https://www.cnblogs.com/PersistFaith/p/4817679.html
Copyright © 2020-2023  润新知