적목 퍼즐 회사의 조합 전 모델 계산

칸파니레(네프회사)라는 블록 퍼즐에 대해python을 사용하여 조합에 대해 전모드로 계산했다.
단지 흥미를 가지고 조사해 보았기 때문에 실용적인 내용은 아니다.

단계


1. 고찰, 공정도 제작
2. 인코딩 방법
3. 코드
4. 결과 출력

1. 고찰, 공정도 제작


고찰한 부분은 다른 기사에서 소개합니다여기.
고찰을 통해 우리는 아래의 절차도를 얻었다.

F의 완전한 상태를 보면 모든 색의 화살표를 하나하나 사용하여 F로 돌아가는 모드의 조합을 구할 수 있다.

2. 인코딩 방법


최근에 atcoder에서 배운 DFS(깊이 우선 탐색)를 사용한다면 BFS(폭 우선 탐색)는 간단하게 할 수 있는 것이 아니라고 생각합니다.
같은 색깔의 화살표는 한 번만 사용할 수 있는 제한이 있어서 좋은 방법이 생각나지 않는다.
이번에는 짙은 파란색, 파란색, 짙은 녹색, 녹색, 노란색, 짙은 빨간색, 연한 빨간색, 오렌지 등 8가지 색깔의 사용 순서를 검사했다.
짙은 빨간색, 녹색은 특정한 상황에서 두 가지 퇴적 방법이 있기 때문에 이런 경우에도 분리해야 한다.
엑셀을 처음 쓰는 거라 아주 간단한 내용이지만 많이 배웠어요.

3. 코드


Campanile.py
from itertools import permutations

colorlist=["濃青","青","濃緑","緑","黄緑","濃赤","薄赤","橙"]#使用する積み木の色のリストです。
flowchart = [
    [-1, 3, 1,-1,-1,-1,-1,-1],
    [-1,-1, 0, 3, 2, 4, 1,-1],
    [-1,-1,-1, 3, 1, 4, 2, 3],
    [ 4,-1,-1,-1, 3, 1,-1,-1],
    [-1, 0,-1, 1, 4,-1,-1, 2]
    ]#[i][j]について、iはA~Eの状態を0~4で、jはcolorlistの順、flowchart[i][j]は積み木を重ねた後の状態を0~4で表している
answerset = set()

def arrow(color:"色",point:"A~Eの状態",green,red): #point:A~Eにcolor:積み木の色を使ったときに状態を求める

    if selectredgreen % 2 == 0 and color == "緑" and point == 4 :#緑①を使用しているかどうか
        green = "緑①"
    elif selectredgreen % 2 == 1 and color == "緑" and point == 4:
        green = "緑②"

    if selectredgreen <= 1 and color == "濃赤" and point == 3:
        red = "赤①"
    elif selectredgreen > 1 and color == "濃赤" and point == 3:
        red = "赤②"
    return flowchart[point][colorlist.index(color)],green,red 

for selectredgreen in range(4):#濃赤①②と緑①②の場合分け

    if selectredgreen % 2 == 0:
        flowchart[4][3] = 1
    else:
        flowchart[4][3] = 2

    if selectredgreen <= 1:
        flowchart[3][5] = 1
    else:
        flowchart[3][5] = 2

    for i in permutations(colorlist,8): #8色の色の順列
        point = 3 #最初の状態 茶色を入れたのみなので状態D
        flag = 0 #完成パターンであるかどうか
        green = 0
        red = 0
        for j in range(8):
            point,green,red = arrow(i[j],point,green,red)
            if point == -1:
                flag = 1
                break
        if flag == 0:
            answerset.add((i,green,red))

import openpyxl

book = openpyxl.load_workbook('data.xlsx')# ブックを取得

sheet = book['Sheet1']# シートを取得

DarkBlue    ='191970'# セルの背景色の設定
Blue        ='0000ff'
DarkGreen   ='006400'
Green       ='228b22'
Greenyellow ='adff2f'
DarkRed     ='8b0000'
Red         ='ffa07a'
Orange      ='ffa500'

fill = [
    openpyxl.styles.PatternFill(patternType='solid',fgColor=DarkBlue   , bgColor=DarkBlue   ),
    openpyxl.styles.PatternFill(patternType='solid',fgColor=Blue       , bgColor=Blue       ),
    openpyxl.styles.PatternFill(patternType='solid',fgColor=DarkGreen  , bgColor=DarkGreen  ),
    openpyxl.styles.PatternFill(patternType='solid',fgColor=Green      , bgColor=Green      ),
    openpyxl.styles.PatternFill(patternType='solid',fgColor=Greenyellow, bgColor=Greenyellow),
    openpyxl.styles.PatternFill(patternType='solid',fgColor=DarkRed    , bgColor=DarkRed    ),
    openpyxl.styles.PatternFill(patternType='solid',fgColor=Red        , bgColor=Red        ),
    openpyxl.styles.PatternFill(patternType='solid',fgColor=Orange     , bgColor=Orange     )
    ]

i = 1
for j in answerset:

    for k in range(8):

        sheet.cell(row=i, column=k+1).value = j[0][k]# セルへ書き込む
        sheet.cell(row=i, column=k+1).fill = fill[colorlist.index(j[0][k])]

    for l in range(1,3):

        sheet.cell(row=i, column=l+8).value = j[l]

    i += 1

book.save('data.xlsx')# 保存する

4. 결과 출력


차와 노란색을 첨가해 같은 색깔로 정렬하자 칸의 색깔도 바뀌었다.


좋은 웹페이지 즐겨찾기