编程介的小学生 2020-02-12 21:26 采纳率: 0.4%
浏览 144

Science! 科学的问题

Problem Description
Welcome, ladies and gentlemen, to Aperture Science. Astronauts, War Heroes, Olympians — you’re here because we want the best, and you are it. That said, it’s time to make some science.
Now, I want each of you to stand on one of these buttons. Well done, we’re making great progress here. Now let’s do it again. Oh, come on - don’t stand on the same button! Move, people! No, no, that button’s only for the Astronauts, you know who you are. What?! You say you can’t do everything I ask? Ok let’s start over. You there, the Programmer, figure out how many times we can do this. And make it quick, we have a lot more science to get through…

Input
There will be several test cases in the input. The first line of each case will contain n (2≤n≤80) giving the number of people (and the number of buttons) in the experiment. The next n lines will contain n characters each. If the jth character of the ith line is Y it indicates that the ith person can stand on the jth button (it is N otherwise). The last line of input will be a 0.

Output
For each test case, output k, the maximum number of times everyone can be standing on buttons such that nobody stands on the same button more than once (This might be 0). After that, output k lines. Each line should contain n integers separated by single spaces, where the ith integer describes which person is standing on the ith button. All of the lines should be valid and none of them should put the same person on the same button as a previous line of the same test case. Output no extra spaces, and do not separate answers with blank lines. Note that correct outputs might not be unique.

Sample Input
3
YYY
NYY
YNY
2
YN
YN
0

Sample Output
2
3 1 2
1 2 3
0

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-25 22:21
    关注

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

    # read data
    data <- read.table(header=TRUE, text="YYY\nNYY\nYNY", sep="")
    
    # create result matrix
    res <- matrix(NA, nrow=length(data), ncol=length(data[1]))
    
    # loop over all combinations of rows
    for(i in 1:nrow(data)) {
      # initialize counter 
      count <- 0
      
      # iterate over all columns 
      for(j in 1:ncol(data[i])) {
        # check if current column has been visited before
        if(res[i,j] == NA) {
          # if not, increment counter
          res[i,j] <- count + 1
          count <- count + 1
        }
      }  
    }
    
    # write results
    write.table(res, file="output.txt")
    

    这段代码首先读取数据,并将其存储在名为data的表中。然后,它创建了一个结果矩阵,用于存储每个测试用例的结果。

    接下来,它使用一个嵌套循环遍历所有可能的行和列组合。对于每个组合,它初始化计数器(count),并检查当前列是否已经访问过。如果还没有,则将计数器加一,并记录当前列的位置。

    最后,它将结果写入文件output.txt

    评论

报告相同问题?