티스토리 뷰

출처 : https://www.hackerrank.com/challenges/magic-square-forming/problem


#include <bits/stdc++.h>


using namespace std;


int MS[8][3][3] = {{ 2,9,4,7,5,3,6,1,8 },

{ 4,3,8,9,5,1,2,7,6 },

{ 4,9,2,3,5,7,8,1,6 },

{ 8,3,4,1,5,9,6,7,2 },

{ 8,1,6,3,5,7,4,9,2 },

{ 2,7,6,9,5,1,4,3,8 },

{ 6,1,8,7,5,3,2,9,4 },

{ 6,7,2,1,5,9,8,3,4 }};


int formingMagicSquare(vector<vector<int>> s) {

int ans = 9999, cnt=0;

for (int m = 0; m < 8; m++) {

cnt = 0;

for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) cnt += abs(s[i][j] - MS[m][i][j]);

ans = min(ans, cnt);

}

return ans;

}


int main()

{

ofstream fout(getenv("OUTPUT_PATH"));


vector<vector<int>> s(3);

for (int i = 0; i < 3; i++) {

s[i].resize(3);


for (int j = 0; j < 3; j++) {

cin >> s[i][j];

}


cin.ignore(numeric_limits<streamsize>::max(), '\n');

}


int result = formingMagicSquare(s);


fout << result << "\n";


fout.close();


return 0;

}



'알고리즘 > 기타' 카테고리의 다른 글

[HackerRank] Encryption  (0) 2018.09.30
[HackerRank] Non-Divisible Subset  (0) 2018.09.30
[HackerRank] Extra Long Factorials  (0) 2018.09.28
[HackerRank] Climbing the Leaderboard  (0) 2018.09.28
[HackerRank] Time Conversion  (0) 2018.09.27
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함