题意:记录连在一起的黑块数,以@字符为搜索的源头,只搜索一次。
一道简单的搜索题。。。
java代码:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
/*
* w-width表示列
* h-high表示行
* Node1312表示结点数组
* dir表示方向,上下左右。
*/
static int w, h, sx, sy;
static Node1312[][] map = new Node1312[20][20];
static int dir[][] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
w = sc.nextInt();
h = sc.nextInt();
if (w == 0 || h == 0) {
return;
}
/*
* h表示行
* w表示列
*/
for (int i = 0; i < h; i++) {
String str = sc.next();
for (int j = 0; j < w; j++) {
map[i][j] = new Node1312(i, j, str.charAt(j));
if (map[i][j].c == '@') {
sx = i;
sy = j;
}
}
}
System.out.println(breadthFirstSearch());
}
}
/*
* breadthFirstSearch-广度优先搜索
* x,y表示搜索源的坐标
*/
private static int breadthFirstSearch() {
MyQueue1312<Node1312> mq = new MyQueue1312<Node1312>();
map[sx][sy].c = '#';
mq.add(map[sx][sy]);
int k = 0;
for (; !mq.isEmpty(); k++) {
Node1312 no1 = mq.pop();
for (int i = 0; i < 4; i++) {
int a = no1.x + dir[i][0];
int b = no1.y + dir[i][1];
// 越界
if (a < 0 || b < 0 || a >= h || b >= w) {
continue;
}
// 遇到障碍物
if (map[a][b].c == '#') {
continue;
}
map[a][b].c = '#';
mq.add(map[a][b]);
}
}
return k;
}
}
/*
* Node表示结点
*/
class Node1312 {
int x;
int y;
char c;
public Node1312() {
}
public Node1312(int x, int y, char c) {
this.x = x;
this.y = y;
this.c = c;
}
}
/*
* 加泛型的队列。
* 用来做广搜题。
*/
class MyQueue1312<E> {
ArrayList<E> al = new ArrayList<E>();
int index;
public MyQueue1312() {
index = 0;
}
public void add(E no) {
al.add(no);
index++;
}
public E pop() {
if (al.isEmpty()) {
return null;
}
E nod = al.remove(0);
index--;
return nod;
}
public boolean isEmpty() {
return al.isEmpty();
}
}
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13468 Accepted Submission(s): 8341
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.
但是他不能移动到红色的瓷砖上,只能移动到黑色的瓷砖上。
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和H;
W and H are the numbers of tiles in the x- and y- directions, respectively.
分别表示方块的y行,x列。
W and H are not more than 20.
w和m不会超过20。
There are H more lines in the data set, each of which includes W characters.
w和m不会超过20。
There are H more lines in the data set, each of which includes W characters.
每一个集合中有h行数据,每一行有w个字符。
Each character represents the color of a tile as follows.
每一个字符代表一种颜色。
'.' - a black tile
每一个字符代表一种颜色。
'.' - a black tile
表示黑块
'#' - a red tile
'#' - a red tile
表示红块
'@' - a man on a black tile(appears exactly once in a data set)
'@' - 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
Source