`
superhack
  • 浏览: 32109 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

TCHS-1-1000

J# 
阅读更多

Problem Statement

    

People enjoy mazes, but they also get them dirty. Arrows, graffiti, and chewing gum are just a few of the souvenirs people leave on the walls. You, the maze keeper, are assigned to whiten the maze walls. Each face of the wall requires one liter of paint, but you are only required to paint visible faces. You are given a map of the maze, and you must determine the amount of paint needed for the job.

The maze is described by a String[] maze, where each character can be either '#' (a wall) or '.' (an empty space). All '.' characters on the perimeter of the map are considered entrances to the maze. Upon entering the maze, one can only move horizontally and vertically through empty spaces, and areas that are not reachable by these movements are not considered visible. Each '#' represents a square block with four wall faces (each side of the square is a face). A face is visible if it is not directly adjacent to another wall (and is in a reachable area of the maze). For example, two adjacent blocks can have at most six visible faces since two of their faces are directly adjacent to each other. All exterior faces on the perimeter are considered visible.

For example, the following picture represents a trivial maze with just one (wide) entrance and only four empty reachable spaces:

To whiten this maze you must paint the faces highlighted in yellow above: 16 for its perimeter, plus 8 interior faces. Note that there are faces that are not visible and thus need not be painted.

Definition

    
Class: TroytownKeeper
Method: limeLiters
Parameters: String[]
Returns: int
Method signature: int limeLiters(String[] maze)
(be sure your method is public)
    
 

Constraints

- maze will contain between 1 and 50 elements, inclusive.
- Each element of maze will contain between 1 and 50 characters, inclusive.
- All elements of maze will have the same number of characters.
- All characters in maze will be either '.' or '#'.

Examples

0)  
    
{"##..#" ,"#.#.#" ,"#.#.#" ,"#####"}

Returns: 24

Example from the problem statement.
1)  
    
{"##", "##"}

Returns: 8

Only the perimeter of the maze (which has no interior!) has to be painted.
2)  
    
{"######" ,"#....." ,"#.####" ,"#.#..#" ,"#.##.#" ,"#....#" ,"######"}

Returns: 56

 
3)  
    
{"######" ,"#....." ,"#..#.." ,"#....." ,"######"}

Returns: 36

 
4)  
    
{"#.#.#.#" ,".#.#.#." ,"#.#.#.#" ,".#.#.#."}

Returns: 36

 

 

import java.util.*;

public class TroytownKeeper {

	class Pos {
		int x, y;
		Pos(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	String[] maze;
	int row, col;
	boolean[][] reachable, visited;
	enum Direction {N, E, S, W};

	public int limeLiters(String[] maze) {
		this.row = maze.length;
		this.col = maze[0].length();
		this.maze = maze;
		this.reachable = new boolean[row][col];
		this.visited = new boolean[row][col];
		for (int i = 0; i < row; i++)
			for (int j = 0; j < col; j++)
				reachable[i][j] = visited[i][j] = false;

		for (int i = 0; i < col; i++)
			if (maze[0].charAt(i) == '.')
				bfs(new Pos(0, i));
		for (int i = 0; i < col; i++)
			if (maze[row - 1].charAt(i) == '.')
				bfs(new Pos(row - 1, i));
		for (int i = 1; i < row - 1; i++)
			if (maze[i].charAt(0) == '.')
				bfs(new Pos(i, 0));
		for (int i = 1; i < row - 1; i++)
			if (maze[i].charAt(col - 1) == '.')
				bfs(new Pos(i, col - 1));

		Pos p, nb = null;
		int count = 0;
		for (int i = 0; i < row; i++)
			for (int j = 0; j < col; j++) {
				if (maze[i].charAt(j) != '#')
					continue;
				p = new Pos(i, j);
				for (Direction d : Direction.values()) {
					nb = getNeighbor(p, d);
					if (nb == null)
						count++;
					else if (maze[nb.x].charAt(nb.y) == '.' && reachable[nb.x][nb.y])
						count++;
				}
			}

		return count;
	}

	void bfs(Pos s) {
		if (reachable[s.x][s.y])
			return;
		Queue<Pos> queue = new LinkedList<Pos>();
		Pos p, nb = null;
		reachable[s.x][s.y] = true;
		queue.add(s);
		while (!queue.isEmpty()) {
			p = queue.poll();
			visited[p.x][p.y] = true;
			for (Direction d : Direction.values()) {
				nb = getNeighbor(p, d);
				if (nb != null && maze[nb.x].charAt(nb.y) == '.') {
					if (visited[nb.x][nb.y] == true)
						continue;
					reachable[nb.x][nb.y] = true;
					queue.add(nb);
				}
			}
		}
	}

	Pos getNeighbor(Pos p, Direction d) {
		switch (d) {
		case N: if (p.x - 1 >= 0)
					return new Pos(p.x - 1, p.y);
				break;
		case E: if (p.y + 1 <= col - 1)
					return new Pos(p.x, p.y + 1);
				break;
		case S: if (p.x + 1 <= row - 1)
					return new Pos(p.x + 1, p.y);
				break;
		case W: if (p.y - 1 >= 0)
					return new Pos(p.x, p.y - 1);
				break;
		}
		return null;
	}

}

 

见到一个NB的(ruk)...

 

 

 

分享到:
评论

相关推荐

    javalruleetcode-Open-Source-Algorithms:快乐编码!

    TCHS-SRM-1 SRM - 算法单轮比赛 2. USACO - C++11 礼物1.cpp 骑车.cpp 测试.cpp 3.乌拉尔 - - C++11,Java 1.8 乌拉尔在线法官的可能解决方案 反向Root.cpp 总和文件 求和程序 最终排名.cpp 磁暴.cpp 磁暴.java 寂寞...

    唐亚辉文件关于算法,怎样去实现算法的

    1. **3G移动通信技术概述** - WCDMA (宽带码分多址) - TD-SCDMA (时分同步码分多址) - CDMA2000 (码分多址2000) 2. **C语言编程实践** - 文件包含了C语言代码示例 - 涉及到的基本输入输出操作 - 数学函数的...

    基站代维考试复习题汇总.doc

    1. **FXC E1 板**:FXC E1 板提供4个75欧姆的2M接口,可以作为回路网络的主控或从属方,线路接口4的Rx-connector可作为同步接口接收外部时钟信号。但FXC E1板并不是微波传输设备。 2. **VX系列模块**:FXC E1对应的...

    基站代维考试复习题Nokia设备.docx

    4. 配置ULTRASITE传输时,不需要配置EDAP(可能是指电子数据接入点),而需要配置TCHs(时隙信道)、TRXSIG(传输信号)和OMUSIG(操作维护信号)。 5. DE34基站的公共设备直流电源由CSUA模块提供,而非PWSB、PSUA...

    Bit Error Rate Testing with CMU300

    BER通常以百分比形式表示,例如1×10^-6表示每百万个比特中有1个比特出错。 #### 支持的测试环境与信道 CMU300支持多种测试环境与信道,包括但不限于: - **上行链路(UL)与下行链路(DL)信号**:包括信道编号...

    TOPCODER 算法PPT1

    【TOPCODER算法PPT1】是一份关于2007年TopCoder竞赛算法讲座的机密文件,它揭示了这个全球知名的编程竞赛平台的核心特点和价值。TopCoder社区是其核心,拥有遍布全球的会员,包括众多活跃的参赛者,涵盖了学生和专业...

    techies:Javascript 验证库。 为什么? 因为我写了一些非常糟糕的验证代码,并且想在学习的同时做一些更健壮的事情

    要告诉技术人员验证元素,请将“tchs”属性添加到元素。 &lt;input type="text" tchs=""&gt;&lt;/input&gt; 技术人员利用规则来验证元素。 验证是在每个元素的基础上完成的,并且根据所使用的元素进行不同的工作...

Global site tag (gtag.js) - Google Analytics