ACM HDOJ 1312 (Red and Black)
来源:互联网 发布:美女图片源码 编辑:程序博客网 时间:2024/06/01 17:51
题目链接 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;}}
0 0
- ACM HDOJ 1312 (Red and Black)
- hdoj 1312 Red and Black
- HDOJ 1312 Red and Black
- HDOJ 1312 Red and Black
- HDOJ 1312 Red and Black
- hdoj 1312 Red and Black
- 【hdoj 1312】Red and Black
- hdoj-1312-Red and Black
- hdoj 1312 Red and Black
- hdoj 1312 Red and Black
- HDOJ 1312 Red and Black
- hdoj-1312-Red and Black
- HDOJ Red and Black
- HDOJ 1312 Red and Black (DFS)
- HDOJ--1312--Red and Black【DFS】
- hdoj 1312 Red and Black 【BFS】
- HDOJ 1312 Red and Black 基础DFS
- HDoj-1312-Red and Black -BFS
- 设计模式初探-代理模式
- 合理利用锁机制优化MySQL
- QT 4.8 静态库编译方法
- android WebView html5 使用本地存储
- QT中的qmake详解
- ACM HDOJ 1312 (Red and Black)
- Qt Creator插件工作流程代码走读
- 黑马程序员--基础加强--第六篇--反射
- 谷歌蝉联"财富"2014最佳雇主
- iphone开发 地图线路
- Qt中translate、tr关系 与中文问题
- Qt文件处理
- Qt的QFile类详解
- backgroundPosition