广度优先搜索_suuccess
来源:互联网 发布:windos系统优化设置 编辑:程序博客网 时间:2024/05/17 08:13
package bfspac;
class Queue{
private final int SIZE = 10000;
private int[] queArrayx;
private int[] queArrayy;
private int[] label;
private int frontx; //队头
private int rearx; //队尾
private int fronty; //队头
private int reary; //队尾
private int frontlabel; //队头
private int rearlabel; //队尾
public Queue()
{
queArrayx = new int[SIZE];
queArrayy = new int[SIZE];
label=new int[SIZE];
frontx = 0 ;
fronty = 0 ;
rearx = -1;
reary = -1;
frontlabel=0; //队头
rearlabel=-1; //队尾
}
public void insert(int x,int y,int flag)
{
if(rearx == SIZE -1)
rearx = -1;
if(reary == SIZE -1)
reary = -1;
if(rearlabel == SIZE -1)
rearlabel = -1;
queArrayx[++rearx] =x;
queArrayy[++reary] =y;
label[++rearlabel]=flag;
}
public int removex()
{
int temp = queArrayx[frontx++];
if(frontx == SIZE)
frontx = 0;
return temp;
}
public int removey()
{
int temp = queArrayy[fronty++];
if(fronty == SIZE)
fronty = 0;
return temp;
}
public int removelabel()
{
int temp = label[frontlabel++];
if(frontlabel == SIZE)
frontlabel = 0;
return temp;
}
public boolean isEmpty()
{
return (rearx+1 == frontx || (frontx+SIZE-1 == rearx));
}
}
public class V_BFS {
public static int[][] map={{1,1,0,0},{0,1,0,0},{0,1,1,0},{0,0,1,0}};
public static int[] direx={-1,0,1,0};
public static int[] direy={0,1,0,-1};
public static void BFS(int x,int y){
map[x][y]=2;
Queue myQueue=new Queue();
myQueue.insert(x,y,2);
while(!myQueue.isEmpty()){
int curx=myQueue.removex();
int cury=myQueue.removey();
int temp=myQueue.removelabel()+1;
for(int i=0;i<4;i++){
int newx=curx+direx[i];
int newy=cury+direy[i];
if(newx>=0&&newx<4&&newy>=0&&newy<4&&map[newx][newy]==1){
map[newx][newy]=temp;
myQueue.insert(newx, newy,temp);
}
}
}
}
public static void main(String[] args){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(map[i][j]+" ");
}
System.out.println(" ");
}
System.out.println(" ");
BFS(0,0);
System.out.println(" ");
for(int k=0;k<4;k++){
for(int m=0;m<4;m++){
System.out.print(map[k][m]+" ");
}
System.out.println(" ");
}
System.out.println(" ");
}
}
- 广度优先搜索_suuccess
- 广度优先搜索算法
- 双向广度优先搜索
- 广度优先搜索法
- pku2251(广度优先搜索)
- POJ1184Clerver_Writer:广度优先搜索
- Java广度优先搜索
- [AI]广度优先搜索
- 广度优先搜索-BFS
- 广度优先搜索算法
- 广度优先搜索算法
- BFS广度优先搜索
- 广度优先搜索 BFS
- 广度优先搜索
- [ 算法 ]广度优先搜索!
- 广度优先搜索
- 广度优先搜索算法
- 广度优先搜索算法
- 广度优先_病毒感染
- 进程描述和控制 --操作系统精髓与设计
- [Mac] 整理mac下好用的app和终端工具
- git第六讲_git的分支命令详解
- 【Java每日一题】20161209
- 广度优先搜索_suuccess
- spring mvc 下载文件 IE浏览器文件名是乱码
- 考试1
- 使用Appium测试Hybrid App的方法
- linux 文档的压缩和打包
- 考试2
- SVN和GIT的比较(一)
- Spring ,Springmvc,mybatis三大框架整合的配置文件支持
- Matlab 球坐标-直角坐标转换sph2cart