https://cn.vjudge.net/problem/LightOJ-1121

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<vector>
  5. using namespace std;
  6. #define fi first
  7. #define se second
  8. #define mp make_pair
  9. #define pb push_back
  10. typedef long long ll;
  11. typedef unsigned long long ull;
  12. char /*ans[1011],*/now[];int len;
  13. int x,y,a[][];//xx1[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
  14. int xx2[][]={{,},{,},{,},{,},{,},{,},{,},{,},{,},
  15. {,},{,},{,},{,},{,},{,},{,}};
  16. bool fl;
  17. int T,n,maxd;
  18. inline int abs1(int x){return x>?x:-x;}
  19. //估价函数:除0以外,所有数当前位置与目标位置的曼哈顿距离之和
  20. int hh()
  21. {
  22. int i,j,cnt=;
  23. for(i=;i<;++i)
  24. for(j=;j<;++j)
  25. if(a[i][j])
  26. cnt+=abs1(i-xx2[a[i][j]][])+abs1(j-xx2[a[i][j]][]);
  27. return cnt;
  28. }
  29. inline void swap1(int &a,int &b)
  30. {
  31. int t=a;a=b;b=t;
  32. }
  33. //#define swap(a,b) {t=a;a=b;b=t;}
  34. #define swap swap1
  35. void dfs(int d,char pre)
  36. {
  37. //printf("1t%d\n",d);
  38. int h=hh();
  39. if(!h) {fl=;/*memcpy(ans,now,sizeof(char)*(len+1));*/return;}
  40. if(d+h>maxd) return;
  41. if(x!= && pre!='U')
  42. {
  43. //now[++len]='D';
  44. swap(a[x][y],a[x+][y]);++x;
  45. dfs(d+,'D');
  46. if(fl) {now[++len]='D';return;}
  47. //--len;
  48. --x;swap(a[x][y],a[x+][y]);
  49. }
  50. if(y!= && pre!='R')
  51. {
  52. //now[++len]='L';
  53. swap(a[x][y-],a[x][y]);--y;
  54. dfs(d+,'L');
  55. if(fl) {now[++len]='L';return;}
  56. //--len;
  57. ++y;swap(a[x][y-],a[x][y]);
  58. }
  59. if(y!= && pre!='L')
  60. {
  61. //now[++len]='R';
  62. swap(a[x][y+],a[x][y]);++y;
  63. dfs(d+,'R');
  64. if(fl) {now[++len]='R';return;}
  65. //--len;
  66. --y;swap(a[x][y+],a[x][y]);
  67. }
  68. if(x!= && pre!='D')//不要让操作刚好抵消上一次操作,实测很有效
  69. {
  70. //now[++len]='U';
  71. swap(a[x][y],a[x-][y]);--x;
  72. dfs(d+,'U');
  73. if(fl) {now[++len]='U';return;}
  74. //--len;
  75. ++x;swap(a[x][y],a[x-][y]);
  76. }
  77. }
  78. /*
  79. 判无解,
  80. https://blog.csdn.net/obsorb_knowledge/article/details/79915484
  81. A=将16个数排成一行,((0,0),(0,1),(0,2),(0,3),(1,0),(1,1),..的顺序)
  82. 删去0,当前状态这么做之后的逆序对数与目标状态奇偶性是否相同
  83. (相同为1,不同为0)
  84. B=当前状态0的行号与目标状态的奇偶性是否相同
  85. 有解要求满足:A==B
  86. */
  87. bool judge()
  88. {
  89. int tmp[],x0,i,j;
  90. tmp[]=;
  91. for(i=;i<;++i)
  92. for(j=;j<;++j)
  93. if(a[i][j])
  94. tmp[++tmp[]]=a[i][j];
  95. else
  96. x0=i;
  97. //for(i=1;i<=15;++i)
  98. // printf("1t%d\n",tmp[i]);
  99. int a1=;
  100. for(i=;i<=;++i)
  101. for(j=;j<i;++j)
  102. if(tmp[j]>tmp[i])
  103. ++a1;
  104. //printf("2t%d %d\n",a1&1,3-x0);
  105. return (a1&)==((-x0)&);
  106. }
  107. int main()
  108. {
  109. int i,j;
  110. scanf("%d",&T);
  111. for(int TT=;TT<=T;++TT)
  112. {
  113. fl=;len=;
  114. for(i=;i<;++i)
  115. for(j=;j<;++j)
  116. {
  117. scanf("%d",&a[i][j]);
  118. if(a[i][j]==) x=i,y=j;
  119. }
  120. printf("Case %d: ",TT);
  121. if(!judge())
  122. {
  123. puts("This puzzle is not solvable.");
  124. continue;
  125. }
  126. for(maxd=;maxd<=;++maxd)
  127. {
  128. dfs(,);
  129. //printf("1t%d\n",maxd);
  130. if(fl) break;
  131. }
  132. //printf("2t%d\n",maxd);
  133. if(fl)
  134. {
  135. for(i=len;i>=;--i)
  136. printf("%c",now[i]);
  137. puts("");
  138. }
  139. else
  140. puts("This puzzle is not solvable.");
  141. }
  142. return ;
  143. }

另有

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. BZOJ 1192 [HNOI2006]鬼谷子的钱袋:二进制 砝码称重问题

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1192 题意: 鬼谷子带了a元钱,他要把a元钱分装在小袋子中,使得任意不大于a的数目的钱,都 ...

  2. python基础-正则2

    正则函数 Python提供re模块,包含所有正则表达式的功能 由于python的字符串本身也有\转义,所以需要注意: s = "ABC\\-001" 对应的正则表达式应为:'ABC ...

  3. Mybatis异常_02_Result Maps collection already contains value for

    一.异常 1.异常信息 2.异常原因 XXXMapper.xml文件中存在重名对象,保持名称不要一样即可正常启动. 我的原因是namespace与其他mapper 一样. 3.可能的原因 (1)nam ...

  4. 详解C/C++ 编译 g++ gcc 的区别

    我们在编译c/c++代码的时候,有人用gcc,有人用g++,于是各种说法都来了,譬如c代码用gcc,而c++代码用g++, 或者说编译用gcc,链 接用g++,一时也不知哪个说法正确,如果再遇上个ex ...

  5. python 案例之老王开枪

  6. 1080 Graduate Admission (30)(30 分)

    It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...

  7. ACM学习历程—HDU 2795 Billboard(线段树)

    Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...

  8. poj1191棋盘分割——区间DP

    题目:http://poj.org/problem?id=1191 分析题意,可知每次要沿棋盘中的一条线把一块一分为二,取其中一块继续分割: σ最小经分析可知即为每块的xi和的平方最小: 故用区间DP ...

  9. Selenium Webdriver元素定位

    http://www.cnblogs.com/qingchunjun/p/4208159.html

  10. 排名Top 16的Java实用类库

    (转载: http://www.hollischuang.com/archives/1606) github地址: https://github.com/liufeiSAP/javaStudy.git ...