Rotating Rings

2023-11-08 11:33
文章标签 rotating rings

本文主要是介绍Rotating Rings,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Rotating Rings

Time Limit: 1000MS Memory limit: 65536K

题目描述

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.

输入

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.

输出

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
result is "YES" or "NO" (without the double quotes.)

示例输入

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

示例输出

1. YES
2. NO
代码:
#include<iostream>
#include<stdio.h>
using namespace std;
int n;
int map[1005][1005];
void get_next(int x , int y , int &xx , int &yy)
{
int round = min(min(n - x +1 , n - y +1) , min(x , y));
if (x == round && y < n - round + 1) xx = x , yy = y + 1;
else if (y == round && x > round) xx = x - 1 , yy = y;
else if (x == n - round +1 && y > round) xx = x , yy = y - 1;
else if (y == n - round + 1 && x < n -round + 1)  xx = x + 1 , yy =y;
else xx = x , yy = y;
}
bool check()
{
int nexti,nextj,x,y,nextx,nexty;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if (map[i][j] <=0  || map[i][j] > n*n)
return false;
get_next(i,j,nexti,nextj);
x=map[i][j]/n+1;
int tmp=map[i][j]%n;
y=map[i][j]%n;
if(tmp==0)
x--;
if(y==0)
y=n;
get_next(x,y,nextx,nexty);
if(map[nexti][nextj]!=(nextx-1)*n+nexty)
return false;
}
}
return true;
}
int main()
{
int cas;
cas=1;
while(scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
scanf("%d",&map[i][j]);
}
bool ans=check();
if(ans)
printf("%d. YES\n",cas++);
else printf("%d. NO\n",cas++);
}
return 0;
}

这篇关于Rotating Rings的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/369578

相关文章

uva 10985 - Rings'n'Ropes( 最短路Folyd)

题目连接:uva 10985 - Rings'n'Ropes 题目大意:给出n和m,表示有n个戒指和m条绳子,每条绳子的长度为1,给出每根绳子连接的戒指,然在左右手各握住一个戒指,拉直,问说最多有多少个绳子被绷直。 解题思路:首先先用Floyd算法将每两个戒指之间的最短距离求出(因为向两端扯的时候,最大距离便是连接两个戒指的最短距离),然后枚举两枚戒指,找出中间所有的点(f[x][

【影片欣赏】【指环王】【魔戒:双塔奇谋 The Lord of the Rings: The Two Towers】

2003年发行,Special Extended DVD Edition Part One 1. The Foundations of Stone 2. Elven Rope 3. The Taming of Smeagol 4. The Uruk-hai 5. The Three Hunters 6. The Burning of the Westfold 7. Massacre a

文献阅读A wearable noncontact free-rotating hybrid nanogenerator for self-powered electronics

抽象 自供电性是便携式设备发展的新趋势。收集生物力学能量为个人信息电子设备提供动力具有重要意义。在这项工作中,我们报告了一种可穿戴的非接触式自由旋转混合纳米发电机(WRG),它由摩擦电纳米发电机和电磁发电机组成。在外力的瞬时激励期间,可以实现超过2秒的连续输出,由于其独特的机械储能设计,与其他可穿戴纳米发电机相比,其提高了两个数量级。WRG可以集成到鞋子中,在每次步进中产生14.68 mJ的输出

Leetcode 1914. Cyclically Rotating a Grid

文章作者:Tyan 博客:noahsnail.com  |  CSDN  |  简书 1. Description 2. Solution **解析:**Version 1,先根据规律求出每一层的索引,然后按逆时针顺序保存到数组中,则k次循环后当前索引的位置index在列表中的位置为(index+k) % len(circle),因此将当前索引位置的值赋给目标索引位置即可。 Versi

POJ - 3335 Rotating Scoreboard(半平面交判断多边形内核)

链接 Rotating Scoreboard 题意 顺时针给出一些点,判断这些点构成的多边形是否存在内核; 思路 多边形内核: 它是平面简单多边形的核是该多边形内部的一个点集,该点集中任意一点与多边形边界上一点的连线都处于这个多边形内部。 就是一个在一个房子里面放一个摄像 头,能将所有的地方监视到的放摄像头的地点的集合即为多边形的核。 利用半平面交,最后判断队列里的点是否 > 2 >