• 【乱搞】【AOJ-298】Rings of square grid


    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
    4
    9 5 1 2
    13 7 11 3
    14 6 10 4
    15 16 12 8
    3
    1 2 3
    5 6 7
    8 9 4
    0
    

     
    Sample Output
    1. YES
    2. NO
    
     
    思路:
    记录需要比较的元素与现有元素进行比较 错误就退出
     
    参考代码
    #include<stdio.h>
    #define LEN1 1000+10
    #define LEN2 20000
    long 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
    ",m++);
                else
                    printf("%d. NO
    ",m++);
        }
        return 0;
    }
  • 相关阅读:
    php中的短标签 太坑人了
    <转>让SVN用户能够修改自身密码的PHP页面
    配置Apache2 管理 SVN
    <转>Java 理论与实践: 正确使用 Volatile 变量
    <转>java中静态方法和非静态方法的存储
    分享:将业务系统页面嵌入到统一平台中(简易版)
    权限管理系统之字典(代码)管理
    权限管理系统之模块管理
    权限管理系统之用户管理
    轻松搞定Struts 2:三步走上手小入门
  • 原文地址:https://www.cnblogs.com/ahu-shu/p/3473188.html
Copyright © 2020-2023  润新知