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.
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.)
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
- AOJ298 Rings of square grid
- AOJ-AHU-OJ-298 Rings of square grid
- The Lord Of The Rings
- Rings
- louboutin soldes Bvlgari rings are an of the best rings that namely extremely appropriate for weddin
- Sum of Square
- Sum of Square
- Sum of Square Numbers
- CodeForces 679C - Bear and Square Grid
- OFM11G-application of grid
- Applications of Chi-Square Tests
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- leetcode[Sum of Square Numbers]
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers。
- AOJ5 渊子赛马
- AOJ10 目标柏林
- Objective-C中的instancetype和id关键字
- Eclipse导入项目:No projects are found to import
- AOJ158 有假币
- AOJ298 Rings of square grid
- AOJ191 删除数字
- Delphi XE 2 精简版在 Windows 8.1 64 位中的安装
- AOJ437 高精度加法
- IOS学习笔记(七)之UISegmentedControl分段控件的基本概念和使用方法
- AOJ194 猴子分桃
- AOJ331 汉诺塔
- AOJ574 爱就大声说出来
- AOJ152 A X B problem(大数相乘、高精度乘法)