蓝桥杯 历届试题 九宫重排
2016-04-08 09:52
246 查看
登录后才能查看试题。
历届试题 九宫重排
时间限制:1.0s 内存限制:256.0MB
锦囊1
锦囊2
锦囊3
问题描述
如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着。与空格子相邻的格子中的卡片可以移动到空格中。经过若干次移动,可以形成第二个图所示的局面。
我们把第一个图的局面记为:12345678.
把第二个图的局面记为:123.46758
显然是按从上到下,从左到右的顺序记录数字,空格记为句点。
本题目的任务是已知九宫的初态和终态,求最少经过多少步的移动可以到达。如果无论多少步都无法到达,则输出-1。
输入格式
输入第一行包含九宫的初态,第二行包含九宫的终态。
输出格式
输出最少的步数,如果不存在方案,则输出-1。
样例输入
12345678.
123.46758
样例输出
3
样例输入
13524678.
46758123.
样例输出
22
历届试题 九宫重排
时间限制:1.0s 内存限制:256.0MB
锦囊1
锦囊2
锦囊3
问题描述
如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着。与空格子相邻的格子中的卡片可以移动到空格中。经过若干次移动,可以形成第二个图所示的局面。
我们把第一个图的局面记为:12345678.
把第二个图的局面记为:123.46758
显然是按从上到下,从左到右的顺序记录数字,空格记为句点。
本题目的任务是已知九宫的初态和终态,求最少经过多少步的移动可以到达。如果无论多少步都无法到达,则输出-1。
输入格式
输入第一行包含九宫的初态,第二行包含九宫的终态。
输出格式
输出最少的步数,如果不存在方案,则输出-1。
样例输入
12345678.
123.46758
样例输出
3
样例输入
13524678.
46758123.
样例输出
22
#include <queue> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <iostream> using namespace std; const int maxn = 1000000; int str[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; char direction[5] = "udlr"; int fac[] = {1,1,2,6,24,120,720,5040,40320,362880}; //这里就是从0的阶乘到9的阶乘 int vis[maxn],aim; struct Node { int s[9]; int loc; int status; int step; string path; }pre,cur; int cantor(int *s) //康拓展开 { int ans = 0; for(int i = 0; i < 9; i++) { int num = 0; for(int j = i+1; j < 9; j++) if(s[i] > s[j]) num++; ans += (fac[9-i-1]*num); } return ans+1; } void BFS() { memset(vis,0,sizeof(vis)); queue <Node> q; vis[pre.status] = 1; q.push(pre); while(!q.empty()) { pre = q.front(); q.pop(); int x = pre.loc/3; int y = pre.loc%3; if(pre.status == aim) { printf("%d\n",pre.step); return; } for(int i = 0; i < 4; i++) { int xx = x + str[i][0]; int yy = y + str[i][1]; if(xx < 0 || xx >= 3 || yy < 0 || yy >= 3) continue; cur = pre; cur.loc = xx*3 + yy; cur.s[pre.loc] = cur.s[cur.loc]; cur.s[cur.loc] = 0; cur.status = cantor(cur.s); if(!vis[cur.status]) { ++cur.step; vis[cur.status] = 1; cur.path = direction[i] + cur.path; q.push(cur); } } } } int main() { char a[10]; while(scanf("%s", a) != EOF) { for(int i = 0; i < 9; i++) { if(a[i] == '.') { pre.s[i] = 0; pre.loc = i; } else pre.s[i] = a[i] - '0'; } pre.status = cantor(pre.s); pre.step = 0; scanf("%s", a); for(int i = 0; i < 9; i++) { if(a[i] == '.') cur.s[i] = 0; else cur.s[i] = a[i] - '0'; } aim = cantor(cur.s); BFS(); } return 0; }
相关文章推荐
- SUBLIME TEXT 3 CHEAT SHEET
- 【追求进步】数字在排序数组中出现的次数
- 在MyBatis+SpringMVC中进行单元测试
- POCO库下访问http获取json串
- iOS线程安全问题
- GPU 加速下的图像处理
- 在spring中用quartz实现定时调度
- ROS知识(12)----cv_bridge依赖opencv版本的问题
- 公司笔记
- ajax 上传参数 下载excel文件
- LeetCode 28 . Implement strStr()
- PS将一辆汽车放到草原上
- linux loop device介绍
- iOS和Android图标大小
- Linux exit --退出当前shell
- ubuntu下Hbase 单机模式配置
- 复杂的声明
- ios开发之判断对象的等同性isEqual
- UICollectionView实例1
- 按钮的裁剪拉伸1