15 Puzzle LightOJ - 1121
https://cn.vjudge.net/problem/LightOJ-1121
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
char /*ans[1011],*/now[];int len;
int x,y,a[][];//xx1[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
int xx2[][]={{,},{,},{,},{,},{,},{,},{,},{,},{,},
{,},{,},{,},{,},{,},{,},{,}};
bool fl;
int T,n,maxd;
inline int abs1(int x){return x>?x:-x;}
//估价函数:除0以外,所有数当前位置与目标位置的曼哈顿距离之和
int hh()
{
int i,j,cnt=;
for(i=;i<;++i)
for(j=;j<;++j)
if(a[i][j])
cnt+=abs1(i-xx2[a[i][j]][])+abs1(j-xx2[a[i][j]][]);
return cnt;
}
inline void swap1(int &a,int &b)
{
int t=a;a=b;b=t;
}
//#define swap(a,b) {t=a;a=b;b=t;}
#define swap swap1
void dfs(int d,char pre)
{
//printf("1t%d\n",d);
int h=hh();
if(!h) {fl=;/*memcpy(ans,now,sizeof(char)*(len+1));*/return;}
if(d+h>maxd) return;
if(x!= && pre!='U')
{
//now[++len]='D';
swap(a[x][y],a[x+][y]);++x;
dfs(d+,'D');
if(fl) {now[++len]='D';return;}
//--len;
--x;swap(a[x][y],a[x+][y]);
}
if(y!= && pre!='R')
{
//now[++len]='L';
swap(a[x][y-],a[x][y]);--y;
dfs(d+,'L');
if(fl) {now[++len]='L';return;}
//--len;
++y;swap(a[x][y-],a[x][y]);
}
if(y!= && pre!='L')
{
//now[++len]='R';
swap(a[x][y+],a[x][y]);++y;
dfs(d+,'R');
if(fl) {now[++len]='R';return;}
//--len;
--y;swap(a[x][y+],a[x][y]);
}
if(x!= && pre!='D')//不要让操作刚好抵消上一次操作,实测很有效
{
//now[++len]='U';
swap(a[x][y],a[x-][y]);--x;
dfs(d+,'U');
if(fl) {now[++len]='U';return;}
//--len;
++x;swap(a[x][y],a[x-][y]);
}
}
/*
判无解,
https://blog.csdn.net/obsorb_knowledge/article/details/79915484
A=将16个数排成一行,((0,0),(0,1),(0,2),(0,3),(1,0),(1,1),..的顺序)
删去0,当前状态这么做之后的逆序对数与目标状态奇偶性是否相同
(相同为1,不同为0)
B=当前状态0的行号与目标状态的奇偶性是否相同
有解要求满足:A==B
*/
bool judge()
{
int tmp[],x0,i,j;
tmp[]=;
for(i=;i<;++i)
for(j=;j<;++j)
if(a[i][j])
tmp[++tmp[]]=a[i][j];
else
x0=i;
//for(i=1;i<=15;++i)
// printf("1t%d\n",tmp[i]);
int a1=;
for(i=;i<=;++i)
for(j=;j<i;++j)
if(tmp[j]>tmp[i])
++a1;
//printf("2t%d %d\n",a1&1,3-x0);
return (a1&)==((-x0)&);
}
int main()
{
int i,j;
scanf("%d",&T);
for(int TT=;TT<=T;++TT)
{
fl=;len=;
for(i=;i<;++i)
for(j=;j<;++j)
{
scanf("%d",&a[i][j]);
if(a[i][j]==) x=i,y=j;
}
printf("Case %d: ",TT);
if(!judge())
{
puts("This puzzle is not solvable.");
continue;
}
for(maxd=;maxd<=;++maxd)
{
dfs(,);
//printf("1t%d\n",maxd);
if(fl) break;
}
//printf("2t%d\n",maxd);
if(fl)
{
for(i=len;i>=;--i)
printf("%c",now[i]);
puts("");
}
else
puts("This puzzle is not solvable.");
}
return ;
}
另有
https://cn.vjudge.net/problem/SCU-1110
https://cn.vjudge.net/problem/UVA-10181
15 Puzzle LightOJ - 1121的更多相关文章
- 15 Puzzle (4乘4谜题) IDA*(DFS策略与曼哈顿距离启发) 的C语言实现
大家好!这是我的第一篇博客,由于之前没有撰写博客的经验,并且也是初入计算机和人工智能领域,可能有些表述或者理解不当,还请大家多多指教. 一.撰写目的 由于这个学期在上算法与数据结构课程的时候,其中一个 ...
- 拼图游戏js
实现算法: 1. JavaScript动态生成拼图:通过生成16个div,且除最后一个div不使用背景图片以外,其他div都设置拼图图片为背景.然后通过调整background-position来实现 ...
- iOS 滑块拼图游戏(Puzzle8)
代码地址如下:http://www.demodashi.com/demo/11505.html 一.准备工作 先了解一个定义和定理 定义:在一个1,2,...,n的排列中,如果一对数的前后位置与大小顺 ...
- lightoj 1370 欧拉函数
A - Bi-shoe and Phi-shoe Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & % ...
- LightOJ 1236 - Pairs Forming LCM(素因子分解)
B - Pairs Forming LCM Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- LightOj 1289 - LCM from 1 to n(LCM + 素数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1289 题意:求LCM(1, 2, 3, ... , n)%(1<<32), ...
- LightOj 1278 - Sum of Consecutive Integers(求奇因子的个数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1278 题意:给你一个数n(n<=10^14),然后问n能用几个连续的数表示; 例 ...
- (最长公共子序列+推导)Love Calculator (lightOJ 1013)
http://www.lightoj.com/volume_showproblem.php?problem=1013 Yes, you are developing a 'Love calcula ...
- CoolShell Puzzle攻略[更新隐藏剧情]
CoolShell博主陈皓做了一个在线的puzzle很有意思,链接在这里,这里记录一下解题的一些步骤. Puzzle 0 ++++++++[>+>++>+++>++++> ...
随机推荐
- Jmeter-聚合报告
线程组右键--添加--监听器--聚合报告 Aggreagete Report:jmeter最常用的一个Listener,“聚合报告”. Label:每个jmeter的element(例如HTTP Re ...
- string类封装
class cMyString{ char* m_str; int m_strSize;public: cMyString();//指针指向一个空字符串 cMyString(char* str);// ...
- Linux网络编程socket错误分析
socket错误码: EINTR: 阻塞的操作被取消阻塞的调用打断.如设置了发送接收超时,就会遇到这种错误. 只能针对阻塞模式的socket.读,写阻塞的socket时,-1返回,错误号为INTR.另 ...
- 【LeetCode】070. Climbing Stairs
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- configured to save RDB snapshots, but is currently not able to persist o...
Redis问题 MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on d ...
- TCP点对点穿透探索--失败
TCP点对点穿透探索 点对点穿透是穿透什么 点对点穿透,需要实现的是对NAT的穿透.想实现NAT的穿透,当然要先了解NAT到底是什么,以及NAT是用来干什么的.NAT全称Network Address ...
- ScrollView cannot scroll in Slidinguppanellayout 解决办法
xml源码如下 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:an ...
- query builder 在线生成sql
http://devtools.korzh.com/easyquery/javascript/docs/javascript-query-builder-php
- AD 学习
http://blog.csdn.net/lingpaoershiyishiji/article/details/9139527
- 搭建Node+NPM+Grunt+Ruby开发环境
序 最近尝试了一下CoffeeScript,和Sass,不得不说这两个搭配起来的确是不错的选择,熟悉以后基本上开发就比较快速了. 当然要开发这个首先需要搭建环境,这里就需要有Node.NPM.Grun ...