不要让昨天 占据你的今天 夏午晴天

夏午晴天

BFS和DFS (java版)

  1 package com.algorithm.test;
  2 
  3 import java.util.ArrayDeque;
  4 import java.util.Scanner;
  5 
  6 public class DfsAndBfs {
  7     
  8     private static final int[][] dir = {
  9             {0,1},
 10             {0,-1},
 11             {1,-1},
 12             {1,0},
 13             {1,1},
 14             {-1,-1},
 15             {-1,0},
 16             {-1,1},
 17     };
 18     
 19     private static int[][] vis;
 20     private static char[][] map = {
 21             {'*','*','*','*','@'},
 22             {'*','@','@','*','@'},
 23             {'*','@','*','*','@'},
 24             {'@','@','@','*','@'},
 25             {'@','@','*','*','@'},
 26     };
 27 
 28     private static Node q;
 29 
 30     private static Node pos;
 31 
 32     private static ArrayDeque<Node> que;
 33 
 34     private static Scanner cin;
 35     
 36     public static void main(String[] args) {
 37         cin = new Scanner(System.in);
 38 //        cinDataForBfs();
 39         cinDataForDfs();
 40     }
 41 
 42     
 43     public static void cinDataForBfs() {
 44         while(cin.hasNext()) {
 45             int m = cin.nextInt();
 46             int n = cin.nextInt();
 47             if(m == 0 && n == 0) {
 48                 break;
 49             }
 50             int ans = 0;
 51             for(int i = 0; i < m; i++) {
 52                 for(int j = 0; j < n; j++) {
 53                     if(map[i][j] == '@') {
 54                         ans++;
 55                         bfs(i,j,m,n);
 56                     }
 57                 }
 58             }
 59             System.out.println(ans);
 60         }
 61     }
 62     
 63     public static void cinDataForDfs() {
 64         while(cin.hasNext()) {
 65             int m = cin.nextInt();
 66             int n = cin.nextInt();
 67             if(m == 0 && n == 0) {
 68                 break;
 69             }
 70             int ans = 0;
 71             for(int i = 0; i < m; i++) {
 72                 for(int j = 0; j < n; j++) {
 73                     if(map[i][j] == '@') {
 74                         ans++;
 75                         dfs(i,j,m,n);
 76                     }
 77                 }
 78             }
 79             System.out.println(ans);
 80         }
 81     }
 82     
 83     
 84     public static void dfs(int x, int y, int m, int n) {
 85         for(int i = 0; i < 8; i ++) {
 86             int xx = x + dir[i][0];
 87             int yy = y + dir[i][1];
 88             if(xx >= 0 && xx < n && yy >= 0 && yy < m && map[xx][yy] == '@'){
 89                 map[xx][yy] = '*';
 90                 dfs(xx,yy,m,n);
 91             }
 92         }
 93     }
 94     
 95     public static void bfs(int x, int y,int m, int n) {
 96         vis = new int[105][105];
 97         for(int i = 0; i < 105; i ++) {
 98             for(int j = 0; j < 105; j++) {
 99                 vis[i][j] = 0;
100             }
101         }
102         que = new ArrayDeque<Node>();
103         que.clear();
104         pos = new Node();
105         q = new Node();
106         pos.x = x;
107         pos.y = y;
108         vis[x][y] = 1;
109         que.add(pos);
110         while(!que.isEmpty()) {
111             pos = que.poll();
112             map[pos.x][pos.y] = '*';
113             for(int i = 0; i < 8; i++) {
114                 int next_x = pos.x + dir[i][0];
115                 int next_y = pos.y + dir[i][1];
116                 if(next_x >=0 && next_x < m && next_y >= 0 && next_y < n && vis[next_x][next_y] == 0) {
117                     q.x = next_x;
118                     q.y = next_y;
119                     vis[next_x][next_y] = 1;
120                     map[next_x][next_y] = '*';
121                     que.add(q);
122                 }
123             }
124         }
125         
126     }
127     
128 }
129 class Node{
130     public int x, y;
131 }

 

posted on 2017-11-21 22:18  夏晴天  阅读(201)  评论(0编辑  收藏

导航

统计

Live2D