以下回答参考 皆我百晓生、券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。
# read data
data <- read.table(header = TRUE, text = Sys.getenv("stdin"))[,c(1:2)]
for(i in 3:nrow(data)) {
if(data[i,1] != "") {
data[[i]] <- paste(data[[i]], collapse = " ")
}
}
data <- cbind(data[1], data)
# generate Zergling positions
zerglings <- matrix(NA, nrow = length(data), ncol = 4)
for(i in 1:length(data)) {
zerglings[i,] <- paste(data[i,2], data[i,3], collapse = "")
}
# generate Zergling attacks
attacks <- matrix(NA, nrow = length(data), ncol = 4)
for(i in 1:length(data)) {
attacks[i,] <- paste(data[i,4], data[i,5], collapse = "")
}
# initialize game map
map <- matrix(rep(c(' ', '.', '#'), times = length(zerglings)), nrow = 4, ncol = 4)
# simulate Zergling rush
for(t in seq_along(attacks)) {
# find directions for Zerglings to move
directions <- sapply(zerglings, function(x) {
if(str_detect(x, "^.*north.*$") || str_detect(x, "^.*south.*$"))
return("north")
else if(str_detect(x, "^.*east.*$") || str_detect(x, "^.*west.*$"))
return("east")
else if(str_detect(x, "^.*south.*$") || str_detect(x, "^.*northeast.*$"))
return("south")
else if(str_detect(x, "^.*west.*$") || str_detect(x, "^.*southeast.*$"))
return("west")
else
return("none")
})
# calculate new Zergling positions based on directions
zerglings <- apply(zerglings, 1, function(x) {
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*east.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*northeast.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*southwest.*$", "", x)
x <- gsub("^.*northwest.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*northeast.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^.*north.*$", "", x)
x <- gsub("^.*southeast.*$", "", x)
x <- gsub("^.*west.*$", "", x)
x <- gsub("^.*south.*$", "", x)
x <- gsub("^