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 . Example $s = [[5, 3, 4], [1, 5, 8], [6, 4, 2]] The matrix looks like this: 534158642 We can convert it to the following magic square: 834159672 This took three replacements at a cost of . Function Description Complete the formingMagicSquare function in the editor below. formingMagicSquare has the following parameter(s): int s[3][3]: a  array of integers Returns int: the minimal total cost of converting the input square to a magic square Input Format Each of the  lines contains three space-separated integers of row . Constraints   Sample Input 0 4 9 23 5 78 1 5 Sample Output 0 1 Explanation 0 If we change the bottom right value, , from  to  at a cost of ,  becomes a magic square at the minimum possible cost. Sample Input 1 4 8 24 5 76 1 6 Sample Output 1 4 Explanation 1 Using 0-based indexing, if we make -> at a cost of  -> at a cost of  -> at a cost of , then the total cost will be

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 .

Example

$s = [[5, 3, 4], [1, 5, 8], [6, 4, 2]]

The matrix looks like this:

534158642

We can convert it to the following magic square:

834159672

This took three replacements at a cost of .

Function Description

Complete the formingMagicSquare function in the editor below.

formingMagicSquare has the following parameter(s):

  • int s[3][3]: a  array of integers

Returns

  • int: the minimal total cost of converting the input square to a magic square

Input Format

Each of the  lines contains three space-separated integers of row .

Constraints

  •  

Sample Input 0

4 9 23 5 78 1 5

Sample Output 0

1

Explanation 0

If we change the bottom right value, , from  to  at a cost of ,  becomes a magic square at the minimum possible cost.

Sample Input 1

4 8 24 5 76 1 6

Sample Output 1

4

Explanation 1

Using 0-based indexing, if we make

  • -> at a cost of 
  • -> at a cost of 
  • -> at a cost of ,

then the total cost will be 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Matrix multiplication
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education