美文网首页
小青蛙迷宫

小青蛙迷宫

作者: Bookish倩宝 | 来源:发表于2016-09-19 09:07 被阅读0次

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int p = in.nextInt();
Main main = new Main(n, m, p);

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            main.maze[i][j] = in.nextInt();
        }
    }

    main.seek();

    if (main.res == null) {
        System.out.println("Can not escape!");
    } else {
        StringBuilder builder = new StringBuilder();
        for (Path path : main.res) {
            builder.append("[" + path.x + "," + path.y + "],");
        }
        String s = builder.toString();
        System.out.println(s.substring(0, s.length() - 1));
    }
}

public Main(int n, int m, int p) {
    this.n = n;
    this.m = m;
    this.p = p;
    maze = new int[n][m];
    mark = new boolean[n][m];
    minCost = p + 1;
}

static int[][] MOVE = new int[][] {
    new int[]{-1, 0, 3},
    new int[]{1, 0, 0},
    new int[]{0, -1, 1},
    new int[]{0, 1, 1}
};

int n, m, p;
int[][] maze;
boolean[][] mark;
int minCost;

List<Path> cur = new ArrayList<Path>();
List<Path> res = null;

public void seek() {
    if (cur.size() == 0) {
        cur.add(new Path(0, 0, 0));
        mark[0][0] = true;
    }
    Path curPath = cur.get(cur.size() - 1);
    if (curPath.x == 0 && curPath.y == m - 1) {
        int cost = 0;
        for (Path p : cur) {
            cost += p.cost;
        }
        if (cost < minCost) {
            res = new ArrayList<Path>(cur);
        }
    }
    for (int i = 0; i < MOVE.length; i++) {
        Path next = new Path(curPath.x + MOVE[i][0], curPath.y + MOVE[i][1], MOVE[i][2]);
        if (next.x >= 0 && next.x < n && next.y >= 0 && next.y < m
                && !mark[next.x][next.y] && maze[next.x][next.y] == 1) {
            cur.add(next);
            mark[next.x][next.y] = true;
            seek();
            cur.remove(cur.size() - 1);
            mark[next.x][next.y] = false;
        }
    }
}

}

class Path {
int x;
int y;
int cost;

public Path(int x, int y, int cost) {
    this.x = x;
    this.y = y;
    this.cost = cost;
}

@Override
public String toString() {
    return "Path{" +
            "x=" + x +
            ", y=" + y +
            ", cost=" + cost +
            '}';
}

}

相关文章

  • 小青蛙迷宫

    import java.util.ArrayList;import java.util.List;import j...

  • 《小青蛙🐸找家》——助人为乐

    小青蛙走路——蹦蹦跳跳 小青蛙唱歌——呱呱呱! 小青蛙吃什么?——害虫 小青蛙是什么?——庄稼的好朋友! 小青蛙今...

  • 2019-03-16

    乐高积木搭迷宫 孩子爸用乐高搭了基础小迷宫,晶晶没有把注意力放在怎么走迷宫上,而是摆放迷宫玩具上。跳跃走迷宫,没有...

  • 青蛙写诗

    青蛙写诗 可爱的小青蛙, 呱呱叫着可爱。 小青蛙,我爱你。 ...

  • HDU 1272 - 小希的迷宫

    HDU 1272 - 小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一...

  • 2018-11-27 并查集

    并查集:合并、查找、集合 问题:小希的迷宫: Description 上次Gardon的迷宫城堡小希玩了很久(见P...

  • 2019-02-27

    数学学了钟表,时针和分针,语文学了小青蛙,还让我们写了小青蛙一课的生字,小青蛙吃虫子,我们要保护小青蛙

  • 【狐狸爸爸故事屋】迷宫历险记

    狐狸姐姐最近迷上了迷宫游戏。 这天,狐狸姐姐和小熊、小猴,准备自己组建一个迷宫。 他们来到草地上,开始制作迷宫。 ...

  • 我用40张成长币换来的礼物

    ​我今天在学校里面,中午休息的时候,我用了40张成长币,换了一套小迷宫,那个小迷宫里面有一个铁球,铁球还能在迷宫里...

  • 小青蛙

    今天戴老师给我们讲了一篇课文《小青蛙》,它不仅是人类的好朋友,更是庄稼的保护神。更让我想到了我们一定要爱护小青蛙。...

网友评论

      本文标题:小青蛙迷宫

      本文链接:https://www.haomeiwen.com/subject/xbkgettx.html