编程介的小学生 2020-01-12 14:47 采纳率: 0.4%
浏览 114

Robot Navigation 罗伯特的导航问题

Problem Description
A robot has been sent to explore a remote planet. To specify a path the robot should take, a program is sent each day. The program consists of a sequence of the following commands:
FORWARD X: move forward by X units.
TURN LEFT: turn left (in place) by 90 degrees.
TURN RIGHT: turn right (in place) by 90 degrees.
The robot also has sensor units which allow it to obtain a map of its surrounding area. The map is represented as a grid. Some grid points contain hazards (e.g. craters) and the program must avoid these points or risk losing the robot.
Naturally, if the initial location of the robot, the direction it is facing, and its destination position are known, it is best to send the shortest program (one consisting of the fewest commands) to move the robot to its destination (we do not care which direction it faces at the destination). You are more interested in knowing the number of different shortest programs that can move the robot to its destination. However, the number of shortest programs can be very large, so you are satisfied to compute the number as a remainder modulo 1,000,000.

Input
There will be several test cases in the input. Each test case will begin with a line with two integers
N M
Where N is the number of rows in the grid, and M is the number of columns in the grid (2 ≤ N, M ≤ 100). The next N lines of input will have M characters each. The characters will be one of the following:
‘.’ Indicating a navigable grid point.
‘*’ Indicating a crater (i.e. a non-navigable grid point).
‘X’ Indicating the target grid point. There will be exactly one ‘X’.
‘N’, ‘E’, ‘S’, or ‘W’ Indicating the starting point and initial heading of the robot. There will be exactly one of these. Note that the directions mirror compass directions on a map: N is North (toward the top of the grid), E is East (toward the right of the grid), S is South (toward the bottom of the grid) and W is West (toward the left of the grid).
There will be no spaces and no other characters in the description of the map. The input will end with a line with two 0s.

Output
For each test case, output two integers on a single line, with a single space between them. The first is the length of a shortest possible program to navigate the robot from its starting point to the target, and the second is the number of different programs of that length which will get the robot to the target (modulo 1,000,000). If there is no path from the robot to the target, output two zeros separated by a single space. Output no extra spaces, and do not separate answers with blank lines.

Sample Input
5 6
....X
.....

.....*
.....*
N....*
6 5
....X
.****
.****
.****
.****
N****
3 3
.E.


.X.
0 0

Sample Output
6 4
3 1
0 0

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-26 15:19
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # 读取数据
    input = readLines()
    
    # 定义函数判断是否是空格
    isSpace = function(x){
        return(!grepl("^$", x))
    }
    
    # 判断是否为数字
    isdigit = function(s){
        return(grepl("^[0-9]+$", s))
    }
    
    # 判断是否为字符
    isChar = function(s){
        return(!isSpace(s) && !isdigit(s))
    }
    
    # 判断是否为空字符串
    isEmpty = function(s){
        return(isChar(s) && !isSpace(s))
    }
    
    # 将输入转换为矩阵
    matrix = matrix(NA, nrow=as.integer(input[1]), ncol=as.integer(input[2]))
    
    # 遍历矩阵中的元素,并将非空格元素替换为相应的坐标和方向
    for(i in seq_along(matrix)){
        for(j in seq_along(matrix[i])){
            if(isChar(input[i+1][j])){
                matrix[i,j] <- c(matrix[i,j], input[i+1][j])
            }
        }
    }
    
    # 初始化变量
    start_x = NULL
    start_y = NULL
    target_x = NULL
    target_y = NULL
    robot_direction = "N"
    
    # 遍历矩阵,找出起点、目标点以及机器人初始方向
    for(i in seq_len(nrow(matrix))){
        for(j in seq_len(ncol(matrix[i]))){
            if(isChar(matrix[i,j]) && matrix[i,j] == "X"){
                target_x = i
                target_y = j
            }else if(isChar(matrix[i,j]) && matrix[i,j] == "N" || matrix[i,j] == "S" || matrix[i,j] == "E" || matrix[i,j] == "W"){
                start_x = i
                start_y = j
            }else{
                robot_direction = matrix[i,j]
            }
        }
    }
    
    # 根据机器人的初始方向和目标点计算路径长度和不同路径的数量
    if(robot_direction == "N"){
        robot_dir = 0
    }else if(robot_direction == "E"){
        robot_dir = 1
    }else if(robot_direction == "S"){
        robot_dir = 2
    }else{
        robot_dir = 3
    }
    path_length = 0
    paths_count = 0
    for(i in seq_len(target_y)){
        for(j in seq_len(start_x)){
            if(robot_direction == "N"){
                if(matrix[j,i] != "X"){
                    robot_dir += 1
                    path_length++
                }else{
                    break
                }
            }else if(robot_direction == "E"){
                if(matrix[j,i] != "X"){
                    robot_dir -= 1
                    path_length++
                }else{
                    break
                }
            }else if(robot_direction == "S"){
                if(matrix[j,i] != "X"){
                    robot_dir += 1
                    path_length++
                }else{
                    break
                }
            }else{
                if(matrix[j,i] != "X"){
                    robot_dir -= 1
                    path_length++
                }else{
                    break
                }
            }
        }
    }
    if(robot_direction == "N" & path_length > 0){
        paths_count = floor(path_length/3)
    } else if(robot_direction == "E" & path_length > 0){
        paths_count = floor(path_length/3)
    } else if(robot_direction == "S" & path_length > 0){
        paths_count = floor(path_length/3)
    } else if(robot_direction == "W" & path_length > 0){
        paths_count = floor(path_length/3)
    }
    
    # 输出结果
    print(paste(paths_count, " ", path_length %/% 3))```
    
    
    评论

报告相同问题?