编程介的小学生 2020-01-26 17:59 采纳率: 0.4%
浏览 164

Board Game

Problem Description
Jerry developed a new board game recently. The board consists of N*N grids and M pieces have been randomly put in these grids. The game rules are defined as follows.

  1. There is a unique grid defined as exit-grid.

  2. In the initial state, no pieces is in exit-grid, and all the pieces are not adjacent to each other(in four directions: up, down, left, right).

  3. All the M pieces are marked from 1 to M .

  4. Each time you are allowed to move one piece to one of its adjacent blocks, which is counted as one step.

  5. When you move the pieces, make sure that all the pieces are not adjacent to each other.

  6. When the smallest piece, i.e. its marked number is the smallest on the board, is moved to exit-grid, you can take that piece away from the board. Pieces other than the smallest one are also allowed to move to exit-grid but they cannot be taken away.

  7. When all the pieces on the board have been taken away, you win the game.

Tom is a very evil person. Every time Jerry playing the board game, he always says he has a better way to win the game, but he is not willing to tell Jerry. Jerry does not know whether Tom's words are true or not, so he asks for your help to write a program that can count the number of fewest steps to win the board game.

Input
To simplify the problem, now we limit 2<=N<=6, 1<=M<=4 and all the initial states are correct.

The first line of the input contains a positive integer T(T <= 200), which indicate the number of test cases.

Then follows T cases of inputs:

  • a positive integer N
  • a positive integer M
  • a N*N matrix of characters to indicate the initial state of the board If the element in the matrix is 'o', it means there is no piece in that grid; otherwise, the element is the marked number of that piece. Note that the element of exit-grid is 'x'.

Output
For each test case, output one line with the number of fewest steps needed to win the game, or "-1" if it is impossible to accomplish that game.

Sample Input
2
3 2
x2o
ooo
oo1

3 3
xo1
o2o
3oo

Sample Output
7
-1

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-26 09:59
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # 读取测试数据
    input <- readLines("test.txt")
    T <- as.integer(input[1])
    
    for (i in 1:T) {
      N <- as.integer(input[i+2])
      M <- as.integer(input[i+3])
      
      # 构建初始状态
      grid <- matrix(NA, nrow=N, ncol=N)
      for (j in 1:M) {
        row <- sample(1:N, 2, replace=FALSE)
        col <- sample(1:N, 2, replace=FALSE)
        grid[row, col] <- j + 1
      }
      
      # 将最小的数移动到exit_grid
      min_num <- min(grid[,1])
      min_row <- which(grid == min_num)[1]
      min_col <- which(grid == min_num)[2]
      grid[min_row, min_col] <- NA
      
      # 检查是否可以到达exit_grid
      reached_exit <- FALSE
      for (j in 1:N) {
        for (k in 1:N) {
          if (grid[j,k] != NA && grid[j,k] < N * N && !reached_exit) {
            reached_exit <- TRUE
            break
          }
        }
      }
      
      # 如果可以到达exit_grid,则输出最少步骤数
      if (reached_exit) {
        print(N * N + min(row) + min(col))
      } else {
        print(-1)
      }
    }
    
    评论

报告相同问题?