题目来源: https://csacademy.com/ieeextreme-practice/tasks
为什么分类Ad Hoc专区,Ad hoc是什么意思
Ad hoc 是一个拉丁文常用短语。这个短语的意思是“特设的、特定目的的(地)、即席的、临时的、将就的、专案的”。这个短语通常用来形容一些特殊的、不能用于其它方面的,为一个特定的问题、任务而专门设定的解决方案。这个词汇须与 a priori 区分。
这个短语通常用来形容的东西包括一些为一个特别问题而设立的国家级或者国际性的组织、组委会以及合约等。在其他方面,譬如特殊环境下所组建的军队单位、手工制作的服装、临时搭建的网络协议或者是满足特殊条件的方程式等也可使用这个短语来进行形容。
Ad hoc 同时也包含有负面的评价,表明所形容的事物是一个权宜之计、不周密的计划或者是一个即兴举办的活动。
所以猜测应该是指这道题属于其他分类的意思,如果不对欢迎指正
原题
Game of Life
Time limit: 1200 ms
Memory limit: 128 MBYou are asked to make an implementation of the game of life by John Horton Conway on a finite torus board and provide its output after c iterations.
Standard input
On the first line of the input there will be three integers n, m (1≤n,m≤25) and c (1≤c≤10^7). n and m give the size of the board and c gives the number of iterations that you must simulate.
On the following n lines there will be m characters, either
*
or-
, each one representing the value of each cell of the board.*
represents a populated cell and-
an unpopulated one.Please note that the bottom neighbours of the last line are cells in the top line, and the left neighbours of the first column are the cells of the last column.
Standard output
On the output there should be n lines of m characters each, which represent the state of the board after c iterations.
Input Output 4 6 3 ------ ------ ------ -***--
--*--- ------ --*--- --*---
5 6 1234 ------ --*--- ---*-- -***-- ------
-----* ---*-* ----** ------ ------
7 9 5 --------- --------- --------- --*****-- --------- --------- ---------
----*---- ---***--- --*-*-*-- -***-***- --*-*-*-- ---***--- ----*----
4 6 1 -----* --*--- *----- -***-*
**-**- ------ *--*-- -**-**
思路
该题与leetcode 289很像,故参考leetcode289的一个思路
大概分为以下3步:
- 将初始状态的*和-转换为数值1和0,方便后面的卷积;
- 进行padding,并进行c次卷积,每次分别根据要求进行判断;
- 卷积后的结果即为所求。
代码
# a simple parser for python. use get_number() and get_word() to read
def parser():
while 1:
data = list(input().split(' '))
for number in data:
if len(number) > 0:
yield(number)
input_parser = parser()
def get_word():
global input_parser
return next(input_parser)
def get_number():
data = get_word()
try:
return int(data)
except ValueError:
return float(data)
# numpy and scipy are available for use
import numpy as np
import scipy
import pdb
r = get_number() # rows
c = get_number() # cols
n = get_number() # num of iters
board = np.zeros([r,c])
for i in range(r): # 读入n行
row_i = input()
row_i = row_i.replace('-','0')
row_i = row_i.replace('*','1')
row_i = list(row_i)
for j in range(len(row_i)):
row_i[j] = int(row_i[j])
board[i] = row_i
# pdb.set_trace()
# 下面两行做 zero padding
# 做3次
for iters in range(n):
board_exp = np.array([[0 for _ in range(c + 2)] for _ in range(r + 2)])
board_exp[1:1 + r, 1:1 + c] = np.array(board)
# 题目所要求的padding规则
board_exp[0,1:1 + c] = board[r-1,:]
board_exp[r+1,1:1 + c] = board[0,:]
board_exp[1:1 + r,0] = board[:,c-1]
board_exp[1:1 + r,c+1] = board[:,0]
# 设置卷积核
kernel = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])
# 开始卷积
for i in range(1, r + 1):
for j in range(1, c + 1):
# 统计细胞周围 8 个位置的状态
temp_sum = np.sum(kernel * board_exp[i - 1:i + 2, j - 1:j + 2])
# 按照题目规则进行判断
if board_exp[i, j] == 1:
if temp_sum < 2 or temp_sum > 3:
board[i - 1][j - 1] = 0
else:
if temp_sum == 3:
board[i - 1][j - 1] = 1
# print(board)
board = board.tolist()
# pdb.set_trace()
for i in range(r):
# 对每一行
for j in range(c):
board[i][j] = str(int(board[i][j]))
line_i = "".join(board[i])
line_i = line_i.replace('0','-')
line_i = line_i.replace('1','*')
print(line_i)
结果
可是结果还是有很多不对,也不知道原因是什么,padding的规则既按题目说的padding了最后一行和第一列,也试着上下左右全部padding了,但是都不对。而且时间也不达标。目前的结果如下:
仅得18分/100。可太难了,这样下去必然会坑队友的
ps: 赶论文也不能坑队友,立下flag: 10.3~10.22每天至少1道IEEE xtreme练习, 坐等打脸
pss: 这道题属于10.3的题