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 ++++++++[>+>++>+++>++++> ...
随机推荐
- BZOJ 1192 [HNOI2006]鬼谷子的钱袋:二进制 砝码称重问题
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1192 题意: 鬼谷子带了a元钱,他要把a元钱分装在小袋子中,使得任意不大于a的数目的钱,都 ...
- python基础-正则2
正则函数 Python提供re模块,包含所有正则表达式的功能 由于python的字符串本身也有\转义,所以需要注意: s = "ABC\\-001" 对应的正则表达式应为:'ABC ...
- Mybatis异常_02_Result Maps collection already contains value for
一.异常 1.异常信息 2.异常原因 XXXMapper.xml文件中存在重名对象,保持名称不要一样即可正常启动. 我的原因是namespace与其他mapper 一样. 3.可能的原因 (1)nam ...
- 详解C/C++ 编译 g++ gcc 的区别
我们在编译c/c++代码的时候,有人用gcc,有人用g++,于是各种说法都来了,譬如c代码用gcc,而c++代码用g++, 或者说编译用gcc,链 接用g++,一时也不知哪个说法正确,如果再遇上个ex ...
- python 案例之老王开枪
- 1080 Graduate Admission (30)(30 分)
It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...
- ACM学习历程—HDU 2795 Billboard(线段树)
Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...
- poj1191棋盘分割——区间DP
题目:http://poj.org/problem?id=1191 分析题意,可知每次要沿棋盘中的一条线把一块一分为二,取其中一块继续分割: σ最小经分析可知即为每块的xi和的平方最小: 故用区间DP ...
- Selenium Webdriver元素定位
http://www.cnblogs.com/qingchunjun/p/4208159.html
- 排名Top 16的Java实用类库
(转载: http://www.hollischuang.com/archives/1606) github地址: https://github.com/liufeiSAP/javaStudy.git ...