Problem Statement
|
|
When programming, I don't like to take my fingers off the keyboard, and hence, I don't use the mouse. So, when navigating my source, I like to do so in such a way as to minimize keystrokes. My text editor supports the following keystrokes: left, right, up, down, home, end, top, bottom, word left and word right. The word left and word right keystrokes position the cursor at a word beginning; a word beginning is the first letter of the word. None of the keystrokes cause the cursor to wrap. When moving the cursor vertically, my text editor does not change the cursor's horizontal position. So, if the cursor is at column 50 and the up arrow is pressed, moving the cursor to a row with only 10 characters, only the row changes. My text editor does not allow the cursor to be positioned left of the first column, above the first row or below the last row.
The keys left, right, up, down, home, end, top, bottom, word left and word right behave as described below:
1. left, right, up and down - move the cursor one position in the indicated direction (see notes). 2. home - causes the cursor to move to column 0 of the current row. 3. end - causes the cursor to move to the last character of source in the current row. 4. top - causes the cursor to move to the top row (row 0) retaining its column position. 5. bottom - causes the cursor to move to the bottom row retaining its column position. 6. word left - causes the cursor to jump to the first word beginning that is strictly left of the cursor position, if one exists. Otherwise does nothing. 7. word right - causes the cursor to jump to the first word beginning that is strictly right of the cursor position, if one exists. Otherwise does nothing.
You will be given a String[] source representing the source code, a int[] start representing the starting position of the cursor and a int[] finish representing the ending position of the cursor. The first int in both start and finish specifies the 0-indexed row and the second int specifies the 0-indexed column. You are to calculate and return the minimum number of keystrokes required to get from start to finish. |
Definition
|
|
Class: |
TextEditorNavigation |
Method: |
keystrokes |
Parameters: |
String[], int[], int[] |
Returns: |
int |
Method signature: |
int keystrokes(String[] source, int[] start, int[] finish) |
(be sure your method is public) |
|
|
|
Notes
|
- |
For the purposes of this problem, we use the convention that the cursor covers each character. Some editors (normally in "insert mode") have the cursor preceding each character. |
- |
A keypress may not have any effect. For example, pressing up in the top row does nothing, pressing left in the first column does nothing and pressing down in the bottom row does nothing. Pressing right always has an effect. |
Constraints
|
- |
source will contain between 1 and 50 elements, inclusive. |
- |
Each element of source will contain between 1 and 50 characters, inclusive. |
- |
Each character must be a letter ('a'-'z', 'A'-'Z') or ' '. |
- |
start and finish will each contain exactly 2 elements. |
- |
start and finish will each represent character positions that exist in source. |
Examples
|
0) |
|
|
{"AAAAA AAA AAAAAAAAAAAAA AAAA",
"AA AAAAAAAAA AAAA AAAA",
"BBBBBBBBBBBBBBBBBBBBBBBBBBB",
"BBBBBBB BBBBBBBBBB BBBBBBB",
"CCC CCCC CCCCCC CCCC",
"DDDDDDDDDDDDDDDDDDD"}
|
{5, 7}
|
{2, 2}
|
|
Returns: 6
|
This can be achieved by the following keystrokes in the given order: home, top, down, down, right, right. |
|
|
1) |
|
|
{"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"BB BB BB BB BB BB BB BB BB BB BB BB BB BB BB BB BB",
"CCC CCC CCC CCC CCC CCC CCC CCC CCC CCC CCC CCC CC",
"DDDD DDDD DDDD DDDD DDDD DDDD DDDD DDDD DDDD DDDD ",
"EEEEE EEEEE EEEEE EEEEE EEEEE EEEEE EEEEE EEEEE EE",
"FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ",
"GGG GGG GGG GGG GGG GGG GGG GGG GGG GGG GGG GGG GG",
"HHHHHHHHHHH HHHHHHHHHH HHHHHHHHHH HHHHHHHHHH HHHHH",
"IIIIIIIIIIIIIII IIIIIIIIIIIIIII IIIIIIIIIIIIIII ",
"JJJJJJJJ JJJJJJJJ JJJJJJJJ JJJJJJJJ JJJJJJJJ JJJJJ",
"KKKKKKKKKKKKKKKKKKKKKKKKKK KKKKKKKKKKKKKKKKKKKKKKK",
"LLLLLLLLLL LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL",
"MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM",
"N N N N N N N N N N N N N N N N N N N N N N N N N ",
"OOOOO OOOO OOO OO O O OO OOO OOOO OOOOO OOOOOO OOO",
"PPPPPPP PPPPPP PPPPP PPPP PPP PP P P PP PPP PPPP P",
"QQQQQQ QQQQQ QQQQ QQQ QQ Q Q QQ QQQ QQQQ QQQQQ QQQ",
"ZZZZ ZZ ZZZ ZZ ZZZZ ZZ ZZZ ZZ ZZZZ ZZ ZZZ ZZ ZZZZ ",
"SSS S SSS S SSS S SSS S SSS S SSS S SSS S SSS S SS",
"TT TT TT TT TT TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"}
|
{12, 20}
|
{4, 36}
|
|
Returns: 8
|
|
|
2) |
|
|
{"A A A A AAAAAAA A A A A A A A A A A",
"B BBBBB B B B B BBBBB B B B B B B B B"}
|
{1, 0}
|
{1, 22}
|
|
Returns: 6
|
|
|
3) |
|
|
{"AAAAAAAAAAAAAA A A A A A A A A A A"}
|
{0, 2}
|
{0, 15}
|
|
Returns: 1
|
|
|
4) |
|
|
{"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"N N N N N N N N N N N N N N N N N N N N N N N N N ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
"A A A A A A A A A A A A A A A A A A A A A A A A A ",
" O O O O O O O O O O O O OO O O O O O O O O O O O ",
" P P P P P P P P P P P P P PP P P P P P P P P P P ",
" Q Q Q Q Q Q Q Q Q Q Q Q Q Q QQ Q Q Q Q Q Q Q Q Q ",
" R R R R R R R R R R R R R R R RR R R R R R R R R ",
" S S S S S S S S S S S S S S S S SS S S S S S S S ",
" T T T T T T T T T T T T T T T T T TT T T T T T T ",
" U U U U U U U U U U U U U U U U U U UU U U U U U ",
" V V V V V V V V V V V V V V V V V V V VV V V V V ",
" W W W W W W W W W W W W W W W W W W W W WW W W W ",
" X X X X X X X X X X X X X X X X X X X X X XX X X ",
" Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y YY Y ",
" Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z ZZZ"}
|
{49, 49}
|
{38, 26}
|
|
Returns: 23
|
|
|
5) |
|
|
{"AAA", "BB", "CCC"}
|
{1, 1}
|
{1, 1}
|
|
Returns: 0
|
|
|
6) |
|
|
{"AAAAA AAA AAAAAAAAAAAAA AAAA",
"AA AAAAAAAAA AAAA AAAA",
"BBBBBBBBBBBBBBBBBBBBBBBBBBB",
"BBBBBBB BBBBBBBBBB BBBBBBB",
"CCC CCCC CCCCCC CCCC",
"DDDDDDDDDDDDDDDDDDD"}
|
{2, 17}
|
{1, 2}
|
|
Returns: 4
|
|
|
7) |
|
|
{"A PC to do CAD huh Sounds reasonable",
"Aurthor go out and buy us five new PCs",
"Dont you want to think about this for a minute",
"No every second counts and we want to be ahead of",
"the competition",
" OK Greate idea Please place lOOk worth of",
"unmarked bills in my suitcase and Ill be on my way"}
|
{0, 11}
|
{1, 15}
|
|
Returns: 2
|
|
|
import java.util.*;
public class TextEditorNavigation {
class Pos {
int row;
int col;
Pos(int row, int col) {
this.row = row;
this.col = col;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + col;
result = prime * result + row;
return result;
}
@Override
public boolean equals(Object o) {
Pos p = (Pos) o;
if (p != null && p.row == row && p.col == col)
return true;
return false;
}
}
enum Keys {LEFT, RIGHT, UP, DOWN, HOME, END, TOP, BOTTOM, WORD_LEFT, WORD_RIGHT}
String[] code;
List<List<Integer>> bounds = new LinkedList<List<Integer>>();
public int keystrokes(String[] code, int[] from, int[] to) {
this.code = code;
for (int i = 0; i < code.length; i++) {
List<Integer> bound = new LinkedList<Integer>();
for (int j = 0, n = code[i].length(); j < n; ) {
for ( ; j < n && code[i].charAt(j) == ' '; j++) ;
if (j < n)
bound.add(j);
for ( ; j < n && code[i].charAt(j) != ' '; j++) ;
}
bounds.add(bound);
}
int[][] map = new int[50][50];
for (int[] a : map)
Arrays.fill(a, Integer.MAX_VALUE);
Pos s = new Pos(from[0], from[1]), t = new Pos(to[0], to[1]);
LinkedList<Pos> open = new LinkedList<Pos>();
LinkedList<Pos> close = new LinkedList<Pos>();
map[s.row][s.col] = 0;
open.add(s);
int times = 0;
while (!open.isEmpty() && ++times < 9999) {
Pos a = open.poll();
close.add(a);
for (int i = 0; i < Keys.values().length; i++) {
Pos b = getNext(a, Keys.values()[i]);
if (b == null || open.contains(b) || close.contains(b))
continue;
map[b.row][b.col] = map[a.row][a.col] + 1;
if (b.equals(t))
return map[b.row][b.col];
open.add(b);
}
}
return 0;
}
private Pos getNext(Pos a, Keys key) {
Pos b = null;
switch (key) {
case LEFT:
if (a.col > code[a.row].length() - 1)
b = new Pos(a.row, code[a.row].length() - 1);
else if (a.col > 0)
b = new Pos(a.row, a.col - 1);
break;
case RIGHT:
if (a.col > code[a.row].length() - 1)
b = new Pos(a.row, code[a.row].length() - 1);
else if (a.col < code[a.row].length() - 1)
b = new Pos(a.row, a.col + 1);
break;
case UP:
if (a.row > 0)
b = new Pos(a.row - 1, a.col);
break;
case DOWN:
if (a.row < code.length - 1)
b = new Pos(a.row + 1, a.col);
break;
case HOME:
if (a.col > 0)
b = new Pos(a.row, 0);
break;
case END:
if (a.col != code[a.row].length() - 1)
b = new Pos(a.row, code[a.row].length() - 1);
break;
case TOP:
if (a.row > 0)
b = new Pos(0, a.col);
break;
case BOTTOM:
if (a.row < code.length - 1)
b = new Pos(code.length - 1, a.col);
break;
case WORD_LEFT:
if (a.col > code[a.row].length() - 1)
b = new Pos(a.row, code[a.row].length() - 1);//?
else {
for (int i = bounds.get(a.row).size() - 1; i >= 0; i--)
if (bounds.get(a.row).get(i) < a.col) {
b = new Pos(a.row, bounds.get(a.row).get(i));
break;
}
}
break;
case WORD_RIGHT:
if (a.col > code[a.row].length() - 1)
b = new Pos(a.row, code[a.row].length() - 1);//?
else {
for (int i = 0; i < bounds.get(a.row).size(); i++)
if (bounds.get(a.row).get(i) > a.col) {
b = new Pos(a.row, bounds.get(a.row).get(i));
break;
}
}
break;
}
return b;
}
}
分享到:
相关推荐
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`函数输出计算结果,包括基站数量、链路接...
电路交换业务信道(TCHs)测试是BER测试的重要组成部分,主要用于评估语音和低速数据服务的质量。这一部分将详细介绍如何设置测试环境并执行测试。 #### 分组数据业务信道(PDTCHs)测试 随着数据传输速度的提高,...
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则专注于网页...