poj 3509

Rotating Rings
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1707 Accepted: 650

Description

11111
12221
12 321
12221
11111
Figure (a)
1234
5678
9101112
13141516
Figure (b)
9512
137113
146104
1516128
Figure (c)
Any square grid can be viewed as one or more rings, one inside the other. For example, as shown in figure (a), a 5 * 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 N 2 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), 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
 
 
 
题意:给定一个矩阵,问能否由类似b图每一层旋转得到。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
int map[1105][1105],map1[1105][1105];
int a[50000],b[50000];
int main()
{
    int n;
    int cas=1;
    while(scanf("%d",&n))
    {
        if(n==0)break;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                map[i][j]=(i-1)*n+j;
                scanf("%d",&map1[i][j]);
            }

        int ceng=(n+1)/2;
        int flag=0;
        for(int k=1; k<=ceng; k++)
        {
            int l=0;
            for(int i=k; i<n+1-k; i++)
            {
                a[l]=map[k][i];
                b[l]=map1[k][i];
                l++;
            }
            for(int i=k; i<=n+1-k; i++)
            {
                a[l]=map[i][n+1-k];
                b[l]=map1[i][n+1-k];
                l++;
            }
            for(int i=n-k; i>=k; i--)
            {
                a[l]=map[n+1-k][i];
                b[l]=map1[n+1-k][i];
                l++;
            }
            for(int i=n-k; i>k; i--)
            {
                a[l]=map[i][k];
                b[l]=map1[i][k];
                l++;
            }

            int index=0;
            for(int i=0; i<l; i++)
            {
                if(a[i]==b[0])
                {
                    index=i;
                    break;
                }
            }
            int j=0;
            for(int i=index; i<l; i++)
                if(a[i]!=b[j++])
                {
                    flag=1;
                    break;
                }
            if(!flag)
            {
                for(int i=0; i<index; i++)
                    if(a[i]!=b[j++])
                    {
                        flag=1;
                        break;
                    }
            }
            /*for(int i=0; i<l; i++)
                cout<<a[i]<<" ";
            cout<<endl;
            for(int i=0; i<l; i++)
                cout<<b[i]<<" ";
            cout<<endl;*/
        }
        if(!flag)
            cout<<cas++<<". YES"<<endl;
        else
            cout<<cas++<<". NO"<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值