设为首页 加入收藏

TOP

HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
2014-11-23 19:22:28 来源: 作者: 【 】 浏览:4
Tags:HDU 1312 Red and Black DFS 深度 优先 搜索 BFS 广度

Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6762 Accepted Submission(s): 4284

Problem Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)

Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output
45
59
6
13


方法一 DFS(深度优先搜素)

import java.io.*;
import java.util.*;
public class Main {
	int M=22,w,h,sx,sy;
	char ch[][];
	int fx[]={1,-1,0,0};
	int fy[]={0,0,1,-1};
	int number;
	boolean boo[][]=new boolean[100][100];
	public static void main(String[] args) {
		new Main().work();
	}
	void work(){
		Scanner sc=new Scanner(new BufferedInputStream(System.in));
		while(sc.hasNext()){
			w=sc.nextInt();
			h=sc.nextInt();
			if(h==0&&w==0)
				System.exit(0);
			ch=new char[h][w];
			for(int i=0;ih-1||py<0||py>w-1||ch[px][py]!='.')
			return false;
		return true;
	}
}
import java.io.*;
import java.util.*;

public class Main {
	Queue que = new LinkedList();
	boolean boo[][] = new boolean[100][100];
	char ch[][];
	int w, h;
	int fx[] = { 1, -1, 0, 0 };
	int fy[] = { 0, 0, 1, -1 };
	int number;

	public static void main(String[] args) {
		new Main().work();
	}

	void work() {
		Scanner sc = new Scanner(new BufferedInputStream(System.in));
		while (sc.hasNext()) {
			w = sc.nextInt();
			h = sc.nextInt();
			if(h==0&&w==0)
				System.exit(0);
			ch = new char[h][w];
			for (int i = 0; i < h; i++) {
				String s = sc.next();
				ch[i] = s.toCharArray();
				Arrays.fill(boo[i], false);
			}
			Node node = new Node();
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					if (ch[i][j] == '@') {
						node.x = i;
						node.y = j;
						node.number = 1;
					}
				}
			}
			boo[node.x][node.y] = true;
			que.add(node);
			number = 1;
			BFS();
			System.out.println(number);

		}
	}

	void BFS() {
		while (!que.isEmpty()) {

			Node node = que.poll();
			for (int i = 0; i < 4; i++) {
				int px = node.x + fx[i];
				int py = node.y + fy[i];
				if (check(px, py) && !boo[px][py]) {
					number++;
					Node td = new Node();
					td.x = px;
					td.y = py;
					boo[px][py] = true;
					ch[px][py] = 'S';
					que.add(td);
				}
			}
		}
	}

	boolean check(int px, int py) {
		if (px < 0 || px > h - 1 || py < 0 || py > w - 1 || ch[px][py] != '.')
			return false;
		return true;
	}

	class Node {
		int x;
		int y;
		int number;

	}
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 1241 Oil Deposits DFS(深度.. 下一篇poj 1247 The Perfect Stall 裸的..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·求navicat for mysql (2025-12-26 13:21:33)
·有哪位大哥推荐一下m (2025-12-26 13:21:30)
·MySQL下载与安装教程 (2025-12-26 13:21:26)
·Linux_百度百科 (2025-12-26 12:51:52)
·Shell 流程控制 | 菜 (2025-12-26 12:51:49)