homework-02
先上代码
import sys def maxsum_h(num,y1,x1): temp=[0]*x1 s=0 a=-1000000 for q in range(y1): for i in range(y1): for j in range(i,y1): for k in range(x1): temp[k]+=int(num[(j+q)%y1][k]) if(s+temp[k]<temp[k]): s=0 s+=temp[k] if(a<s): a=s s=0 s=0 temp=[0]*x1 s=0 temp=[0]*x1 return a def maxsum_v(num,y1,x1): temp=[0]*x1 s=0 a=-1000000 for t in range(x1): for i in range(y1): for j in range(i,y1): for k in range(x1): temp[(k+t)%x1]+=int(num[j][(k+t)%x1]) if(s+temp[(k+t)%x1]<temp[(k+t)%x1]): s=0 s+=temp[(k+t)%x1] if(a<s): a=s s=0 s=0 temp=[0]*x1 s=0 temp=[0]*x1 return a def maxsum_vh(num,y1,x1): temp=[0]*x1 s=0 a=-1000000 for q in range(y1): for t in range(x1): for i in range(y1): for j in range(i,y1): for k in range(x1): temp[(k+t)%x1]+=int(num[(j+q)%y1][(k+t)%x1]) if(s+temp[(k+t)%x1]<temp[(k+t)%x1]): s=0 s+=temp[(k+t)%x1] if(a<s): a=s s=0 s=0 temp=[0]*x1 s=0 temp=[0]*x1 return a def maxsum(num,y1,x1): temp=[0]*x1 s=0 a=-1000000 for i in range(y1): for j in range(i,y1): for k in range(x1): temp[k]+=int(num[j][k]) if(s+temp[k]<temp[k]): s=0 s+=temp[k] if(a<s): a=s s=0 s=0 temp=[0]*x1 return a def searchthrough(x,y,num,now_sum): global max_sum,pointgroup,min_x,min_y,visited max_sum = max(max_sum, now_sum) for i in [[0,-1],[1,0],[0,1],[-1,0]]: if x+i[0]>=min_x and x+i[0]<n1 and y+i[1]>=min_y and y+i[1]<n2 and visited[(x+i[0])%n1,(y+i[1])%n2]==0 and [(x+i[0])%n1,(y+i[1])%n2,num[(x+i[0])% n1][(y+i[1])%n2]] not in pointgroup: pointgroup.append([(x + i[0]) % n1, (y + i[1]) % n2, num[(x + i[0]) % n1][(y + i[1]) % n2]]) if pointgroup == []: return pointgroup = sorted(pointgroup, key=lambda x: x[2]) nextpoint = pointgroup.pop() if now_sum + nextpoint[2] > 0: visited[nextpoint[0], nextpoint[1]] = 1 searchthrough(nextpoint[0],nextpoint[1],num,now_sum + nextpoint[2]) visited[nextpoint[0], nextpoint[1]] = 0 else: return def maxsum_a(num,x1,y1): global min_x,min_y,max_sum,visited min_x = 0 min_y = 0 max_sum = 0 now_sum = 0 startpointx = [] startpointy = [] pointgroup = [] for i in range(0,x1): for j in range(0,y1): visited[i,j] = 0 for i in range(0,x1): for j in range(0,y1): if num[i][j] > 0: startpointx.append(i) startpointy.append(j) for pointx in startpointx: pointy = startpointy.pop() visited[pointx, pointy] = 1 searchthrough(pointx,pointy,num,num[pointx][pointy]) return max_sum def maxsum_vha(num,x1,y1): global min_x,min_y,max_sum,visited min_x = -n1 min_y = -n2 max_sum = 0 now_sum = 0 startpointx = [] startpointy = [] pointgroup = [] for i in range(0,x1): for j in range(0,y1): visited[i,j] = 0 for i in range(0,x1): for j in range(0,y1): if num[i][j] > 0: startpointx.append(i) startpointy.append(j) for pointx in startpointx: pointy = startpointy.pop() visited[pointx, pointy] = 1 searchthrough(pointx,pointy,num,num[pointx][pointy]) return max_sum num=[] V=H=A=False if "\\v" in sys.argv[1:]: V=True if "\\h" in sys.argv[1:]: H = True if "\\a" in sys.argv[1:]: A = True filename=sys.argv[-1] f=open(filename,"r") l=f.readline() l=l.strip('\n').strip(",") x=l l=f.readline() l=l.strip('\n').strip(",") y=l for i in range(int(x)): for j in range(int(y)): l=f.readline() l=l.strip('\n').split(",") num.append(l) if V!=True and H!=True and A==True: max_sum=maxsum_a(num,int(x),int(y)) elif V==True and H!=True and A != True: max_sum = maxsum_v(num,int(x),int(y)) elif V!=True and H==True and A != True: max_sum = maxsum_h(num,int(x),int(y)) elif V==True and H==True and A != True: max_sum = maxsum_vh(num,int(x),int(y)) elif V==True and H==True and A == True: max_sum = maxsum_vha(num,int(x),int(y)) else: max_sum = maxsum(num,int(x),int(y)) print max_sum
首先,也是最简单的情况,即不存在相连,也不存在连通的情况。
这种情况我在第一次作业中已经说明。二维的情况就是讲二维转换为一维。即将n行m列的二维数组转换为n^2个长度为m的一维数组。时间复杂度为O(m*n^2)
然后我们考虑上下相连的情况,原矩阵若是可以上下相连则等同于有n个不同的二维数组。我们仅需进行计算n次二维数组的最大字串和,时间复杂度为O(m*n^3)
左右相连同理,时间复杂度为O(m^2*n^3)
然后就到了最让人觉得恼火的连通情况了。虽然说状态压缩动态规划可能是对这个问题的一个很好的解决方案,但是我不会啊!所以就采用了跟其他同学一样的暴力算法,对于一个m*n的矩阵,时间复杂度就要有O(2^(m+n)),可想而知效率如何。所能做的事情就是不断的深度搜索。
这次作业除了连通情况外,其他情况均很好解决。并且充分体现出了python效率高的特点,但是还是写出来的python代码还是有C代码的直视感。并且觉得如果一直是这样难度的作业的话,很难去用不是很熟悉的编程语言来完成。
最后是项目时间表格
|
PSP2.1 |
Personal Software Process Stages |
Time (%) Senior Student |
|
Planning |
计划 |
4 |
|
· Estimate |
· 估计这个任务需要多少时间 |
4 |
|
Development |
开发 |
80 |
|
· Analysis |
· 需求分析 (包括学习新技术) |
10 |
|
· Design Spec |
· 生成设计文档 |
0 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
0 |
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
0 |
|
· Design |
· 具体设计 |
10 |
|
· Coding |
· 具体编码 |
40 |
|
· Code Review |
· 代码复审 |
10 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
10 |
|
Reporting |
报告 |
16 |
|
3 |
|
|
3 |
|
|
10 |
浙公网安备 33010602011771号