文章标题
来源:互联网 发布:阿里云服务器创建svn 编辑:程序博客网 时间:2024/06/05 12:40
import java.util.*;
public class MIgongwenti {
public class Point{ int x; int y; private Point() { this(0,0); } private Point(int x,int y) { this.x=x; this.y=y; } public boolean equals(Point p) { return( x==p.x)&&(y==p.y); } public String toString() { return "("+x+","+y+")"; }}private int [][] maze=null;private Stack<Point>stack=new Stack<Point>();//保存路径的站 public MIgongwenti(int [][]maze) { this.maze=maze; } public void go() { Point out=new Point(maze.length-1,maze[0].length-1); Point in=new Point(0,0); Point curNode=in; Point nextNode=null; while(!curNode.equals(out)) { nextNode=new Point(curNode.x,curNode.y); if((curNode.x+1)<maze.length&&maze[curNode.x+1][curNode.y]==0) { nextNode.x++; } else if((curNode.y+1)<maze[0].length&&maze[curNode.x][curNode.y+1]==0) { nextNode.y++; } else if((curNode.x-1)>=0&&maze[curNode.x-1][curNode.y]==0) { nextNode.x--; } else if((curNode.y-1)>=0&&maze[curNode.x][curNode.y-1]==0) { nextNode.y--; } else { maze[curNode.x][curNode.y]=3; if(stack.isEmpty()) { System.out.println("No"); return; } curNode=stack.pop(); continue; } stack.push(curNode); maze[curNode.x][curNode.y]=2; curNode=nextNode; } if(nextNode.equals(out)) { stack.push(nextNode); maze[nextNode.x][nextNode.y]=2; } System.out.println("输出路径为:"); for(int i=0;i<stack.size();i++) { System.out.print(stack.elementAt(i)); } } public static void main(String[]args) { Scanner in=new Scanner(System.in); int [][]maze=new int [5][5]; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { maze[i][j]=in.nextInt(); } } new MIgongwenti(maze).go(); }
}
阅读全文
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- IO多路复用之poll函数详解
- Hibernate关系映射---单向多对一增删查改实例
- 数据丢失!手机数据覆盖是否还能找的回来
- Vue 引入假数据
- 移动端1px边框的实现
- 文章标题
- phalcon配置安装
- 使用Prometheus监控Kafka logsize(kafka-logsize-exporter)
- 存储类型与作用域
- Android_适配器(1)
- 查找某个日期之前的文件
- 欢迎使用CSDN-markdown编辑器
- [教程]网关日志查看方法
- Android APK反编译就这么简单 详解(附图)