1077 Eight
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 37738 | Accepted: 15932 | Special Judge |
Description
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 x
where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
r-> d-> r->
The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively.
Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course).
In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.
Input
1 2 3
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
Output
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr
Source
int contor(int ar[]) {
int v = ;
for (int i = ; i < ; i++) {
int num = ;
for (int j = i + ; j < ; j++) {
if (_______) num++;
}
v += num * fac[ - i];
}
return v + 1;
}
#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
int fac[];
int dx[] = {-, , , };
int dy[] = {, , , -};
char d[] = {'u','d','r','l'};
const int goal = , maxv = 4e5;
bool vis[maxv];
int pre[maxv];
char dir[maxv];
int dish(int ar[]) {
int dis = ;
for (int i = ; i < ; i++) {
if (ar[i] == ) continue;
int v = ar[i] - ;
int rx = i / , ry = i % ;
int x = v / , y = v % ;
dis += abs(x - rx) + abs(ry - y);
}
return dis;
}
int contor(int ar[]) {
int v = ;
for (int i = ; i < ; i++) {
int num = ;
for (int j = i + ; j < ; j++) if (ar[j] < ar[i]) num++; v += num * fac[ - i];
}
return v + ;
}
struct node {
int g, h, f, sta, xpos;
int s[];
void init() {
g++; h=dish(s); f=h+g; sta=contor(s);
}
bool operator < (const node x)const {
return f > x.f;
}
};
priority_queue<node> q;
bool Astar(node now) {
fill(vis, vis + maxv, );
while (!q.empty()) q.pop();
now.g = -;
now.init();
q.push(now);
pre[now.sta] = -;
vis[now.sta] = ;
while (!q.empty()){
now = q.top(); q.pop();
if (now.sta == goal) return true;
int xpos = now.xpos;
int x = xpos / , y = xpos % ;
for (int i = ; i < ; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx < || ty < || tx >= || ty >= ) continue;
int newp = tx * + ty;
node tp = now;
tp.xpos = newp;
swap(tp.s[newp], tp.s[xpos]);
tp.init(); if (vis[tp.sta]) continue;
vis[tp.sta] = ;
pre[tp.sta] = now.sta;
dir[tp.sta] = d[i];
q.push(tp);
}
}
return false;
}
void Print(int sta) {
if (pre[sta] == -) return;
Print(pre[sta]);
printf("%c", dir[sta]);
}
int main() {
fac[] = ;
for (int i = ; i < ; i++) fac[i] = i * fac[i - ];
char str[];
while (gets(str)) {
int cnt = ;
node now;
for (int i = ; str[i]; i++) {
if (str[i] == ' ') continue;
if (str[i] == 'x') {
now.xpos = cnt;
now.s[cnt++] = ;
}
else
now.s[cnt++] = str[i] - '';
}
if (Astar(now)) Print(goal);
else printf("unsolvable");
printf("\n");
}
return ;
}
1077 Eight的更多相关文章
- hihocode 1077 : RMQ问题再临-线段树
#1077 : RMQ问题再临-线段树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到:小Hi给小Ho出了这样一道问题:假设整个货架上从左到右摆放了N种商品,并 ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- LightOJ::1077 -----奇妙的最大公约数
题目:http://www.lightoj.com/volume_showproblem.php?problem=1077 题意:在平面上, 给出两个点的坐标 例如:(x, y) 其中x, y 都是整 ...
- POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...
- hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...
- 九度OJ 1077 最大序列和 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1077 题目描述: 给出一个整数序列S,其中有N个数,定义其中一个非空连续子序列T中所有数的和为T的“序列和”. 对 ...
- hihocoder 1077线段树
http://hihocoder.com/problemset/problem/1077 #include <bits/stdc++.h> using namespace std; #de ...
- poj 1077 Eight(双向bfs)
题目链接:http://poj.org/problem?id=1077 思路分析:题目要求在找出最短的移动路径,使得从给定的状态到达最终状态. <1>搜索算法选择:由于需要找出最短的移动路 ...
- UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)
传送门1: https://uva.onlinejudge.org/external/5/536.pdf 传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1 ...
- PAT 1077 Kuchiguse
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Person ...
随机推荐
- wordpress百度熊掌号“搜索结果出图”改造代码
<?php if(is_single()||is_page()){ echo '<script type="application/ld+json">{ &quo ...
- Win10系列:C#应用控件进阶8
LineGeometry LineGeometry控件通过指定直线的起点和终点来定义线.LineGeometry对象无法进行自我绘制,因此同样需要使用 Path元素来辅助呈现.LineGeometry ...
- MySQL— 索引,视图,触发器,函数,存储过程,执行计划,慢日志,分页性能
一.索引,分页性能,执行计划,慢日志 (1)索引的种类,创建语句,名词补充(最左前缀匹配,覆盖索引,索引合并,局部索引等): import sys # http://www.cnblogs.com/w ...
- javascript事件流机制
(1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div ...
- python之路-----MySql操作
一.概述 1.什么是数据库 数据库就是按照数据结构来组织.存储和管理数据的仓库.如我们创建的文件夹,就是一个数据库. 2.什么是mysql,oracle,access,sqlit等? 他们都是一款软件 ...
- 关于memset函数--赋最大值
问题起源: 这几天在刷CCF的时候,图论那边经常用到赋最大值,一开始自己一直手工for循环赋值(INT_MAX或者是LONG_LONG_MAX),后来看到别人的代码,发现了一个比较高端的赋值 mem ...
- FORTH 虚拟机内部结构
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- python 捕获异常顺序
catch 异常的时候,有关的异常(若是抛出子类异常,则父类异常的except也算.反之不算)except的语句是按代码顺序执行, 也就是说,当一个异常发生时,从若干except中若遇见异常类基类,父 ...
- winfrom窗体中嵌套WPF控件
前言 本文主要介绍如何在winfrom窗体中嵌套WPF控件, 一来是自己记录一下,而来希望能对有需要的朋友提供实现思路. 如有错误请指出...下面进入正题... -1.前期准备 准备一个建立好的win ...
- php短信验证码接口接入流程及代码示例
对于绝大部分企业来说,所使用的短信验证码接口都是第三方短信服务商所提供,目前市场上短信服务商有很多,在此向大家推荐一家动力思维乐信,运营13年,值得信赖! 就拿动力思维乐信短信验证码接口为例,详细介绍 ...