标题
有一个人正试图在正方形板内移动。板是一个大小的网格,点在左上角,点在右下角。这个人只有遵守一些规则才能动起来: 它总是按照当前的风向移动(可能的方向是北、南、西、东、东北、西北、东南、西南)。它只能访问董事会的特定细胞一次。它不能走出董事会的边界。如果它到达一个不能按照规则移动的点,它将保持在当前的位置。它只能移动到邻近的小区(根据上述方向有8个可能的移动)。它总是从点(板的左上角)开始它的旅程。它每秒只能做一次运动。为了给他制造更多的困难,在人的旅途中,风会多次改变方向。在人开始移动之前,你总是会得到风向(第0秒钟),然后你会在几秒钟内得到风向改变的特定时间(按严格的递增顺序)。 在跟踪了这个人的所有旅程之后,你必须说明董事会中有多少职位没有被这个人访问过。 输入格式 在第一行中,您将得到两个整数'n‘和'k’,其中'n‘表示板的维数。然后在下面的'k‘行中,您将得到一个整数'ti’和字符串'w‘。'ti‘代表板上“W”方向的风将为applied.The 'w’可能取N、S、W、E、NE、NW、SE或SW值的时间(分别代表北、南、西、东、东北、西北、东南或西南)。 约束条件 3 <= n <= 100000 2 <= k <= 100000 0 <= ti <=100000 t0 =0< t1 <...< tk-1 输出格式 输出董事会内未被该男子访问的职位数。 样本输入0 5 6 0 SE 1 NE 2英 6瑞士法郎 15 N 20 W 样本输出0 13
public class Solution {
public static void main (String args []) {
//Table
int [][] board = new int [7][7];
// Seconds available for moving
int seconds = 43;
// Initial Man Coordinate
int Man = board [0][0];
// Two dimensional array to save the coordinates visited by the man
int [][] isVisited = new int [7][7];
// Formula to calculate the cells that are not visited by the man ( i know is wrong )
int notVisited = isVisited.isEmpty(true) - isVisited.isEmpty(false);
for (int i=0;i<=board.length;i++) {
for(int j=0;j<=board.length;j--) {
while (seconds < 4 ) {
i++;
j++;
Man = board [i][j];
//Here should be the code to save the coordinates visited by the man -->
发布于 2018-11-28 17:05:13
我首先要创建一个名为Board
的新调用
public class Board {
private boolean[][] visited;
private int boardSize;
private int numVisited;
public Board(int boardSize) {
this.boardSize = boardSize;
visited = new boolean[boardSize][boardSize];
}
public void setVisited(int x, int y) {
if (!visited[x][y]) {
visited[x][y] = true;
numVisited++;
}
}
public int getBoardSize() {
return boardSize;
}
public int getNumVisited() {
return numVisited;
}
public int getNumNotVisited() {
return boardSize * boardSize - numVisited;
}
}
之后,您可以创建一个Board
实例
Board myBoard = new Board(7);
然后,在您的逻辑中,您可以通过调用setVisited
来设置一个访问单元
myBoard.setVisited(3, 3);
并且您可以通过调用countVisited
来计算已访问的单元数量。
int numberOfVisitedCells = myBoard.getNumVisited();
或者,如果您想要访问的单元格数:
int numberofCellsNotVisited = myBoard.getNumNotVisited();
编辑:谢谢马修的改进!
https://stackoverflow.com/questions/53524478
复制相似问题