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]++;
				}
			}
		}
	}
}

这是一道经典的位运算目,考察对二进制的理解和位运算的熟练程度。 目描述: 给定一个长度为 $n$ 的数组 $a$,初始时每个数的值都为 $0$。现在有 $m$ 个操作,每个操作为一次询问或修改。 对于询问,给出两个整数 $l,r$,求 $a_l \oplus a_{l+1} \oplus \cdots \oplus a_r$ 的值。 对于修改,给出一个整数 $x$,表示将 $a_x$ 的值加 $1$。 输入格式: 第一行两个整数 $n,m$。 接下来 $m$ 行,每行描述一次操作,格式如下: 1 l r:表示询问区间 $[l,r]$ 的异或和。 2 x:表示将 $a_x$ 的值加 $1$。 输出格式: 对于每个询问操作,输出一个整数表示答案,每个答案占一行。 数据范围: $1 \leq n,m \leq 10^5$,$0 \leq a_i \leq 2^{30}$,$1 \leq l \leq r \leq n$,$1 \leq x \leq n$ 输入样例: 5 5 2 1 2 3 1 2 4 2 2 1 1 5 输出样例: 0 2 解题思路: 对于询问操作,可以利用异或的性质,即 $a \oplus b \oplus a = b$,将 $a_l \oplus a_{l+1} \oplus \cdots \oplus a_r$ 转化为 $(a_1 \oplus \cdots \oplus a_{l-1}) \oplus (a_1 \oplus \cdots \oplus a_r)$,因为两个前缀异或后的结果可以相互抵消,最后的结果即为 $a_1 \oplus \cdots \oplus a_{l-1} \oplus a_1 \oplus \cdots \oplus a_r = a_l \oplus \cdots \oplus a_r$。 对于修改操作,可以将 $a_x$ 对应的二进制数的每一位都分离出来,然后对应位置进行修改即可。由于只有加 $1$ 操作,所以只需将最后一位加 $1$ 即可,其余位不变。 参考代码:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值