ACM HDOJ 1312 (Red and Black)

题目链接 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;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值