Rings of square grid
Time Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 64 MB
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
Original | Transformed |
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
Original | Transformed |
1. YES
2. NO
————————————————————Geeker分割线————————————————————
思路:还记得这题曾经问过Yuuji,这是个模拟题。想判断是否能旋转成横着读是123456789这种,就分别一圈一圈拆成一条线性数组,然后存入队列,将这个队列和一圈一圈拆开的“标准参照队列”进行比较。我的方法是选取起点和终点暴力过一遍。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int a[1005][1005];
int b[1005][1005];
int q1[4016];
int q2[4016];
int main() {
int n, cas = 1;
while(scanf("%d", &n)&&n) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
a[i][j] = j + (i-1)*n;//a[][]存的是123456789这样的参照数组
scanf("%d", b[i]+j);
}
}
printf("%d. ", cas++);
int fron, rear, flag = 0;//接下来一圈一圈拆
for(int k = 1; k <= (n+1) / 2; k++) {//模拟发现,偶数只需要做n/2次,奇数需要(n+1)/2次循环
int mini = 1000001;//拆每一圈的过程中,记录b[][]的最小值,以找到匹配的起点
fron = rear = 0;
for(int j = k; j < n+1-k; j++) {//上面一行
q1[rear] = a[k][j];
q2[rear] = b[k][j];
if(q2[rear] < mini) {//一旦发现该元素比最小值更小,说明起点可能在该元素处
fron = rear; //定匹配起点
mini = q2[rear];
}
rear++;
}
for(int i = k; i <= n+1-k; i++) {//右边一列
q1[rear] = a[i][n+1-k];
q2[rear] = b[i][n+1-k];
if(q2[rear] < mini) {
fron = rear;
mini = q2[rear];
}
rear++;
}
for(int j = n-k; j >= k; j--) {//下面一行
q1[rear] = a[n+1-k][j];
q2[rear] = b[n+1-k][j];
if(q2[rear] < mini) {
fron = rear;
mini = q2[rear];
}
rear++;
}
for(int i = n-k; i > k; i--) {//左边一列
q1[rear] = a[i][k];
q2[rear] = b[i][k];
if(q2[rear] < mini) {
fron = rear;
mini = q2[rear];
}
rear++;
}
for(int k1 = 0,k2 = fron; k1 < rear; k1++,k2++) {
if(k2 == rear) k2 = 0;//fron是起点,fron前面的元素是终点
if(q2[k2] != q1[k1]) {
flag = 1;
break;
}
}
}
if(flag) puts("NO");
else puts("YES");
}
return 0;
}