Light OJ - 1012 深搜简单题 Java解题

Light OJ - 1012

深搜基本题

题目
Once there was a king named Akbar. He had a son named Shahjahan. For an unforgivable reason the king wanted him to leave the kingdom. Since he loved his son he decided his son would be banished in a new place. The prince became sad, but he followed his father’s will. In the way he found that the place was a combination of land and water. Since he didn’t know how to swim, he was only able to move on the land. He didn’t know how many places might be his destination. So, he asked your help.For simplicity, you can consider the place as a rectangular grid consisting of some cells. A cell can be a land or can contain water. Each time the prince can move to a new cell from his current position if they share a side.Now write a program to find the number of cells (unit land) he could reach including the cell he was living.

题目大意是:(其实是百度翻译的)

从前有个国王叫阿克巴。他有个儿子叫沙贾汗。由于不可原谅的原因,国王希望他离开王国。因为他爱他的儿子,他决定把他的儿子放逐到一个新的地方。王子很伤心,但他听从了父亲的意愿。他发现这个地方是水和地的结合体。因为他不知道怎么游泳,他只能在陆地上移动。他不知道有多少地方可能是他的目的地。所以,他请求你的帮助。为了简单起见,你可以把这个地方看作是一个由一些单元格组成的矩形网格。细胞可以是陆地,也可以含有水。每次王子都可以从他现在的位置移到一个新的牢房,如果他们有共同的一面。现在写一个程序,找出他可以到达的牢房数量(单位土地),包括他所居住的牢房。

输入

Input starts with an integer T (≤ 500), denoting the number of test cases.
Each case starts with a line containing two positive integers W and H;
W and H are the numbers of cells in the x and y directions, respectively. W and H are not more than 20.
There will be H more lines in the data set, each of which includes W characters. Each character represents the status of a cell as follows.

  1. ‘.’ - land 陆地
  2. ‘#’ - water 水
  3. ‘@’ - initial position of prince (appears exactly once in a dataset)小王子

输出

For each case, print the case number and the number of cells he can reach from the initial position (including it).

实例

4 6 9
…#.
…#





#@…#
.#…#.
11 9
.#…
.#.#######.
.#.#…#.
.#.#.###.#.
.#.#…@#.#.
.#.#####.#.
.#…#.
.#########.

11 6
…#…#…#…
…#…#…#…
…#…#…###
…#…#…#@.
…#…#…#…
…#…#…#…
7 7
…#.#…
…#.#…
###.###
…@…
###.###
…#.#…
…#.#…

实例输出

Case 1: 45
Case 2: 59
Case 3: 6
Case 4: 13

大致题意是一个国王要找王子,王子不会游泳,国王想知道王子可能在的地方有多少个

代码如下

import java.util.Scanner;

public class Main {
	
	//全局变量
	private static int nx;//总面积
	private static int ny;
	private static char failed[][];
	private static int count[];

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//深搜王子题
		int N;
		Scanner scanner=new Scanner(System.in);
		N=scanner.nextInt();
		count=new int[N];
		for(int i=0;i<N;i++)count[i]=1;
		for(int n=0;n<N;n++) {
			nx=scanner.nextInt();
			ny=scanner.nextInt();
			failed=new char[ny][nx];
			for(int i=0;i<ny;i++) 
				failed[i]=scanner.next().toCharArray();
			for(int i=0;i<ny;i++) 
				for(int j=0;j<nx;j++) 
					if(failed[i][j]=='@')
						dft(j,i,n);
		}
		
		for(int i=0;i<N;i++)
			System.out.println("Case "+(i+1)+": "+count[i]);
	}
	
	private static void dft(int x,int y,int n) {
		failed[y][x]='*';
		//这里显示过程,简单明了
		System.out.println("--------------------------------------");
		System.out.println("x="+x+" y="+y);
		for(char a[]:failed) {
			for(char b:a) 
				System.out.print(b);		
			System.out.println("");
		}
		
		int dx_array[]={-1,0,1,0};
		int dy_array[]={0,1,0,-1};
		for(int i=0;i<4;i++) {
			int dx=dx_array[i];int dy=dy_array[i];
			int px=x+dx;int py=y+dy;
			if((px>=0&&px<nx)&&(py>=0&&py<ny)) {
				if(failed[py][px]=='.') {
					dft(px,py,n);
					count[n]++;
				}
			}
		}
	}
}

发布了5 篇原创文章 · 获赞 1 · 访问量 187

猜你喜欢

转载自blog.csdn.net/AcStudio/article/details/87902710