RockDrake

a016 in numpy

Aug 17th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. import sys
  2. import numpy as np
  3. def hLine():        #橫列
  4.     hCounter = 0
  5.     for i in range(0,9):
  6.         hList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  7.        
  8.         for j in range(0,9):
  9.             if np1[i][j] in hList:
  10.              hList.remove(np1[i][j])
  11.        
  12.         if not hList:
  13.             hCounter += 1
  14.     if hCounter == 9:        # 9列都有1~9的數字,回傳確認訊號
  15.         return 'yes'
  16.  
  17.  
  18. def sLine():        #直行
  19.     sCounter = 0
  20.     for i in range(0,9):
  21.         sList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  22.        
  23.         for j in range(0,9):
  24.             if np1[j][i] in sList:
  25.                 sList.remove(np1[j][i])
  26.        
  27.         if not sList:
  28.             sCounter += 1
  29.        
  30.     if sCounter == 9:        # 9行都有1~9的數字,回傳確認訊號
  31.         return 'yes'
  32.  
  33.  
  34. def sArea():        #3x3區域
  35.     temp = []
  36.     aCounter = 0
  37.     for i in range(0,8,3):
  38.         for j in range(0,8,3):
  39.             temp = []
  40.             temp = np1[i:i+3,j:j+3]
  41.             aList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  42.             for k in range(0,3):
  43.                 for m in range(0,3):
  44.                     if temp[k][m] in aList:
  45.                         aList.remove(temp[k][m])
  46.             print(aList)
  47.             if not aList:
  48.                 aCounter += 1
  49.             print(aCounter)
  50.     if aCounter == 9:        # 9區3X3都有1~9的數字,回傳確認訊號
  51.         return 'yes'
  52.  
  53. # 主程式
  54. for s in sys.stdin:
  55.     np1 = np.zeros((9,9))        # 宣告
  56.  
  57.     for j in range(0,9):
  58.         if j == 0:
  59.             a0, b0, c0, d0, e0, f0, g0, h0, i0 = s.split()        # 輸入第一筆至陣列
  60.             np1[j][0] = a0
  61.             np1[j][1] = b0
  62.             np1[j][2] = c0
  63.             np1[j][3] = d0
  64.             np1[j][4] = e0
  65.             np1[j][5] = f0
  66.             np1[j][6] = g0
  67.             np1[j][7] = h0
  68.             np1[j][8] = i0
  69.         elif j != 0:
  70.             a, b, c, d, e, f, g, h, i = input().split()        # 輸入其他筆至陣列
  71.             np1[j][0] = a
  72.             np1[j][1] = b
  73.             np1[j][2] = c
  74.             np1[j][3] = d
  75.             np1[j][4] = e
  76.             np1[j][5] = f
  77.             np1[j][6] = g
  78.             np1[j][7] = h
  79.             np1[j][8] = i
  80.    
  81.     # 呼叫副程式並輸出
  82.     if hLine() == sLine() == sArea() =='yes':
  83.         print('yes')
  84.     else:
  85.         print('no')
  86.  
  87.     # Debug區域
  88.     # print('hLine = ' + str(hLine()))
  89.     # print('sLine = '+ str(sLine()))
  90.     # print('sArea = ' +  str(sArea()))
  91.     # break
Advertisement
Add Comment
Please, Sign In to add comment