競合の問題注(2):切断スタンプ

12干支スタンプリアン12一緒があります。

今、あなたは5にカットをしたい、要件が添付されなければなりません。

あなたは合計をクリッピングするどのように多くの異なる方法を計算します。

 

奇妙な擬似関数型プログラミング作業が深い検索を行うには問題外が深い検索でネスト始めたと、動作速度が極端に遅くなり、最終的にそれを取り除くための順列と組み合わせの公式リポジトリと、時間を見てする必要があります全体の配置の公式リポジトリを書き込む方法である参照

デフ  is_connect パス ):
    パス化= []

     デフ  DFS P パス ):
         非ローカル  パス化
          D  における  [( 0 1 )、( 1 0 )、( - 1 0 )、( 0 、 - 1 )]:
            NPX = P [ 0 ] + D [ 0 ]
            NPY = P [ 1 ] + D [ 1 ]
             if npx >= 0  and npx< 3  and npy>= 0  and npy< 4:
                
                 if (npx,npy)  in path:
                     if (npx,npy)  not  in pathed:
                        pathed.append((npx,npy))
                        dfs((npx,npy),path)
                             
     for p  in path:
        dfs(p,path)
         break
    
     if  len(pathed)== len(path):
         return  True
     else:
         return  False
        
import copy
import itertools
stamp=[[ 0, 1, 2, 3],
       [ 4, 5, 6, 7],
       [ 8, 9, 10, 11]]

last_path=[]
nums= 0

#注意第一个for在外层
position=[(x,y) for x  in  range( 3for y  in  range( 4)]

for perm  in itertools.combinations( range( 12), 5):
     #一维转二维
    elem_p=[(e// 4,e% 4for e  in perm]
 
     if is_connect(elem_p):
        last_path.append(elem_p)
        nums+= 1
         print(nums)

おすすめ

転載: www.cnblogs.com/shitianfang/p/12405839.html