python 代码优化 时间_优化python代码,减少分配/释放时间

我试图从某个网站解决编程挑战问题,我不想在这里提及.

问题如下:

There is an square board with N*N points. An insect starts out at a particular point (x1,y1). It can jump to any point (x2,y2) if |x1-x2|+|y1-y2| <= S. Also, some points contain water on which the insect cannot jump. How many different paths can it take in M jumps? Note that it can remain at the same point by jumping in place.

给出了整数N,S,M,初始板配置也是如此.

我很确定我的解决方案是正确的,可以通过归纳证明.我将电路板转换为图形(邻接列表),其中点之间的边缘使得有效的昆虫跳跃.然后它只需要迭代M次并更新路径计数.

我主要担心的是代码需要进行优化,以便它可以在多个测试用例上工作,而无需分配/解除分配太多次,这会减慢运行时间.如果有人能在算法本身内建议优化,那也很棒.

谢谢!

import sys

#The board on which Jumping insect moves.

#The max size in any test case is 200 * 200

board = [['_']*200 for j in xrange(200)]

#Graph in the form of an adjancency list created from the board

G = [list() for i in xrange(200*200)]

def paths(N,M,S):

'''Calculates the total number of paths insect takes

The board size is N*N, Length of paths: M,

Insect can jusp from square u to square v if ||u-v|| <=S

Here ||u-v|| refers to the 1 norm'''

# Totals paths are modulo 1000000007

MOD = 1000000007

# Clearing adjacency list for this testcase

for i in xrange(N*N): del(G[i][:])

s = -1 #Starting point s

#Creating G adjacency list

# Point 'L' represents starting point

# Point 'P' cannot be accessed by the insect

for u in xrange(N*N):

x1, y1 = u/N, u%N

if board[x1][y1] == 'L': s = u

elif board[x1][y1] == 'P': continue

for j in xrange(S+1):

for k in xrange(S+1-j):

x2, y2 = x1+j, y1+k

if x2 < N and y2 < N and not board[x2][y2] == 'P':

v = x2*N+y2

G[u].append(v)

if not u == v: G[v].append(u)

if j > 0 and k > 0:

x2, y2 = x1+j, y1-k

if x2 < N and y2 >= 0 and not board[x2][y2] == 'P':

v = x2*N+y2

G[u].append(v)

G[v].append(u)

# P stores path counts

P = [[0 for i in xrange(N*N)] for j in xrange(2)]

# Setting count for starting position to 1

P[0][s] = 1

# Using shifter to toggle between prev and curr paths

shifter, prev, curr = 0, 0, 0

# Calculating paths

for i in xrange(M):

prev, curr = shifter %2, (shifter+1)%2

#Clearing Path counts on curr

for i in xrange(N*N): P[curr][i] = 0

for u in xrange(N*N):

if P[prev][u] == 0: continue

for v in G[u]:

P[curr][v] = (P[curr][v]+P[prev][u]) % MOD

shifter = (shifter+1)%2

return (sum(P[curr])%MOD)

#Number of testcases

num = int(sys.stdin.readline().split()[0])

results = []

# Reading in test cases

for i in xrange(num):

N, M, S = [int(j) for j in sys.stdin.readline().split()]

for j in xrange(N):

board[j][:N] = list(sys.stdin.readline().split()[0])

results.append(paths(N,M,S))

for result in results:

print result

这是numpy有用的东西,尽管你可以通过使用数组和结构模块来管理这个特定的用例.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值