POJ 2983 M × N Puzzle
M × N Puzzle
| Time Limit: 4000MS | Memory Limit: 131072K | |
| Total Submissions: 4860 | Accepted: 1321 |
Description
The Eight Puzzle, among other sliding-tile puzzles, is one of the famous problems in artificial intelligence. Along with chess, tic-tac-toe and backgammon, it has been used to study search algorithms.
The Eight Puzzle can be generalized into an M × N Puzzle where at least one of M and N is odd. The puzzle is constructed with MN − 1 sliding tiles with each a number from 1 toMN − 1 on it packed into a M by N frame with one tile missing. For example, with M = 4 and N = 3, a puzzle may look like:
| 1 | 6 | 2 |
| 4 | 0 | 3 |
| 7 | 5 | 9 |
| 10 | 8 | 11 |
Let's call missing tile 0. The only legal operation is to exchange 0 and the tile with which it shares an edge. The goal of the puzzle is to find a sequence of legal operations that makes it look like:
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
| 10 | 11 | 0 |
The following steps solve the puzzle given above.
|
START |
|
DOWN |
|
LEFT ⇒ |
|
UP |
|
… |
||||||||||||||||||||||||||||||||||||||||||||||||
|
RIGHT |
|
UP |
|
UP ⇒ |
|
LEFT |
|
GOAL |
||||||||||||||||||||||||||||||||||||||||||||||||
Given an M × N puzzle, you are to determine whether it can be solved.
Input
The input consists of multiple test cases. Each test case starts with a line containing M and N (2 ≤ M, N ≤ 999). This line is followed by M lines containing N numbers each describing an M × N puzzle.
The input ends with a pair of zeroes which should not be processed.
Output
Output one line for each test case containing a single word YES if the puzzle can be solved and NO otherwise.
Sample Input
3 3
1 0 3
4 2 5
7 8 6
4 3
1 2 5
4 6 9
11 8 10
3 7 0
0 0
Sample Output
YES
NO
#include<cstdio>
//#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
//#include<queue>
//#include<set>
#define INF 0x3f3f3f3f
#define N 10000005
#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],sum_start,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];
}
int main(){
int n;
while(scanf("%d %d",&n,&m)!=EOF,n,m){
sum_start=;
int sum_end,num_cnt=;
R(i,,n)
R(j,,m){
int num=read();
!num?
sum_end=i:
a[++num_cnt]=num;
}
Merge_Sort(a,,num_cnt,sum_start);
D_e(sum_start);
sum_end=n-sum_end;
if(m&)sum_end=;
(sum_start&)==(sum_end&)?
printf("YES\n"):
printf("NO\n");
}
return ;
}
/*
Note:
When Commas Are Used In Trinary Operators, Parentheses Shoule Be Used.
Error:
None.
*/
POJ 2983 M × N Puzzle的更多相关文章
- 【POJ 2983】Is the Information Reliable?(差分约束系统)
id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...
- POJ 2983 Is the Information Reliable?(差分约束系统)
http://poj.org/problem?id=2983 题意:N 个 defense stations,M条消息,消息有精确和模糊之分,若前边为P.则为精确消息,有两个defense stati ...
- ●POJ 2983 Is the Information Reliable?
题链: http://poj.org/problem?id=2983 题解: 差分约束. 1).对于条件(P u v w),不难发现反映到图上就是: $dis[u]-dis[v]=w$,所以添加两条边 ...
- POJ 2983 Is the Information Reliable? 依旧差分约束
http://poj.org/problem?id=2983 题目大意: 星际大战开始了.你购买了情报,需要判断它的准确性.已知地方的根据地在由南向北排成一条直线.P A B X,表示A在B北面距离X ...
- 【POJ】3678 Katu Puzzle
http://poj.org/problem?id=3678 题意:很幼稚的题目直接看英文题面= = #include <cstdio> #include <cstring> ...
- POJ 2893 M × N Puzzle(树状数组求逆序对)
M × N Puzzle Time Limit: 4000MS Memory ...
- POJ 1651:Multiplication Puzzle(区间DP)
http://poj.org/problem?id=1651 题意:给出n个数字,每取中间一个数,就会使得权值加上中间这个数和两边的乘积,求取剩两个数最少的权值是多少. 思路:区间dp. 一开始想了挺 ...
- POJ 1651:Multiplication Puzzle 矩阵相乘式DP
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7118 Accepted: ...
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
随机推荐
- hdu 4741 Save Labman No.004 (异面直线的距离)
转载学习: #include <cstdio> #include <cstdlib> #include <cstring> #include <algorit ...
- js-day
1.克莱托指数 公式 :体重(kg) / (身高(m) * 身高(m)) < 20 : 偏瘦 > 20 <25 : 正常 > 25 : 偏旁 步骤: 1.输入体重(weight ...
- Open Message Queue 集群问题
nohup ./imqbrokerd -tty -name myBroker -port 7677 -javahome /opt/omae/jdk1.7.0_45 -cluster 192.168.2 ...
- Requests接口测试(一)
接口测试概念 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻辑依赖关 ...
- 基于HTML5 Ajax文件上传进度条如何实现(jquery版本)
<!DOCTYPE html> <html> <head> <title>html5_2.html</title> <meta htt ...
- Ubuntu 16.04 安装jdk
Ubuntu 16.04 安装jdk 准备工作 安装版本:jdk-8u91-linux-x64.tar.gz 官方下载 创建目录作为JDK的安装目录,这里选择安装位置为:/usr/java/ sudo ...
- Floyd-Warshall求图中任意两点的最短路径
原创 除了DFS和BFS求图中最短路径的方法,算法Floyd-Warshall也可以求图中任意两点的最短路径. 从图中任取两点A.B,A到B的最短路径无非只有两种情况: 1:A直接到B这条路径即是最短 ...
- 关于使用idea的一些小技巧
1:idea与git同步以后查看修改变化: file --setting--versioncontorller
- @html.dropdown用法
controller1 List<SelectListItem> itemList = new List<SelectListItem>() { "}, " ...
- JavaScript 如何工作:渲染引擎和性能优化技巧
翻译自:How JavaScript works: the rendering engine and tips to optimize its performance 这是探索 JavaScript ...