题目链接:Crazy Rows - Problem - QOJ.ac
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
#define x first
#define y second
//#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
const int mod = 1e9 + 7;
const int N = 1e6+ 10;
int n, m, o[N], x, cnt;
inline void sovle()
{
cin >> n;
vector<int> o;
for(int i = 1; i <= n; i ++)
{
int s = 0;
char a;
for(int j = 0; j < n; j ++)
{
cin >> a;
if(a == '1') s = 0;
else s ++;
}
o.push_back(s);
}
int sum = 0, x = n - 1;
for(int i = 0; i < n; i ++)
{
if(o[i] < x)
{
int a;
for(int j = i + 1; j < n; j ++)
{
if(o[j] >= x)
{
a = o[j];
sum += j - i;
o.erase(o.begin() + j);
break;
}
}
o.insert(o.begin() + i + 1, o[i]);
}
x --;
}
cout << "Case #" << ++ cnt << ": " << sum << endl;
}
signed main(void)
{
IOS;
int t = 1;
cin >> t;
while(t --) sovle();
return 0;
}