-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeforces_230C.cpp
More file actions
68 lines (62 loc) · 1.28 KB
/
codeforces_230C.cpp
File metadata and controls
68 lines (62 loc) · 1.28 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// https://codeforces.com/contest/230/problem/C
// Created by Rahul Sharma
#include <bits/stdc++.h>
//#include<conio.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;
// In-place rotates s towards left by d
vector<string> leftrotate(int i, int d, vector<string> v)
{
reverse(v[i].begin(), v[i].begin() + d);
reverse(v[i].begin() + d, v[i].end());
reverse(v[i].begin(), v[i].end());
return v;
}
// In-place rotates s towards right by d
vector<string> rightrotate(int i, int d, vector<string> v)
{
leftrotate(i, v[i].length() - d, v);
return v;
}
int findMin(vector<string> &v, int count) {
bool flag = true;
rep(j, v[0].length()) {
rep(i, v.size()) {
if (v[i][j] != '1') {
flag = false;
break;
}
}
if (flag) return 1;
}
if (flag == false) return count + 1;
rep(i, v.size()) {
count = min(1 + findMin(leftrotate(i, 1, v), count),
(1 + findMin(rightrotate(i, 1, v), count)));
}
return count;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
bool flag = true;
string s;
vector<string> v(n);
rep(i, n) {
cin >> v[i];
auto findOne = v[i].find('1');
if (findOne == string::npos) {
cout << "-1";
break;
}
}
cout << findMin(v, 1);
cout << '\n';
//_getch();
return 0;
}