思路:bfs裸题。

对这种迷宫问题的bfs,我们把坐标点用一个class来存储,并放入队列进行求解。

//一直接收不了输入,找了一个多小时的问题,居然是行和列搞反了ORZ

Red and Black

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 48833   Accepted: 26054

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) 
The end of the input is indicated by a line consisting of two zeros. 

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

Japan 2004 Domestic

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class Point{
	
	public int x;
	public int y;
}
public class Main {

	static int ans=0;//数量初始化为0
	static int[][] dir={{0,1},{0,-1},{-1,0},{1,0}};//右左上下四个方向
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();//列数
		int m=in.nextInt();//行数
		Queue<Point> q=new LinkedList<Point>();//利用队列实现bfs
		while(m!=0&&n!=0){//不为0时读入
			char gra[][]=new char[m][n];//存迷宫
			boolean vis[][]=new boolean[m][n];//标记是否访问
			Point p=new Point();//表示坐标点位置
			for(int i=0;i<m;i++){
				String s=in.next();
				for(int j=0;j<s.length();j++){
					gra[i][j]=s.charAt(j);
					if(gra[i][j]=='@'){	//找到@起始的位置					
						p.x=i;//x,y为搜索起点
						p.y=j;			
					}
				}
			}
			bfs(gra,vis,p,m,n,q);//调用
			System.out.println(ans+1);//输出答案
			ans=0;//恢复为0,为下一组做准备
			q.clear();//清空队列
			n=in.nextInt();//读入列
			m=in.nextInt();//读入行
		}
	}

	private static void bfs(char[][] gra, boolean vis[][],Point p, int m, int n,Queue q) {
		// TODO Auto-generated method stub
		vis[p.x][p.y]=true;//将起始点标记为已访问
		q.add(p);//将起始点放入队列中
		while(!q.isEmpty())//队列不为空时
		{
			Point qq=(Point) q.remove();//取出第一个元素
			for(int i=0;i<4;i++)//枚举该点附近所有能到达的点
			{
				Point pp=new Point();
				pp.x=qq.x+dir[i][0];
				pp.y=qq.y+dir[i][1];
				if((!(pp.x<0||pp.x>=m||pp.y<0||pp.y>=n))&&vis[pp.x][pp.y]==false&&gra[pp.x][pp.y]=='.')//如果点合法
				{
					vis[pp.x][pp.y]=true;//标记已访问
					q.add(pp);//将该点加入队列中
					ans++;//可达点数目+1
				}
			}
		
	}}

}