zoukankan      html  css  js  c++  java
  • HDU1241(bfs)JAVA

    import java.util.Scanner;
    public class Main1241 {
    public static void main(String[] args) {
    Scanner cin=new Scanner(System.in);
    while(cin.hasNext()){
    int n=cin.nextInt();
    int m=cin.nextInt();
    if(n==0){
    break;
    }
    plot [][]plots=new plot[n][m];
    for(int i=0;i<n;i++){
    String str=cin.next();
    for(int j=0;j<m;j++){
    plots[i][j]=new plot();
    plots[i][j].x=i;
    plots[i][j].y=j;
    plots[i][j].c=str.charAt(j);

    }
    }
    int count=0;
    //print(plots);
    for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
    if(plots[i][j].c=='@'&&!plots[i][j].isvisable){
    Plotqueue queue=new Plotqueue();
    queue.in(plots[i][j]);
    bfs(plots,queue);
    count++;
    }

    }
    }
    System.out.println(count);

    }
    }
    final static int [][]dir={{-1,0},{0,-1},{-1,-1},{0,1},{1,0},{1,1},{1,-1},{-1,1}};
    private static void bfs(plot[][] plots, Plotqueue queue) {
    int px;
    int py;
    int n=plots.length;
    int m=plots[0].length;
    while(!queue.isempty()){
    plot Plots=queue.out();
    for(int i=0;i<8;i++){
    px=Plots.x+dir[i][0];
    py=Plots.y+dir[i][1];
    if(px>=0&&px<n&&py>=0&&py<m&&plots[px][py].c=='@'&&!plots[px][py].isvisable){
    plots[px][py].isvisable=true;
    queue.in(plots[px][py]);

    }
    }

    }
    }


    private static void print(plot[][] plots) {
    for(int i=0;i<plots.length;i++){
    for(int j=0;j<plots[i].length;j++){
    System.out.print(plots[i][j].c);
    }
    System.out.println();
    }
    }
    }

    class plot{
    int x;
    int y;
    char c;
    boolean isvisable=false;

    }
    class Plotqueue{
    plot []Plot;
    int end;
    final int FRIST=0;
    public Plotqueue() {
    Plot=new plot[100];

    }
    public void in(plot p){
    Plot[end]=p;
    end++;
    }
    public plot out(){
    plot p;
    if(end<=0){
    return null;
    }
    else{
    p=Plot[FRIST];
    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;

    }

    }




  • 相关阅读:
    crontab与系统时间不一致
    MySQL构造测试数据
    【SQL优化】SQL优化工具
    mysql case when then 使用
    update没带where,寻找问题的思路
    线程池
    线程理论
    数据共享
    进程池
    管道
  • 原文地址:https://www.cnblogs.com/1314wamm/p/5638276.html
Copyright © 2011-2022 走看看