Problem Statement
|
|
We are given a maze which is a cube of NxNxN cells. We start at (1,1,1) and we move to (N,N,N), in each move visiting one of the three adjacents cells in the positive x, y or z directions. Each cell of the maze contains either a '(', ')' or '.'. A path is the sequence of cells visited during the journey. We intend to find the number of such paths that produce a valid parenthesized expression.
Periods can occur freely in an expression, and must be ignored while checking if an expression is properly paranthesized or not. A paranthesized expression is said to be valid only if it adheres to the following grammar (quotes for clarity): <expr> ::= empty-string | "("<expr>")" | <expr>"." | <expr><expr>
e.g., "(())", "....", ".()." and "().(.)" are valid parathesized expressions. The maze is given in a String[] which, when concantenated, represents the cube encoded in the following manner. The characters of maze correspond to the following cube coordinates, in order: (1,1,1), (1,1,2), ..., (1,1,N), (1,2,1), (1,2,2), ..., (1,N,N), (2,1,1), ..., (N,N,N)
Given the String[] maze return an int representing the number of possible paths forming valid parenthesized expressions. If there are more than 1,000,000,000 paths, return -1 instead.
|
Definition
|
|
Class: |
BracketMaze |
Method: |
properPaths |
Parameters: |
String[], int |
Returns: |
int |
Method signature: |
int properPaths(String[] maze, int N) |
(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. |
- |
The maze when concatenated will contain exactly N^3 characters. |
- |
Each character in maze will be '(', ')', or '.'. |
- |
N will be between 1 and 13, inclusive. |
Examples
|
0) |
|
|
|
Returns: 2
|
There are two paths in this 2x2x2 cube. From the 3 possible moves in the beginning, you cannot go right directly which completes a "()" but all it's neighbours are ')', hence lead to an invalid expression. The other two possible moves lead to unique valid paths. |
|
|
1) |
|
|
|
2) |
|
|
{ "..........................." }
|
3
|
|
Returns: 90
|
|
|
3) |
|
|
|
4) |
|
|
{ "(.........................)"}
|
3
|
|
Returns: 90
|
|
|
import java.util.Arrays;
public class BracketMaze {
String s = "";
int n;
long[][][][] memo;
long dfs(int x, int y, int z, int p) {
char c = s.charAt(x * n * n + y * n + z);
if (x==y && y==z && z==n-1)
return (p==0 && c=='.' || p==1 && c==')') ? 1 : 0;
if (memo[x][y][z][p] != -1)
return memo[x][y][z][p];
long ret = 0;
int p1 = p;
if (c == '(')
p1++;
else if (c == ')')
p1--;
if (p1 >= 0) {
if (x < n - 1)
ret += dfs(x + 1, y, z, p1);
if (y < n - 1)
ret += dfs(x, y + 1, z, p1);
if (z < n - 1)
ret += dfs(x, y, z + 1, p1);
}
return memo[x][y][z][p] = ret;
}
public int properPaths(String[] maze, int N) {
for (int i = 0; i < maze.length; i++)
s += maze[i];
n = N;
memo = new long[N][N][N][3*N];
for (long[][][] mmm : memo)
for (long[][] mm : mmm)
for (long[] m : mm)
Arrays.fill(m, -1);
long count = dfs(0, 0, 0, 0);
return (count > 1e9) ? -1 : (int) count;
}
}
分享到:
相关推荐
TCHS-SRM-1 SRM - 算法单轮比赛 2. USACO - C++11 礼物1.cpp 骑车.cpp 测试.cpp 3.乌拉尔 - - C++11,Java 1.8 乌拉尔在线法官的可能解决方案 反向Root.cpp 总和文件 求和程序 最终排名.cpp 磁暴.cpp 磁暴.java 寂寞...
- 计算`PDCHS_zhuan`和`TCHS_zhuan`(专用数据信道和语音信道转换后的数量)。 - 计算`DSP_pdch`和`DSP_tch`(所需的数字信号处理器数量)。 #### 输出结果 - 使用`printf`函数输出计算结果,包括基站数量、链路接...
BER通常以百分比形式表示,例如1×10^-6表示每百万个比特中有1个比特出错。 #### 支持的测试环境与信道 CMU300支持多种测试环境与信道,包括但不限于: - **上行链路(UL)与下行链路(DL)信号**:包括信道编号...
TCHs(时隙分配)、TRXSIG(发射信号)和OMUSIG(操作维护信道)是需要配置的。 5. **电源模块**:DE34基站的公共设备直流电源由CSUA模块提供。 6. **GSM多址方式**:GSM系统采用FDMA(频分多址)和TDMA(时分多址...
4. 配置ULTRASITE传输时,不需要配置EDAP(可能是指电子数据接入点),而需要配置TCHs(时隙信道)、TRXSIG(传输信号)和OMUSIG(操作维护信号)。 5. DE34基站的公共设备直流电源由CSUA模块提供,而非PWSB、PSUA...
要告诉技术人员验证元素,请将“tchs”属性添加到元素。 <input type="text" tchs=""></input> 技术人员利用规则来验证元素。 验证是在每个元素的基础上完成的,并且根据所使用的元素进行不同的工作...
此外,TopCoder竞赛提供了丰富的奖金和机会,如TopCoder Open(TCO)、TopCoder Collegiate Challenge(TCCC)和TopCoder High School(TCHS)等,涵盖算法、设计、开发和组装等领域。TopCoder Studio则专注于网页...