http://poj.org/problem?id=3984 迷宫问题
来源:互联网 发布:协同过滤推荐算法研究 编辑:程序博客网 时间:2024/06/01 17:02
//JAVA
import java.util.*;
public class Main{
static Scanner inScanner=new Scanner(System.in);
static int[][] num=new int[5][5];
static Stack<Point> stack;
static void bfs(){
while(!stack.isEmpty()){
Point point=stack.pop();
int x=point.x;
int y=point.y;
if(point.x+1<=4&&num[point.x+1][point.y]==0){
point.x++;
}else if(point.y+1<=4&&num[point.x][point.y+1]==0){
point.y++;
}else if(point.x-1>=0&&num[point.x-1][point.y]==0){
point.x--;
}else if(point.y-1>=0&&num[point.x][point.y-1]==0){
point.y--;
}
if(num[point.x][point.y]==2) continue;
num[point.x][point.y]=2;
stack.push(new Point(x, y));
stack.push(point);
if(point.x==4&&point.y==4){
break;
}
}
for(int i=0;i<stack.size();i++)
System.out.println(stack.elementAt(i));
}
public static void main(String[] args){
while(inScanner.hasNext()){
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
num[i][j]=inScanner.nextInt();
stack=new Stack<Point>();
stack.push(new Point(0, 0));
bfs();
}
}
}
class Point{
int x=0;
int y=0;
public Point(int x,int y){
this.x=x;
this.y=y;
}
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
}
- http://poj.org/problem?id=3984 迷宫问题
- 棋盘问题&&http://poj.org/problem?id=1321
- http://poj.org/problem?id=2965
- http://poj.org/problem?id=2075
- http://poj.org/problem?id=1125
- http://poj.org/problem?id=2983
- http://poj.org/problem?id=2406
- http://poj.org/problem?id=1961
- http://poj.org/problem?id=1486
- http://poj.org/problem?id=1062
- http://poj.org/problem?id=2983
- http://poj.org/problem?id=3159
- http://poj.org/problem?id=1201
- http://poj.org/problem?id=3259
- http://poj.org/problem?id=3233
- http://poj.org/problem?id=1273
- http://poj.org/problem?id=3281
- http://poj.org/problem?id=1007
- Hadoop HDFS文件操作的Java代码
- 2 数字签名
- 拷贝构造函数和赋值构造函数
- 默认参数和占位参数
- 朴素贝叶斯分类
- http://poj.org/problem?id=3984 迷宫问题
- 黑马程序员_Java基础_动态代理
- 【Thinking in Java笔记】Java中基本类型的大小、范围及其包装器类型
- java--局部代码块——构造代码块——静态代码块
- 1 UML介绍
- 支付宝手机网页支付即时到账接口
- ActiveMQ性能调优
- herbinate的基本属性总结
- 计算机整数乘积计算