# -*- coding: utf-8 -*- #Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45) #CodeIQã®Pythonã§å³¶ã®æ°ãã«ãŠã³ããããã£ãŠããåé¡ã #https://codeiq.jp/ace/joboffer_apli/q377 #æåã«èªåã§èããã¢ã«ãŽãªãºã ã #ã¢ã«ãŽãªãºã ãã®1:import_islandãisland_countã__joinlist #ã§ããããã¯é£æ¥ãªã¹ãããç©éåãšåéåã䜿ã£ãŠå³¶ã®æ°ãã«ãŠã³ããããã§ãã #çœç³ãšé»ç³ã®æ°ãå¢ãããšæ¥µç«¯ã«ããã©ãŒãã³ã¹ãäœäžããŸãã #ããã§ã€ã³ã¿ãŒãããã®åãåããŠå床èããã¢ã«ãŽãªãºã ã #ã¢ã«ãŽãªãºã ãã®2:import_island2ãisland_count2ã__check_replace #ãšãªã£ãŠããŸãã #ãã®ã¢ã«ãŽãªãºã 2ã¯ã¢ã«ãŽãªãºã 1ãšéã£ãŠç³ã®æ°ãå
šéšèµ°æ»ããã°çµããã®ã§ç·åœ¢æéã§çµãããŸãã #ããã©ãŒãã³ã¹æ¯èŒçµæïŒ #ç³ã®ç·æ°:100*100=10000ãé»ç³ã®æ°:4987ãå³¶ã®æ°:703 #import_islandã®å®è¡æé:24ç§227035 #import_island2ã®å®è¡æé:0ç§066305 #ããããã¯ã©ã¹ã®æžãæ¹ã«èªä¿¡ããªãã§ãããã #ããã¹ããã¡ã€ã«ã¯èªåã§çšæããŠãã ããã import math, datetime class IslandCount(object): def __init__(self,ff='sample_island1.txt'): self.ff=ff #çœç³0é»ç³1ã§æç»ããããã¹ããã¡ã€ã« #ç³ãæç»ãããçœ:0ãé»:1ã def draw_island(self,c=[],m=0,n=0): list1=[] b='' for i in range(m*n): list1.append('0') for i in c: list1[i-1]='1' for i in range(m-1): list1.insert((i+1)*(n+1)-1,'\n') for i in range(len(list1)): b+=str(list1[i]) print b ''' ã¢ã«ãŽãªãºã ãã®1ãããã ''' #txtãã¡ã€ã«ãèªã¿èŸŒãå Žåã«äœ¿çšããã def import_island(self,f=''): f=self.ff flist=[] sindex=[] j=0 flg=0 f=self.ff f=open(f).read() for i in f: j+=1 if i!='\n': flist.append(i) else: if flg==0: flg=1 clmns=j-1 lns=len(flist)/clmns for i, v in enumerate(flist): if v=='1': sindex.append(i+1) return self.island_count(sindex,lns,clmns) #å³¶ã®æ°ãè¿ãã def island_count(self,a=[],m=0,n=0): count=0 join_list2=[] new_list=[] flist=[] l=len(a) newlist2=[] #n=int(math.sqrt(m)) #é»ç³ã®1æ¹å飿¥ãªã¹ããçæããã #äŸ:[1,2],[2,3],[2,8] for i in range(l-1): for j in range(l-1): if a[j+1]-a[i]==1 and a[i]%n!=0 or a[j+1]-a[i]==n: join_list2+=[[a[i],a[j+1]]] #é»ç³ã®é£æ¥ãªã¹ãã§çµåã§ãããã®ãçµåããã #äŸ:[1,2,3,8] for i in range(len(join_list2)): for j in range(len(join_list2)): if join_list2[i][1]==join_list2[j][0]: join_list2[i]=join_list2[i]+join_list2[j][1:] for i in range(len(join_list2)): join_list2[i]=sorted(join_list2[i]) #çµåãã飿¥ãªã¹ãããå³¶ãªã¹ããè¿ãã #äŸ:[1,2,3,8],[7,8,9,10],[3,4,10]â[1,2,3,4,7,8,9,10] newlist2=self.__joinlist(join_list2) #å³¶ãªã¹ããçµåããã #[[6, 12], [7, 13]]â[6, 12, 7, 13] for i in newlist2: flist+=i #é»ç³ã®æ°-çµåããå³¶ãªã¹ãã®èŠçŽ æ°=é»ç³1ã€ã ãã®å€å³¶ #æ±ãããå³¶ã®æ°=å€å³¶+å³¶ãªã¹ãã®æ°(å€å³¶ã§ã¯ãªãå³¶ã®æ°) count=l-len(flist)+len(newlist2) '''æç»ãäžèŠãªå Žåã¯1è¡äžã®ã®ã³ã¡ã³ãåããŠãã ããã''' self.draw_island(a,m,n) return count #é»ç³ã®é£æ¥ãªã¹ãå士ã«ãŠç©éåãååšããå Žåãåéåãçæãéè€ãåé€ããã def __joinlist(self,join_list2): newlist=[] flg=0 for i in range(len(join_list2)): for j in range(len(join_list2)): #èªåèªèº«ä»¥å€ã§ç©éåã空ã§ãªãå Žå if i!=j and join_list2[j]!=[]: if len(set(join_list2[i])&set(join_list2[j]))>0: #åéå flg=1 join_list2[i]=list(set(join_list2[i])|set(join_list2[j])) join_list2[j]=[] #éè€ãåé€ããã for line in join_list2: if sorted(line) not in newlist and line!=[]: newlist.append(sorted(line)) #ç©éåããã¹ãŠç©ºã®å Žåãååž°ãæããã if flg==0: return newlist return self.__joinlist(newlist) ''' ã¢ã«ãŽãªãºã ãã®1ãããŸã§ ''' ''' ã¢ã«ãŽãªãºã ãã®2ãããã ''' #txtãã¡ã€ã«ãèªã¿èŸŒãå Žåã«äœ¿çšãããPart2 def import_island2(self,f=''): f=self.ff linescount=0 c=[] for line in open(f): b=[] columnscount=0 for char in line: if char.strip()!='': columnscount+=1 b+=[int(char.strip())] linescount+=1 c+=[b] return self.island_count2(c,linescount,columnscount) #å³¶ã®æ°ãè¿ããPart2 def island_count2(self,c,m,n): r=[0,0] for i in range(m): for j in range(n): if c[i][j] != -1: #c[i][j]==0ã®å Žåãr[0]+=1 #c[i][j]==1ã®å Žåãr[1]+=1 r[c[i][j]] += 1 #äžã§+1ããåŸãc[i][j]ã®äžäžå·Šå³ããã§ãã¯ããŠããåå€ã®å Žå-1ã«çœ®æãã #å³ã¡æå®ããc[i][j]ãšåãå³¶ã¯ã«ãŠã³ãã«+1åŸããã¹ãŠ-1ã«çœ®æãããæ¬¡ã®ãã§ãã¯ã§ã«ãŠã³ããããªã self.__check_replace(c,i,j,m,n) return r[1] #é»ç³ã®å³¶ã®æ°ãè¿ããçœç³ã¯r[0] #ãã§ãã¯ããèŠçŽ ãšé£æ¥ããåå€ã®èŠçŽ ã-1ã§ååž°çã«çœ®æããŠããã def __check_replace(self,c,i,j,m,n): k = c[i][j] c[i][j] = -1 #2è¡ç®ä»¥éã§äžã®ç³ãåãå Žåãäžã®ç³ã«å¯ŸããŠååž° if i>0 and c[i-1][j]==k: self.__check_replace(c,i-1,j,m,n) #2åç®ä»¥éã§å·Šã®ç³ãåãå Žåãå·Šã®ç³ã«å¯ŸããŠååž° if j>0 and c[i][j-1]==k: self.__check_replace(c,i,j-1,m,n) #æçµè¡ããåã®è¡ã§äžã®ç³ãåãå Žåãäžã®ç³ã«å¯ŸããŠååž° if i self.__check_replace(c,i+1,j,m,n) #æçµåããåã®åã§å³ã®ç³ãåãå Žåãå³ã®ç³ã«å¯ŸããŠååž° if j self.__check_replace(c,i,j+1,m,n) ''' ã¢ã«ãŽãªãºã ãã®2ãããŸã§ ''' if __name__=='__main__': ''' a1:é»ç³ã®ã€ã³ããã¯ã¹ b1,c1:瞊ãã¹ã®æ°,暪ãã¹ã®æ° f1-f4:é»ç³ã1çœç³ã0ã§èšè¿°ããããã¹ããã¡ã€ã« ''' a1=[6,7,12,13] b1,c1=6,6 f1='sample_island1.txt' a2=[6,11,12,17] b2,c2=6,6 f2='sample_island2.txt' a3=[1,2,3,4,7,8,9,10,12,13,24,25,29,30,31,35,36] b3,c3=6,6 f3='sample_island3.txt' a4=[1,2,3,4,7,8,9,10,12,13,24,25,29,30,31,35,36] b4,c4=7,7 f4='sample_island_100_100.txt' aaa=IslandCount(f1) #print 'å³¶ã®æ°:'+str(aaa.island_count(a1,b1,c1)) #é»ç³ã®ã€ã³ããã¯ã¹æ°ãšç³ã®ç·æ°ããå³¶ã®æ°ãè¿ãã t1=datetime.datetime.now() print 'å³¶ã®æ°:'+str(aaa.import_island())+' #ã¢ã«ãŽãªãºã 1' #filenameã§æå®ããããã¡ã€ã«ããå³¶ã®æ°ãè¿ãã1çªç®ã®ã¢ã«ãŽãªãºã t2=datetime.datetime.now() print str(t2-t1)+'ç§' t1=datetime.datetime.now() print 'å³¶ã®æ°:'+str(aaa.import_island2())+' #ã¢ã«ãŽãªãºã 2' #filenameã§æå®ããããã¡ã€ã«ããå³¶ã®æ°ãè¿ãã2çªç®ã®ã¢ã«ãŽãªãºã t2=datetime.datetime.now() print str(t2-t1)+'ç§' print '===========================================================================================' bbb=IslandCount(f2) #print 'å³¶ã®æ°:'+str(bbb.island_count(a2,b2,c2)) t1=datetime.datetime.now() print 'å³¶ã®æ°:'+str(bbb.import_island())+' #ã¢ã«ãŽãªãºã 1' t2=datetime.datetime.now() print str(t2-t1)+'ç§' t1=datetime.datetime.now() print 'å³¶ã®æ°:'+str(bbb.import_island2())+' #ã¢ã«ãŽãªãºã 2' t2=datetime.datetime.now() print str(t2-t1)+'ç§' print '===========================================================================================' ccc=IslandCount(f3) #print 'å³¶ã®æ°:'+str(ccc.island_count(a3,b3,c3)) t1=datetime.datetime.now() print 'å³¶ã®æ°:'+str(ccc.import_island())+' #ã¢ã«ãŽãªãºã 1' t2=datetime.datetime.now() print str(t2-t1)+'ç§' t1=datetime.datetime.now() print 'å³¶ã®æ°:'+str(ccc.import_island2())+' #ã¢ã«ãŽãªãºã 2' t2=datetime.datetime.now() print str(t2-t1)+'ç§' print '===========================================================================================' ddd=IslandCount(f4) #print 'å³¶ã®æ°:'+str(ddd.island_count(a4,b4,c4)) t1=datetime.datetime.now() print 'å³¶ã®æ°:'+str(ddd.import_island())+' #ã¢ã«ãŽãªãºã 1' t2=datetime.datetime.now() print str(t2-t1)+'ç§' t1=datetime.datetime.now() print 'å³¶ã®æ°:'+str(ddd.import_island2())+' #ã¢ã«ãŽãªãºã 2' t2=datetime.datetime.now() print str(t2-t1)+'ç§' print '==========================================================================================='