Odd number problem
描述
你一定玩过八数码游戏,它实际上是在一个3*3的网格中进行的,1个空格和1~8这8个数字恰好不重不漏地分布在这3*3的网格中。
例如:
5 2 8
1 3 _
4 6 7
在游戏过程中,可以把空格与其上、下、左、右四个方向之一的数字交换(如果存在)。
例如在上例中,空格可与左、上、下面的数字交换,分别变成:
5 2 8 5 2 _ 5 2 8
1 _ 3 1 3 8 1 3 7
4 6 7 4 6 7 4 6 _
奇数码游戏是它的一个扩展,在一个n*n的网格中进行,其中n为奇数,1个空格和1~n*n-1这n*n-1个数恰好不重不漏地分布在n*n的网格中。
空格移动的规则与八数码游戏相同,实际上,八数码就是一个n=3的奇数码游戏。
现在给定两个奇数码游戏的局面,请判断是否存在一种移动空格的方式,使得其中一个局面可以变化到另一个局面。
输入格式
多组数据,对于每组数据:
第1行一个整数n,n<500,n为奇数。
接下来n行每行n个整数,表示第一个局面。
接下来n行每行n个整数,表示第二个局面。
局面中每个整数都是0~n*n-1之一,其中用0代表空格,其余数值与奇数码游戏中的意义相同,保证这些整数的分布不重不漏。
输出格式
对于每组数据,若两个局面可达,输出TAK,否则输出NIE。
样例输入
3 1 2 3 0 4 6 7 5 8 1 2 3 4 5 6 7 8 0 1 0 0
样例输出
TAK TAK
#include<cstdio>
//#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
//#include<queue>
//#include<set>
#define INF 0x3f3f3f3f
#define N 250005
#define re register
#define Ii inline int
#define Il inline long long
#define Iv inline void
#define Ib inline bool
#define Id inline double
#define ll long long
#define Fill(a,b) memset(a,b,sizeof(a))
#define R(a,b,c) for(register int a=b;a<=c;++a)
#define nR(a,b,c) for(register int a=b;a>=c;--a)
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define Cmin(a,b) ((a)=(a)<(b)?(a):(b))
#define Cmax(a,b) ((a)=(a)>(b)?(a):(b))
#define D_e(x) printf("\n&__ %d __&\n",x)
#define D_e_Line printf("-----------------\n")
#define D_e_Matrix for(re int i=1;i<=n;++i){for(re int j=1;j<=m;++j)printf("%d ",g[i][j]);putchar('\n');}
using namespace std;
// The Code Below Is Bingoyes's Function Forest.
Ii read(){
int s=,f=;char c;
for(c=getchar();c>''||c<'';c=getchar())if(c=='-')f=-;
while(c>=''&&c<='')s=s*+(c^''),c=getchar();
return s*f;
}
Iv print(ll x){
if(x<)putchar('-'),x=-x;
if(x>)print(x/);
putchar(x%^'');
}
/*
Iv Floyd(){
R(k,1,n)
R(i,1,n)
if(i!=k&&dis[i][k]!=INF)
R(j,1,n)
if(j!=k&&j!=i&&dis[k][j]!=INF)
Cmin(dis[i][j],dis[i][k]+dis[k][j]);
}
Iv Dijkstra(int st){
priority_queue<int>q;
R(i,1,n)dis[i]=INF;
dis[st]=0,q.push((nod){st,0});
while(!q.empty()){
int u=q.top().x,w=q.top().w;q.pop();
if(w!=dis[u])continue;
for(re int i=head[u];i;i=e[i].nxt){
int v=e[i].pre;
if(dis[v]>dis[u]+e[i].w)
dis[v]=dis[u]+e[i].w,q.push((nod){v,dis[v]});
}
}
}
Iv Count_Sort(int arr[]){
int k=0;
R(i,1,n)
++tot[arr[i]],Cmax(mx,a[i]);
R(j,0,mx)
while(tot[j])
arr[++k]=j,--tot[j];
}
Iv Merge_Sort(int arr[],int left,int right,int &sum){
if(left>=right)return;
int mid=left+right>>1;
Merge_Sort(arr,left,mid,sum),Merge_Sort(arr,mid+1,right,sum);
int i=left,j=mid+1,k=left;
while(i<=mid&&j<=right)
arr[i]<=arr[j]?
tmp[k++]=arr[i++]:
tmp[k++]=arr[j++],sum+=mid-i+1;//Sum Is Used To Count The Reverse Alignment
while(i<=mid)tmp[k++]=arr[i++];
while(j<=right)tmp[k++]=arr[j++];
R(i,left,right)arr[i]=tmp[i];
}
Iv Bucket_Sort(int a[],int left,int right){
int mx=0;
R(i,left,right)
Cmax(mx,a[i]),++tot[a[i]];
++mx;
while(mx--)
while(tot[mx]--)
a[right--]=mx;
}
*/
int n,m,a[N],tmp[N];
Iv Merge_Sort(int arr[],int left,int right,int &sum){
if(left>=right)return;
int mid=left+right>>;
Merge_Sort(arr,left,mid,sum),Merge_Sort(arr,mid+,right,sum);
int i=left,j=mid+,k=left;
while(i<=mid&&j<=right)
(arr[i]<=arr[j])?
tmp[k++]=arr[i++]:
(tmp[k++]=arr[j++],sum+=mid-i+);//Sum Is Used To Count The Reverse Alignment
while(i<=mid)tmp[k++]=arr[i++];
while(j<=right)tmp[k++]=arr[j++];
R(i,left,right)arr[i]=tmp[i];
}
#define PutTAK printf("TAK\n")
#define PutNIE printf("NIE\n")
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int sum_start=,sum_end=;
n*=n;
if(!n)
PutNIE;
//Judge Case Of n=0 Specially
if(n==){
(read()==read())?
PutTAK:
PutNIE;
continue;
} //Judge Case Of n=1 Specially
int cnt_num=;
R(i,,n){
int num=read();
if(num)
a[++cnt_num]=num;
}
Merge_Sort(a,,cnt_num,sum_start);
cnt_num=;
R(i,,n){
int num=read();
if(num)
a[++cnt_num]=num;
}
Merge_Sort(a,,cnt_num,sum_end);
((sum_start&)==(sum_end&))?
PutTAK:
PutNIE;
}
return ;
}
/*
Note:
Error:
*/
Odd number problem的更多相关文章
- Buge's Fibonacci Number Problem
Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...
- odd number、 even number
odd number 奇数 even number 偶数
- 《高性能javascript》 --- in case of odd number of items(奇怪的条目的数量)
不知道是做着故意放的还是什么原因.总之运行后就会出现问题(奇怪的条目的数量) function merge(left, right){ var result = []; while (left.len ...
- shiro : java.lang.IllegalArgumentException: Odd number of characters.
shiro使用的时候: java.lang.IllegalArgumentException: Odd number of characters. at org.apache.shiro.cod ...
- 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.
找出数组中的单身狗: 1. OddOccurrencesInArray Find value that occurs in odd number of elements. A non-empty ze ...
- POJ 1350 Cabric Number Problem (模拟)
题目链接 Description If we input a number formed by 4 digits and these digits are not all of one same va ...
- 【HDOJ】3509 Buge's Fibonacci Number Problem
快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. /* 3509 */ #inc ...
- PERFECT NUMBER PROBLEM(思维)
题目链接:https://nanti.jisuanke.com/t/38220 题目大意:这道题让我们判断给定数字是否为完美数字,并给来完美数字的定义,就是一个整数等于除其自身之外的所有的因子之和. ...
- 2106 Problem F Shuffling Along 中石油-未提交-->已提交
题目描述 Most of you have played card games (and if you haven’t, why not???) in which the deck of cards ...
随机推荐
- 在页面完成读取EXCEL
protected void btnUpload_Click(object sender, EventArgs e) { if (Page.IsValid) { string sFILENAME = ...
- c++ 组合模式(composite)
原文地址:http://www.cnblogs.com/jiese/p/3168844.html 当你发现需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一地使用 ...
- ubuntu下不用eclipse开发cocos2d-x(命令行)
之前在ubuntu下通过eclipse搭建开发cocos2d-x的环境,但是由于eclipse界面在ubuntu显得很丑,在里面写代码,完全没有感觉,后来我转到了win7上通过vs2010和eclip ...
- Angular问题04 模块导入错误???、BrowserModule模块重复加载???、material模块引入后报错
1 模块导入错误 1.1 问题描述 项目启动时报错:元数据错误,错误截图如下: 1.2 问题原因 利用VsCode开发angular项目时利用自动导入出现错误 坑01:VsCode 的自动导入功能比较 ...
- 345. Reverse Vowels of a String翻转字符串中的元音字母
[抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
- 虚拟机Ubuntu16.04安装lrzsz
[系统环境] 宿主机:Win7 64位 虚拟机软件:Vmware workstation 12 虚拟机:Ubuntu 16.0.4 [目的] 配合Secure CRT使用rz,sz,方便在Ubuntu ...
- Python3 网络爬虫开发实战学习弱点书签
1. urllib.robotparse模块对robot.txt文件的解析,can_fetch()方法和parse()方法. Page121 2. lxml.etree模块自动补全Html代码,Htm ...
- Oracle——分组函数
AVG(平均值)和 SUM (合计)函数 可以对数值型数据使用AVG 和 SUM 函数. AVG组函数忽略空值 --在组函数中使用NVL函数 --求平均值 )) MIN(最小值)和 MAX(最大值)函 ...
- hdu 4279 Number(G++提交)
打表找规律: #include<stdio.h> #include<math.h> #define N 250 bool judge(int i,int j) { ;k< ...
- redis过期key的清理策略
一,有三种不同的删除策略(1),立即清理.在设置键的过期时间时,创建一个回调事件,当过期时间达到时,由时间处理器自动执行键的删除操作. (2),惰性清理.键过期了就过期了,不管.当读/写一个已经过期的 ...