【算法题】机器人走迷宫

机器人走迷宫

力扣62. 不同路径
在这里插入图片描述

package dynamicProgramming;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static java.lang.Integer.parseInt;
import static java.lang.System.in;

public class RobotMaze {
    public static void main(String[] args) throws IOException {
        // 构造数据源
        BufferedReader bf = new BufferedReader(new InputStreamReader(in));
        int x = parseInt(bf.readLine());
        int y = parseInt(bf.readLine());
        bf.close();

        int[][] map = new int[x][y];

        // 第一行赋值为1
        for (int i = 0; i < map[0].length; i++) {
            map[0][i] = 1;
        }

        // 第一列赋值为1
        for (int i = 0; i < map.length; i++) {
            map[i][0] = 1;
        }

        // 动态规划
        for (int i = 1; i < map.length; i++) {
            for (int j = 1; j < map[i].length; j++) {
                map[i][j] = map[i - 1][j] + map[i][j - 1];
            }
        }

        // 输出结果
        System.out.println(map[x - 1][y - 1]);
    }
}

统计的时多少种方法,而不是一共走了多少步,不要被最后一步给干扰了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言实现机器人走迷宫搜索算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_ROW 5 #define MAX_COL 5 struct point { int row, col; } queue[512]; int head = 0, tail = 0; void enqueue(struct point p) { queue[tail++] = p; } struct point dequeue(void) { return queue[head++]; } bool is_empty(void) { return head == tail; } bool maze[MAX_ROW][MAX_COL] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; void print_maze(void) { for (int i = 0; i < MAX_ROW; i++) { for (int j = 0; j < MAX_COL; j++) { printf("%d ", maze[i][j]); } putchar('\n'); } printf("*********\n"); } void visit(int row, int col) { struct point visit_point = { row, col }; maze[row][col] = 2; enqueue(visit_point); } int main(void) { struct point p = { 0, 0 }; maze[p.row][p.col] = 2; enqueue(p); while (!is_empty()) { struct point current = dequeue(); if (current.row == MAX_ROW - 1 && current.col == MAX_COL - 1) { printf("Success!\n"); return 0; } if (current.col+1 < MAX_COL && maze[current.row][current.col+1] == 0) { visit(current.row, current.col+1); } if (current.row+1 < MAX_ROW && maze[current.row+1][current.col] == 0) { visit(current.row+1, current.col); } if (current.col-1 >= 0 && maze[current.row][current.col-1] == 0) { visit(current.row, current.col-1); } if (current.row-1 >= 0 && maze[current.row-1][current.col] == 0) { visit(current.row-1, current.col); } print_maze(); } printf("Failed!\n"); return EXIT_FAILURE; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值