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的更多相关文章

  1. 15 Puzzle (4乘4谜题) IDA*(DFS策略与曼哈顿距离启发) 的C语言实现

    大家好!这是我的第一篇博客,由于之前没有撰写博客的经验,并且也是初入计算机和人工智能领域,可能有些表述或者理解不当,还请大家多多指教. 一.撰写目的 由于这个学期在上算法与数据结构课程的时候,其中一个 ...

  2. 拼图游戏js

    实现算法: 1. JavaScript动态生成拼图:通过生成16个div,且除最后一个div不使用背景图片以外,其他div都设置拼图图片为背景.然后通过调整background-position来实现 ...

  3. iOS 滑块拼图游戏(Puzzle8)

    代码地址如下:http://www.demodashi.com/demo/11505.html 一.准备工作 先了解一个定义和定理 定义:在一个1,2,...,n的排列中,如果一对数的前后位置与大小顺 ...

  4. lightoj 1370 欧拉函数

    A - Bi-shoe and Phi-shoe Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & % ...

  5. LightOJ 1236 - Pairs Forming LCM(素因子分解)

    B - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  6. LightOj 1289 - LCM from 1 to n(LCM + 素数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1289 题意:求LCM(1, 2, 3, ... , n)%(1<<32), ...

  7. LightOj 1278 - Sum of Consecutive Integers(求奇因子的个数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1278 题意:给你一个数n(n<=10^14),然后问n能用几个连续的数表示; 例 ...

  8. (最长公共子序列+推导)Love Calculator (lightOJ 1013)

    http://www.lightoj.com/volume_showproblem.php?problem=1013   Yes, you are developing a 'Love calcula ...

  9. CoolShell Puzzle攻略[更新隐藏剧情]

    CoolShell博主陈皓做了一个在线的puzzle很有意思,链接在这里,这里记录一下解题的一些步骤. Puzzle 0 ++++++++[>+>++>+++>++++> ...

随机推荐

  1. 【HDU2050】折线分割平面

    Position Solution 2×n^2-n+1 证明见分割问题 Code // This file is made by YJinpeng,created by XuYike's black ...

  2. BaseServlet优化Servlet,实现类似struts2的一些简单效果

    package cn.itcast.web.servlet; import java.io.IOException; import javax.servlet.ServletException; im ...

  3. 1091 Acute Stroke (30)(30 分)

    One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...

  4. 关于CDH

    进入到任何一个Host的页面,点击“components",就可以看到这个主机安装的组件的版本

  5. .Net 学习资源整理

    01.Visual Studio 隐藏的财富 --- C# 语言规范 安装完Visual Studio之后,我们好像忽略了,微软给我们准备的<C# 语言规范>. 路径参考下图: 02.MS ...

  6. python中列表元组字符串相互转换

    python中有三个内建函数:列表,元组和字符串,他们之间的互相转换使用三个函数,str(),tuple()和list(),具体示例如下所示: >>> s = "xxxxx ...

  7. JavaScript总结(1)

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  8. npm下载模块提速方法

    通过config配置指向国内镜像源,命令如下 npm config set registry https://registry.npm.taobao.org 然后可以查看是否配置成功 npm conf ...

  9. Ubuntu下如何禁用IPv6

    Ubuntu下如何禁用IPv6 2013-10-16 11:32:02 分类: HADOOP      分布式下的hadoop/hbase运行总出问题,zookeeper连接总是出问题,怀疑可能是ip ...

  10. win32常用代码整理

    1.ShellExecute [Use ShellAPI] ShellExecute(Handle, 'open', 'http://www.cnblogs.com/lovelp/', nil, ni ...