Malevich Strikes Back!

来源:互联网 发布:php中is numeric 编辑:程序博客网 时间:2024/05/14 14:31

1221. Malevich Strikes Back!

Time limit: 1.0 second
Memory limit: 64 MB
Problem illustration
After the greatest success of Malevich's "Black Square" the famous artist decided to create a new masterpiece. He took a large sheet of checked paper and filled some cells with black. After that he realized the picture to be too complicated. He was afraid, that people would not understand the sense of the painting. Thus, Malevich decided to cut out a smaller picture of the special form. It should be a black square with its sides parallel to the sides of the list. A white square rotated by 45 degrees should be placed inside the black square. The corners of the white square should lay on the sides of the black square. You can see an example of such picture on the figure.
The original paper size is N × N, 0 < N ≤ 100. Your program should help Malevich to find the largest figure corresponding to the pattern described above.

Input

The input contains several test cases. Each test case starts with the size of paper N. The followingN lines of the test case describe the original painting: "1" denotes a black and "0" denotes a white cell. End of the input is marked by a zero value for N.

Output

Your program should output the size (i.e. the maximum width or height) of the largest figure, which Malevich would like to cut out. If no such figure exists, output "No solution".

Sample

inputoutput
61 1 0 1 1 01 0 0 0 1 10 0 0 0 0 01 0 0 0 1 11 1 0 1 1 1 0 1 1 1 1 141 0 0 10 0 0 00 0 0 01 0 0 10
5No solution

/*    直接枚举图形的边长和中心,判断图形是否满足要求*/#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#define MAXN 110using namespace std;int map[MAXN][MAXN];int n;int abs(int x,int y){    if(x<y)  return y-x;    else return x-y;}int check(int x,int y,int w){    if(y-w<1||y+w>n||x-w<1||x+w>n)          return 0;    for(int i=y-w; i<=y+w; i++)    {        for(int j=x-w; j<x-w+abs(i-y); j++)            if(map[j][i]==0)                  return 0;        for(int j=x-w+abs(i-y); j<=x+w-abs(i-y); j++)            if(map[j][i]==1)                  return 0;        for(int j=x+w; j>x+w-abs(i-y); j--)            if(map[j][i]==0)                  return 0;    }    return 1;}int main(){    while(scanf("%d",&n)!=EOF&&n)    {        for(int i=1; i<=n; i++)        {            for(int j=1; j<=n; j++)                scanf("%d",&map[i][j]);        }        int flag=0;        for(int k=(n%2==0?n-1:n); k>=3; k-=2)//边长        {            for(int i=1; i<n; i++)            {                for(int j=1; j<n; j++)                {                    if(map[i][j]==0&&check(i,j,k/2))//(i,j)为中心                    {                        flag=1;                        cout<<k<<endl;//碰到的第一个满足条件的k一定是最大的,从大到小枚举                        break;                    }                    if(flag)  break;                }                if(flag)  break;            }            if(flag)  break;        }        if(!flag)  cout<<"No solution\n";    }    return 0;}


原创粉丝点击