hdu 1241 AC广搜
2015-08-10 17:20
281 查看
点击打开链接,原地址
package cn.hncu.search; import java.util.Scanner; public class searchbfs { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int count=0; while(sc.hasNext()){ int m=sc.nextInt(); int n=sc.nextInt(); Plot plots[][]=new Plot[m] ; for(int i=0;i<m;i++){ String str =sc.next(); for(int j=0;j<n;j++){ plots[i][j]=new Plot(i,j); plots[i][j].c=str.charAt(j);//以上都是接受字符串,以及收数字 } } for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(plots[i][j].c=='@'&&!plots[i][j].flag){//开始进行判断,是否满足条件以及是否访问过 PlotQueue queue=new PlotQueue(); queue.add(plots[i][j]);//满足条件,进队列 bfs(plots,queue);//开始广搜 count++;//搜索完成后就直接加一个 } } } System.out.println(count); } } private static void bfs(Plot[][] plots, PlotQueue queue) { while(!queue.isEmpty()){//判断是否为空, Plot plot=queue.pop();//位于一个的出队列 int px,py; int m=plots.length;//为后面的要求 y值 int n=plots[0].length;//x 的最大值 for(int i=0;i<8;i++){ px=plot.x+a[i][0]; py=plot.y+a[i][1]; if(px>=0&&px<m && py>=0&&py<n &&plots[px][py].c=='@' &&! plots[px][py].flag){ plots[px][py].flag=true;//访问过之后就改变标签 queue.add(plots[px][py]);//满足条件就直接加 } } } } final static int a[][]={//该点的四周的八个点必须一一开始遍历 {0,-1}, {0,1}, {-1,0}, {1,0}, {-1,-1}, {-1,1}, {1,-1}, {1,1} }; } //点的作用, class Plot{ int x,y; char c; boolean flag=false; public Plot(int x, int y) { this.x = x; this.y = y; } } //广搜必须要到队列,就像深搜用到地 递归一样 class PlotQueue{ Plot plot[]; public PlotQueue(){ plot=new Plot[100]; } final int FRONT=0; int end=0; public void add(Plot p) {//进站 plot[end++]=p; } public Plot pop() {//出栈,第一个先出去,其余的往前移动 if(end<=0){ return null; } Plot p=plot[FRONT]; if(end>1){ for(int i=0;i<end;i++){ plot[i]=plot[i+1]; } }end--; return p; } public boolean isEmpty() {//判断是都为空 if(end<=0){ return true; } return false; } }
相关文章推荐
- 搜索(HDU 1026)
- 八数码(hdu 1043)
- hdu1241Oil Deposits 深广搜
- poj1753 Flip Game 广搜哈希
- zoj 1148 The Game 一个晚上终于AC!
- Dungeon Master (广搜,bfs)
- 迷宫问题(广搜 bfs)
- hdoj 2612 Find a way简单广搜
- LeetCode之Word Ladder
- HDU2612,简单广搜题
- FZU2150,暴力广搜
- Gym100187E,Two Labyrinths,广搜
- 广搜状态压缩 OpenJudge 百炼4105
- Save__TangMonk之状态压缩
- Prime Path(广搜)
- hdu 1072
- hdu 1241
- ACM之广搜
- 1151 魔板
- hdu 1195 Open the Lock