唐山市城乡建设网站,农村电商平台简介,网站的基本价格,做网站设计描述 定义一个二维数组 N*M #xff0c;如 5 5 数组下所示#xff1a; int maze[5][5] { 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫#xff0c;其中的1表示墙壁#xff0c;0表示可以走的路#xff0c;只能横着走或… 描述 定义一个二维数组 N*M 如 5 × 5 数组下所示 int maze[5][5] { 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫其中的1表示墙壁0表示可以走的路只能横着走或竖着走不能斜着走要求编程序找出从左上角到右下角的路线。入口点为[0,0],既第一格是可以走的路。 数据范围 2≤n,m≤10 输入的内容只包含 0≤val≤1 输入描述 输入两个整数分别表示二维数组的行数列数。再输入相应的数组其中的1表示墙壁0表示可以走的路。数据保证有唯一解,不考虑有多解的情况即迷宫只有一条通道。 输出描述 左上角到右下角的最短路径格式如样例所示。 示例1 输入 5 5
0 1 0 0 0
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0输出 (0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)
(4,4) 示例2 输入 5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 1
0 1 1 1 0
0 0 0 0 0输出 (0,0)
(1,0)
(2,0)
(3,0)
(4,0)
(4,1)
(4,2)
(4,3)
(4,4) 说明 注意不能斜着走 import java.util.*;// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextInt()) { // 注意 while 处理多个 caseint a in.nextInt();int b in.nextInt();int[][] array new int[a][b];for (int i0; ia; i) {for (int j0; jb; j) {array[i][j] in.nextInt();}}getRes1(array);}}public static void getRes1(int[][] array) {int dS array.length;int rS array[0].length;int[][] res new int[dS][rS];Obj [][] objs new Obj[dS][rS];Obj start Obj.build(0, 0);start.before null;objs[0][0] start;// 消息队列QueueObj queue new LinkedList();queue.add(start);int[] chooses {0, 1, 2, 3};while (!queue.isEmpty()) {Obj obj queue.poll();// 已经存在数据的元素不允许被覆盖if (objs[obj.d][obj.r] null) {objs[obj.d][obj.r] obj.before;}if (obj.equals(dS-1, rS-1)) {break;}for (int i0; ichooses.length; i) {switch(i) {case 0:if (obj.r1rS array[obj.d][obj.r1]0) {Obj tmp Obj.build(obj.d, obj.r1);tmp.before obj;queue.add(tmp);}break;case 1:if (obj.d1dS array[obj.d1][obj.r]0) {Obj tmp Obj.build(obj.d1, obj.r);tmp.before obj;queue.add(tmp);}break;case 2:if (obj.r-10 array[obj.d][obj.r-1]0) {Obj tmp Obj.build(obj.d, obj.r-1);tmp.before obj;queue.add(tmp);}break;case 3:if (obj.d-10 array[obj.d-1][obj.r]0) {Obj tmp Obj.build(obj.d-1, obj.r);tmp.before obj;queue.add(tmp);}break;}}}String s ;if (objs[dS-1][rS-1] ! null) {int idS-1, jrS-1;s (i,j)\n;boolean bool true;while (bool) {int dobjs[i][j].d;int robjs[i][j].r;s (d,r)\n s;if ((d0 r0)) {bool false;}else {i d;j r;}}// s (0,0)\ns;System.out.println(s);}}public static class Obj {int d;int r;Obj before;public static Obj build(int d, int r) {Obj o new Obj();o.d d;o.r r;return o;}public boolean equals(int d, int r) {return this.dd this.rr;}}
} // 已经存在数据的元素不允许被覆盖顺序应该是由下往上 if (objs[obj.d][obj.r] null) { objs[obj.d][obj.r] obj.before; } 文章转载自: http://www.morning.pflry.cn.gov.cn.pflry.cn http://www.morning.qrqcr.cn.gov.cn.qrqcr.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.qsy40.cn.gov.cn.qsy40.cn http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.hlzpb.cn.gov.cn.hlzpb.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.fbfnk.cn.gov.cn.fbfnk.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.dhxnr.cn.gov.cn.dhxnr.cn http://www.morning.thmlt.cn.gov.cn.thmlt.cn http://www.morning.lsgsn.cn.gov.cn.lsgsn.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.ysbrz.cn.gov.cn.ysbrz.cn http://www.morning.fwllb.cn.gov.cn.fwllb.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.zdmrf.cn.gov.cn.zdmrf.cn http://www.morning.wpqcj.cn.gov.cn.wpqcj.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.mhmdx.cn.gov.cn.mhmdx.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.mhlsx.cn.gov.cn.mhlsx.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.kkjlz.cn.gov.cn.kkjlz.cn http://www.morning.junyaod.com.gov.cn.junyaod.com http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.gnyhc.cn.gov.cn.gnyhc.cn http://www.morning.bloao.com.gov.cn.bloao.com http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn http://www.morning.cfnht.cn.gov.cn.cfnht.cn http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.ydfr.cn.gov.cn.ydfr.cn http://www.morning.rsxw.cn.gov.cn.rsxw.cn http://www.morning.gthc.cn.gov.cn.gthc.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.jfcbz.cn.gov.cn.jfcbz.cn http://www.morning.rmpfh.cn.gov.cn.rmpfh.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.gjmll.cn.gov.cn.gjmll.cn http://www.morning.fhtmp.cn.gov.cn.fhtmp.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.ypbdr.cn.gov.cn.ypbdr.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.zrkws.cn.gov.cn.zrkws.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.gthwz.cn.gov.cn.gthwz.cn http://www.morning.rmdwp.cn.gov.cn.rmdwp.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.nwqyq.cn.gov.cn.nwqyq.cn http://www.morning.jxjrm.cn.gov.cn.jxjrm.cn http://www.morning.qbdqc.cn.gov.cn.qbdqc.cn http://www.morning.trrpb.cn.gov.cn.trrpb.cn http://www.morning.ckwrn.cn.gov.cn.ckwrn.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.snnwx.cn.gov.cn.snnwx.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn