/*
给定一个仅包含0和1的n*n二维矩阵
请计算二维矩阵的最大值
计算规则如下
1、每行元素按下标顺序组成一个二进制数(下标越大约排在低位),
二进制数的值就是该行的值,矩阵各行之和为矩阵的值
2、允许通过向左或向右整体循环移动每个元素来改变元素在行中的位置
比如
[1,0,1,1,1] 向右整体循环移动两位 [1,1,1,0,1]
二进制数为11101 值为29
[1,0,1,1,1] 向左整体循环移动两位 [1,1,1,1,0]
二进制数为11110 值为30
输入描述
1.数据的第一行为正整数,记录了N的大小
0<N<=20
2.输入的第2到n+1行为二维矩阵信息
行内元素边角逗号分割
输出描述
矩阵的最大值
示例1
输入
5
1,0,0,0,1
0,0,0,1,1
0,1,0,1,0
1,0,0,1,1
1,0,1,0,1
输出
122
说明第一行向右整体循环移动一位,得到最大值 11000 24
因此最大122
*/
#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int n;
int bin2dec(queue<int> q) //2进制转10
{
int idec = 0; int k = n;
while (q.empty() == 0)
{
idec += pow(2, k - 1) * q.front();
q.pop();
k--;
}
return idec;
}
int maxvalue(string str)
{
queue<int> q; //q要定义为局部变量(或者在这儿清空也行),不然会有上一行数据堆积
int rowmax = 0; //一行元素在移动过程中能产生的的最大值
for (char c : str)
{
int a = c - '0';
q.push(a);
}
int j = n;
while (j)
{
q.push(q.front());
q.pop();
rowmax = max(rowmax, bin2dec(q));
j--;
}
return rowmax;
}
int main()
{
cin >> n;
int ans = 0;
string str;
for (int i = 0; i < n; i++)
{
cin >> str;
regex reg(",");
str = regex_replace(str, reg, "");
ans += maxvalue(str);
}
cout << ans;
return 0;
}