编程介的小学生 2019-10-03 20:09 采纳率: 0.4%
浏览 81

One-move checkmate 程序怎么写

Description

The chess endgame "a king and a queen versus a king" is known to be easy victory for the side that has a queen.

The position in such an endgame is described by the locations of three figures: the white king, the white queen, and the black king. Locations are written in a usual chess notation, constructed from a letter from "a" to "h", which determine vertical line, and the digit from 1 to 8, which determine horizontal line of the location.

Given the position described, you are find a move for the white queen by which the black king will be checkmated, or determine that no such a move exists.
Input

The input contains three two-character locations for the white king, white queen, and black king, in that order, separated by spaces.
The input position is a valid chess position for the white's turn, i.e. all figures occupy different spaces, kings are not located in the neighboring squares, and the black king is not under the check.
Output

Output file must contain the two-character location — destination square for the white queen's move. If checkmating move does not exist, output file must contain the string "no". If there is more than one checkmating move, output the one with the smallest vertical position then with the smallest horizontal postion.
Sample Input

a3 g2 a1
Sample Output

a2

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-30 02:32
    关注

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

    # read data
    positions <- readLines("input.txt")
    
    # define the function to check if the queen can be moved to the given square
    check_move <- function(square) {
      # get the king's coordinates
      king_coords <- positions[1]
      
      # create a list of possible moves for the queen
      possible_moves <- c(
        paste0(king_coords, "g1"), 
        paste0(king_coords, "c1"), 
        paste0(king_coords, "f1"), 
        paste0(king_coords, "e1"), 
        paste0(king_coords, "d1")
      )
      
      # iterate over each possible move
      for (move in possible_moves) {
        # if the move results in a checkmate, return true
        if (is_checkmate(move)) {
          return(TRUE)
        }
      }
      
      # if none of the moves resulted in a checkmate, return false
      return(FALSE)
    }
    
    # initialize variables
    best_square <- ""
    smallest_diff <- Inf
      
    # iterate over each square
    for (square in unique(strsplit(positions[-1], " ")[[1]])) {
      # if the current square is not already visited, try to move the queen there
      if (!grepl(square, best_square)) {
        # call the function to check if the queen can be moved there
        if (check_move(square)) {
          # update the best square and smallest difference if necessary
          if (length(best_square) == 0 || abs(length(square) - length(best_square)) < smallest_diff) {
            best_square <- square
            smallest_diff <- length(square) - length(best_square)
          }
        }
      }
    }
    
    # write the result to the output file
    writeLines(paste(" ", best_square), file = "output.txt")
    

    This code reads the input data from a text file, defines a function check_move to check if the queen can be moved to a given square, initializes variables to keep track of the best square found so far and the smallest difference between the number of rows and columns of the queen's new position and the best square, iterates over each square, checks if the queen can be moved there using the check_move function, updates the best square and smallest difference if necessary, and finally writes the result to the output file.

    评论

报告相同问题?