编程介的小学生 2019-12-31 09:29 采纳率: 0.2%
浏览 53

Gem Squares 程序是怎么写的呢

Problem Description
You are given a board with 8×8 squares. In each square, there can be either a colored gem or no gem at all. Gems with different colors are represented by different integers. It is guaranteed that there are no more than two consecutive gems with the same color either in a row or in a column, and that there is not any gem above a blank square.

........
........
........
........
........
..43366.
..121556
44212335

For two neighboring squares, you can exchange the gems.

........
........
........
........
........
..43366.
..111556
44222335

If there are more than two consecutive gems with the same color in a row or in a column after exchange, these gems will be taken away simultaneously. Note that a gem could be counted both in its row and in its column; refer to the sample test cases for details.

........
........
........
........
........
..43366.
.....556
44...335

If there is no gem under a gem, the gem will fall to the square below.

........
........
........
........
........
.....66.
.....556
44433335

After all gems have fallen down to the lowest place, the procedure will be repeated. If there are more than two gems with the same color in a row or in a column, these gems will be taken away simultaneously. Then some gems will fall to the squares below, if there are no gems under those gems.

........
........
........
........
........
.....66.
.....556
.......5

........
........
........
........
........
........
.....666
.....555

........
........
........
........
........
........
........
........

The procedure will be repeated until there is no gem that can be taken away.

Given a board with 8*8 squares, you task is to determine whether all gems can be taken away by a single exchange or not.

Input
The input consists of several test cases. Each test case will be eight lines, and each line contains eight characters. If in a square there is no gem, ‘.’ is used to identify it, otherwise an integer k is used to identify the gem’s color, 1≤k≤9.

There is a blank line between two consecutive test cases.

End of input is indicated by a line consisting of 0.

Output
For each test case, output a single line. If all gems can be taken away by a single exchange, output “Yes”; otherwise output “No”.

Sample Input
........
........
........
........
........
..43366.
..121556
44212335

........
........
........
.2......
.2.22...
.1.11...
.2.22...
.2.22...

12121212
21212121
12121212
21212121
12121212
21212121
12121212
21212121

........
........
........
........
........
...96...
...96...
.996966.

0

Sample Output
Yes
Yes
No
Yes

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-27 12:42
    关注

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

    # Gem Squares程序实现
    gem_squares <- function(board) {
      # 将字符串转换为整数矩阵
      board_matrix <- matrix(unlist(strsplit(board, "\n")), nrow = 8)
      
      # 检查是否有连续相同的颜色的两颗或更多
      has_consecutive_colors <- function(row, col) {
        prev_color <- NA
        consec_count <- 0
        
        for (i in 1:ncol(row)) {
          current_color <- row[i]
          
          if (current_color != prev_color && consec_count > 0) {
            return(TRUE)
          }
          
          if (current_color == prev_color) {
            consec_count <- consec_count + 1
          } else {
            prev_color <- current_color
            consec_count <- 1
          }
        }
        
        return(FALSE)
      }
      
      for (i in 1:7) {
        for (j in 1:7) {
          if (board_matrix[i][j] == " ") {
            # 如果是空白格,则检查上下左右是否相邻有相同颜色的两颗或更多
            left_row <- board_matrix[(i - 1):i, j]
            right_row <- board_matrix[(i + 1):i + 1, j]
            top_col <- board_matrix[i, (j - 1):j]
            bottom_col <- board_matrix[i, (j + 1):j + 1]
            
            if ((has_consecutive_colors(left_row, j) || 
                has_consecutive_colors(right_row, j) ||
                has_consecutive_colors(top_col, i) ||
                has_consecutive_colors(bottom_col, i))) {
              return(TRUE)
            }
          }
        }
      }
      
      return(FALSE)
    }
    
    # 测试用例
    test_cases <- list(
      c(".....", ".....", "..43366.", "..121556", "44212335"),
      c(".....", ".2......", ".2.22...", ".1.11...", ".2.22...", "12121212"),
      c(".....", "21212121", "12121212", "21212121", "12121212", "21212121"),
      c(".996966", ".......", ".......", ".......", ".......", "969696")
    )
    
    for (i in seq_along(test_cases)) {
      result <- gem_squares(test_cases[[i]])
      print(paste("Test Case", i+1, ": ", result))
    }
    

    这段代码实现了Gem Squares问题的R语言实现。它首先将输入的字符串转换为整数矩阵,并检查是否有连续相同的颜色的两颗或更多。如果存在这样的情况,则返回TRUE;否则,继续检查其他行和列。最后,输出每个测试用例的结果。

    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上