题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1312
程序一 广度优先搜索
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
int width = Integer.parseInt(scn.next());
int height = Integer.parseInt(scn.next());
if (0 == width && 0 == height) {
break;
}
Search search = new Search(height, width);
for (int i = 0; i < height; ++i) {
String str = scn.next();
search.setMatrixLine(i, str.toCharArray());
int position = str.indexOf("@");
if (-1 != position) {
search.setStartX(i);
search.setStartY(position);
}
}
search.beginSearch();
}
scn.close();
}
}
class Search {
private final int[][] direction = { { -1, 0 }, { 1, 0 }, { 0, -1 },
{ 0, 1 } };
private int height;
private int width;
private char[][] matrix;
private int startX;
private int startY;
public Search(int height, int width) {
this.height = height;
this.width = width;
matrix = new char[height][width];
}
public void beginSearch() {
int count = 1;
Queue<Node> queue = new LinkedList<Node>();
queue.offer(new Node(startX, startY));
matrix[startX][startY] = '#';
while (!queue.isEmpty()) {
Node node = queue.poll();
for (int k = 0; k < 4; ++k) {
int nextX = node.getX() + direction[k][0];
int nextY = node.getY() + direction[k][1];
if (0 > nextX || nextX >= height || 0 > nextY || nextY >= width
|| '#' == matrix[nextX][nextY]) {
continue;
}
queue.offer(new Node(nextX, nextY));
matrix[nextX][nextY] = '#';
++count;
}
}
System.out.println(count);
}
public void setMatrixLine(int line, char[] ch) {
matrix[line] = ch;
}
public void setStartX(int startX) {
this.startX = startX;
}
public void setStartY(int startY) {
this.startY = startY;
}
}
class Node {
private int x;
private int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
程序二 深度优先搜索
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
int width = Integer.parseInt(scn.next());
int height = Integer.parseInt(scn.next());
if (0 == width && 0 == height) {
break;
}
Search search = new Search(height, width);
for (int i = 0; i < height; ++i) {
String str = scn.next();
search.setMatrixLine(i, str.toCharArray());
int position = str.indexOf("@");
if (-1 != position) {
search.setStartX(i);
search.setStartY(position);
}
}
search.beginSearch();
System.out.println(search.getCount());
}
scn.close();
}
}
class Search {
private final int[][] direction = { { -1, 0 }, { 1, 0 }, { 0, -1 },
{ 0, 1 } };
private int height;
private int width;
private char[][] matrix;
private int startX;
private int startY;
private int count;
public Search(int height, int width) {
this.height = height;
this.width = width;
matrix = new char[height][width];
count = 1;
}
public void beginSearch() {
dfs(startX, startY);
}
private void dfs(int x, int y) {
for (int k = 0; k < 4; ++k) {
int nextX = x + direction[k][0];
int nextY = y + direction[k][1];
if (0 <= nextX && nextX < height && 0 <= nextY && nextY < width
&& '.' == matrix[nextX][nextY]) {
matrix[nextX][nextY] = '#';
++count;
dfs(nextX, nextY);
}
}
}
public void setMatrixLine(int line, char[] ch) {
matrix[line] = ch;
}
public void setStartX(int startX) {
this.startX = startX;
}
public void setStartY(int startY) {
this.startY = startY;
}
public int getCount() {
return count;
}
}