AOJ298 Rings of square grid

来源:互联网 发布:软件的质量标准 编辑:程序博客网 时间:2024/06/13 01:13
Description
Any square grid can be viewed as one or more rings, one inside the other. For example, as shown in figure (a), a 5 X 5 grid is made of three rings, numbered 1,2 and 3 (from outside to inside.) A square grid of size N is said to be sorted, if it includes the values from 1 to N2 in a row-major order, as shown in figure (b) for N = 4. We would like to determine if a given square grid can be sorted by only rotating its rings. For example, the grid in figure (c) can be sorted by rotating the first ring two places counter-clockwise(逆时针), and rotating the second ring one place in the clockwise direction.

 

Input
Your program will be tested on one or more test cases. The first input line of a test case is an integer N which is the size of the grid. N input lines will follow, each line made of N integer values specifying the values in the grid in a row-major order. Note than 0 < N ≤ 1, 000 and grid values are natural numbers less than or equal to 1,000,000.
The end of the test cases is identified with a dummy test case with N = 0.

 

Output
For each test case, output the result on a single line using the following format:
k.result
Where k is the test case number (starting at 1,)  is a single space, and result
is "YES" or "NO" (without the double quotes.)

 

Sample Input
49 5 1 213 7 11 314 6 10 415 16 12 831 2 35 6 78 9 40

 
Sample Output
1. YES2. NO
 
思路:
记录需要比较的元素与现有元素进行比较 错误就退出
 
参考代码
#include<stdio.h>#define LEN1 1000+10#define LEN2 20000long int m1[LEN1][LEN1],m2[LEN1][LEN1];long int a[LEN2],b[LEN2];int main(){    int n,m=1;    int i,j,k;    int c,flag,q;    int s;    while(scanf("%d",&n)&&n)    {        for(i=1;i<=n;i++)            for(j=1;j<=n; j++)            {                m1[i][j]=(i-1)*n+j;                scanf("%ld",&m2[i][j]);            }            c=(n+1)/2;            flag=0;            for(k=1;k<=c;k++)            {                q=0;                for(i=k;i<n+1-k;i++)                {                    a[q]=m1[k][i];                    b[q]=m2[k][i];                    q++;                }                for(i=k;i<=n+1-k;i++)                {                    a[q]=m1[i][n+1-k];                    b[q]=m2[i][n+1-k];                    q++;                }                for(i=n-k;i>=k;i--)                {                    a[q]=m1[n+1-k][i];                    b[q]=m2[n+1-k][i];                    q++;                }                for(i=n-k;i>k;i--)                {                    a[q]=m1[i][k];                    b[q]=m2[i][k];                    q++;                }                s=0;                for(i=0;i<q;i++)                {                    if(a[i]==b[0])                    {                        s=i;                        break;                    }                }                j=0;                for(i=s;i<q;i++)                    if(a[i]!=b[j++])                    {                        flag=1;                        break;                    }                    if(!flag)                    {                        for(int i=0;i<s;i++)                            if(a[i]!=b[j++])                            {                                flag=1;                                break;                            }                    }            }            if(!flag)                printf("%d. YES\n",m++);            else                printf("%d. NO\n",m++);    }    return 0;}

 

0 0
原创粉丝点击