编程介的小学生 2019-12-30 21:48 采纳率: 0.2%
浏览 82

Mine sweeping 程序的问题

Problem Description
I think most of you are using system named of xp or vista or win7.And these system is consist of a famous game what is mine sweeping.You must have played it before.If you not,just look the game rules followed.

There are N*N grids on the map which contains some mines , and if you touch that ,you lose the game.If a position not containing a mine is touched, an integer K (0 < =K <= 8) appears indicating that there are K mines in the eight adjacent positions. If K = 0, the eight adjacent positions will be touched automatically, new numbers will appear and this process is repeated until no new number is 0. Your task is to mark the mines' positions without touching them.

Now, given the distribution of the mines, output the numbers appearing after the player's first touch.

Input
The first line of each case is two numbers N (1 <= N <= 100) .Then there will be a map contain N*N grids.The map is just contain O and X.'X' stands for a mine, 'O' stand for it is safe with nothing. You can assume there is at most one mine in one position. The last line of each case is two numbers X and Y(0<=X<N,0<=Y<N, indicating the position of the player's first touch.

Output
If the player touches the mine, just output "it is a beiju!".

If the player doesn't touch the mine, output the numbers appearing after the touch. If a position is touched by the player or by the computer automatically, output the number. If a position is not touched, output a dot '.'.

Output a blank line after each test case.

Sample Input
5
OOOOO
OXXXO
OOOOO
OXXXO
OOOOO
1 1
5
OOOOO
OXXXO
OOOOO
OXXXO
OOOOO
0 0

Sample Output
it is a beiju!

1....
.....
.....
.....
.....

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-27 15:40
    关注

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

    # read input
    input <- readLines()
    
    # split into lines
    lines <- unlist(strsplit(input[1], "\n"))
    
    # parse the grid into matrix
    grid <- as.matrix(lines)
    
    # get coordinates of the player's first touch
    x <- as.integer(substr(input[2], 1, 3))
    y <- as.integer(substr(input[3], 1, 3))
    
    # create a matrix with all zeros except the cells in the grid
    # where the player's first touch was located
    mask <- matrix(rep(0, nrow(grid)), ncol=ncol(grid), nrow=nrow(grid)) 
    mask[x,y] <- 1 
    
    # find the locations of the mines
    mines <- apply(mask, 1, function(x) sum(x == 1))
    
    # create a vector to store the numbers after the player's first touch
    result <- rep("", length(mines))
    
    # loop over all positions in the grid
    for(i in 1:nrow(grid)){
      for(j in 1:ncol(grid)){
        # check if the current position is a mine
        if(mask[i,j] == 1){
          result[mekeep(mines)] <- c(result[mekeep(mines)], paste("it is a beiju!", sep=""))
        } else {
          result[mekeep(mines)] <- c(result[mekeep(mines)], paste("", sep=""))
        }
      }
    }
    
    # print the results
    print(result)
    

    The code reads the input from stdin, splits it into lines, and then parses the grid into a matrix. It finds the coordinates of the player's first touch and creates a mask matrix with all zeros except for the cells in the grid where the player's first touch was located. Then it loops over all positions in the grid and checks if they are mines. If they are, it adds their location to the result vector along with the message "it is a beiju!". Otherwise, it adds their location to the result vector along with a blank string. Finally, it prints the resulting vector.

    评论

报告相同问题?

悬赏问题

  • ¥20 看图片)删除这个自动化录屏脚本就一直报错找不到脚本文件,如何解决?(相关搜索:bat文件)
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集