Ghasem Banazadeh
Ghasem Banazadeh

Reputation: 3

Forming a magic square

We define a magic square to be an matrix of distinct positive integers from to where the sum of any row, column, or diagonal of length is always equal to the same number: the magic constant.

You will be given a matrix of integers in the inclusive range . We can convert any digit to any other digit in the range at cost of . Given , convert it into a magic square at minimal cost. Print this cost on a new line.

Note: The resulting magic square must contain distinct integers in the inclusive range .

For example, we start with the following matrix :

5 3 4

1 5 8

6 4 2

We can convert it to the following magic square:

8 3 4

1 5 9

6 7 2

This took three replacements at a cost of . 5-8 + 8-9 + 4-7 = 7

I Have Write a programm to slove this but i get the incorrect result when i try to run it.

def formingMagicSquare(s):
    arr=[]
    duplicates=[]
    totaldifference=0
    for i in range(0,len(s)):
        linesum=sum(s[i])
        for j in range(0,len(s[i])):
            if(s[i][j] in arr and linesum!=15):
                duplicates.append(i*10+j)
            else:
                arr.append(s[i][j])
    for i in range(0,len(duplicates)):
        iarr = duplicates[i]//10
        jarr = duplicates[i]%10
        linesum=sum(s[i])
        difference=15-linesum
        totaldifference = totaldifference + difference
    return totaldifference

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = []

    for _ in range(3):
        s.append(list(map(int, input().rstrip().split())))

    result = formingMagicSquare(s)

    fptr.write(str(result) + '\n')

    fptr.close()

Upvotes: 0

Views: 3363

Answers (2)

ModarYaghi
ModarYaghi

Reputation: 1

I think you can easily try:

def forming_magic_square(s):

    # Flaten s
    s = list(itertools.chain.from_iterable(s))

    magic_squares = [
        [8, 1, 6, 3, 5, 7, 4, 9, 2],
        [6, 1, 8, 7, 5, 3, 2, 9, 4],
        [4, 9, 2, 3, 5, 7, 8, 1, 6],
        [2, 9, 4, 7, 5, 3, 6, 1, 8],
        [8, 3, 4, 1, 5, 9, 6, 7, 2],
        [4, 3, 8, 9, 5, 1, 2, 7, 6],
        [6, 7, 2, 1, 5, 9, 8, 3, 4],
        [2, 7, 6, 9, 5, 1, 4, 3, 8],
    ]

    costs = []
    for magic_square in magic_squares:
        costs.append(sum([abs(magic_square[i] - s[i]) for i in range(9)]))

    return min(costs)

Upvotes: 0

Ghasem Banazadeh
Ghasem Banazadeh

Reputation: 3

class Magic(object):

    pre = [
            [[8, 1, 6], [3, 5, 7], [4, 9, 2]],
            [[6, 1, 8], [7, 5, 3], [2, 9, 4]],
            [[4, 9, 2], [3, 5, 7], [8, 1, 6]],
            [[2, 9, 4], [7, 5, 3], [6, 1, 8]],
            [[8, 3, 4], [1, 5, 9], [6, 7, 2]],
            [[4, 3, 8], [9, 5, 1], [2, 7, 6]],
            [[6, 7, 2], [1, 5, 9], [8, 3, 4]],
            [[2, 7, 6], [9, 5, 1], [4, 3, 8]],
            ]

    def evaluate(self, s):
        totals = []
        for p in self.pre:
            total = 0
            for p_row, s_row in zip(p, s):
                for i, j in zip(p_row, s_row):
                    if not i == j:
                        total += max([i, j]) - min([i, j])
            totals.append(total)
        return min(totals)
def main():
    s=[]
    for _ in range(3):
        s.append(list(map(int, input().rstrip().split())))

    magic = Magic()
    result = magic.evaluate(s)

    print(result)

if __name__ == '__main__':
    main()

Thank You I Have Write A new Code And I Have Change my code from the base.

Upvotes: 0

Related Questions