Codeforces 230C Shifts (analog)

Source: Internet
Author: User

Test instructions There are N m-columns of the turntable a column of each turntable 1 or 0 You can turn a turntable one at a time ask at least how many times to make a column n the number on the turntable is 1

The minimum time required to convert all the columns of each turntable to 1 is saved and can be rotated clockwise and counterclockwise by one of the 1 points to calculate the number of times each point needs to be rotated and the last to see which column and the smallest.

#include <bits/stdc++.h>using namespace Std;const int n =, M = 10005;int d[m], t[m], N, M;char g[n][m];void Gett (int r, int c)    {int p = (c + 1)% m, cnt = t[c] = 0;        while (P! = c)//clockwise turn round {if (g[r][p] = = ' 1 ') cnt =-1;        T[P] = ++cnt;    p = (p + 1)% m;    } p = (p + m-1)% m, cnt = 0;        while (P! = c)//counter clockwise round {if (g[r][p] = = ' 1 ') cnt =-1;        if (T[p] > cnt + 1) t[p] = cnt + 1;        D[P] + = t[p], ++cnt;    p = (p + m-1)% m;    }}int Main () {int i, J;        while (~SCANF ("%d%d", &n, &m)) {memset (d, 0, sizeof (d));        for (i = 0; i < n; ++i) scanf ("%s", G[i]); for (i = 0; l < n; ++i) {for (j = 0; j < m; ++j) if (g[i][j] = = ' 1 ') Gett (i, j), j =            M        if (j = = m) break;        } sort (d, D + M);    printf ("%d\n", I < n -1:d[0]); } return 0;}

C. Shifts

< Span style= "Font-family:system" >you is given a table consisting of  n  rows and  m  columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift It values either one cell to the left, or one cell t o the right.

to cyclically Shift  a table row one cell to the right means to move the value of each cell, except for the last one, to the Right neighboring cells, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left was performed similarly, but the other direction. For example, if we cyclically shift a row "00110< /span> "One cell to the right, we get a row" 00011< /span> ", but if we shift a row" 00110 "one Cell to the left, we get a row "01100 ".

Determine the minimum number of moves needed to make some table column consist only of numbers 1.

Input

The first line contains space-separated integers:N(1?≤? n? ≤?100)-the number of rows in the table andm(1?≤? m. ≤?104 )-the number of columns in the table. ThenNLines follow, each of them containsmCharacters "0"or"1": theJ-th character of theI-th Line describes the contents of the cells in theI-th row and in theJ-th column of the table.

It is guaranteed, the description of the table contains no other characters besides "0" and "1".

Output

Print a single number:the minimum number of moves needed to get only numbers 1 in some column of the table. If This is impossible, print-1.

Sample Test (s)input
3 6101010000100100000
Output
3
input
2 3111000
Output
-1


Codeforces 230C Shifts (analog)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.