Codeforces 230C Shifts (analog + unwind string) __ analog

Source: Internet
Author: User
Tags abs

Description

You are 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 the all cell, except for the last one, to th e Right neighboring cell, and to move the value of the "last cell" to the "the". A cyclical shift of a row to the left is performed similarly and but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "0011 0 "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 "I" contains two space-separated integers:n (1≤n≤100)-the number of rows in the table and M (1≤m≤104 )-the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the Cont Ents of the cell in the I-th row and the j-th column of the table.

It is guaranteed this 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 It is impossible, print-1.

Sample Input Input

3 6
101010
000100
100000
Output
3
Input
2 3
000
Output
-1

Meaning

Push the table can only push one grid at a time, note that the loop, from the first to the last one as long as the push, so that there is a column of 1, output minimum number of steps

Exercises

Feeling is not difficult, but time out n times, the idea is very simple simulation, there is the idea of expanding strings, if not the last one of the line The 1 ' position takes the current 1 position and the next 1 position in the middle node to find the shortest distance from two. Add to the array of each column, if the last one expands the string, the shortest distance from the first 1 of each row, then accumulates, scans all rows, and the smallest of the last array is the answer.

Code:

#include <stdio.h> #include <cstring> #include <string> #include <iostream> #include <
algorithm> #include <math.h> #include <queue> #include <stack> using namespace std; int p[105][10005];//for storing graphs ... The feeling can not be int b[105];//with the storage of how many ' 1 ' int a[105][10005];//on each line has the ' 1 ' position int h[10005];//to store the cumulative minimum distance of each column and int main () {int m,n
    , I,j,k,tag,flag,minn,num;
        while (scanf ("%d%d", &m,&n)!=eof) {flag=1;
            for (i=0;i<m;i++) {b[i]=0;
            tag=0;
                for (j=0;j<n;j++) {scanf ("%1d", &p[i][j]);
                    if (p[i][j]==1) {a[i][b[i]]=j;
                    b[i]++;
                tag=1;
        } if (!tag) flag=0;
        } if (!flag) {printf (" -1\n");//If a row does not appear 1 then there is no continue; for (i=0;i<n;i++) h[i]=0;//initializes eachRow for (i=0;i<m;i++) {for (j=0;j<b[i];j++) {if (j==b[i]-1)//expand
                        The thought of the string is divided into {for (k=a[i][j];k<=n+a[i][0];k++) {
                        if (k<n) {h[k]+=min (ABS (K-A[I][J)), ABS (k-n-a[i][0)); else {h[k%n]+=min (ABS) (K-
                        A[I][J]), ABS (K-n-a[i][0]); }} else {for (k=a[i][j];k<=a I
                    [j+1];k++) {H[k]+=min (ABS (K-A[I][J]), ABS (k-a[i][j+1));
        }}} minn=10086111;
        for (i=0;i<n;i++)//sweep the minimum value {if (Minn>h[i]) minn=h[i];
    printf ("%d\n", Minn);
}    return 0;
 }



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.