hihocoder 1233 Boxes
题意:类汉诺塔的一个东西……移动规则与汉诺塔一样,但初始状态为题目中给出的每根棍上一个盘子,目标状态为盘子在棍上按大小顺序排列,盘子只能在相邻的棍儿上移动。
解法:广搜并打表记录从目标状态到所有可能的初始状态的答案。我记录每个盘子的位置为状态,vis用七位数组(被队友吐槽还真敢写啊=3=),然后每次转移状态的时候转化为每个棍儿上最小的盘子是啥(反正很抽象又不好解释……我懂就好啦哈哈哈哈哈哈哈哈哈哈(被队友嘲笑自暴自弃中))。答案的记录用转化为排列序数的方式。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int ans[8][6000];
int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int order(int v[], int len)//将排列转化为排列序数
{
int i, j, temp, num;
num = 0;
for(i = 0; i < len - 1; i++)
{
temp = 0;
for(j = i + 1; j < len; j++)
{
if(v[j] < v[i])
temp++;
}
num += fac[v[i] - 1] * temp;
}
return num;
}
struct node
{
int a[10], step;//a数组表示每个盘子放的棍儿的序号
node(int tmp[], int tstep)
{
step = tstep;
memcpy(a, tmp, sizeof a);
}
node() {}
};
bool vis[8][8][8][8][8][8][8];//记录状态
queue <node> q;
bool isanswer(int a[], int n)
{
int stick[8] = {0, 100, 100, 100, 100, 100, 100, 100};
for(int i = 0; i < n; i++)
stick[a[i]] = min(stick[a[i]], i + 1);
for(int i = 1; i <= n; i++)
if(stick[i] == 100) return false;
return true;
}
void bfs(int a[], int n)
{
while(!q.empty()) q.pop();
memset(vis, 0, sizeof vis);
q.push(node(a, 0));
vis[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]] = 1;
while(!q.empty())
{
node tmp = q.front();
q.pop();
if(isanswer(tmp.a, n))
{
ans[n][order(tmp.a, n)] = tmp.step;
}
int stick[8] = {0, 100, 100, 100, 100, 100, 100, 100};
for(int i = 0; i < n; i++)
stick[tmp.a[i]] = min(stick[tmp.a[i]], i + 1);//算一下每根棍儿上最小的盘子是啥
for(int i = 1; i <= n; i++)
{
if(stick[i] == 100) continue;
if(i > 1 && (stick[i] < stick[i - 1] || (stick[i] != 100 && stick[i - 1] == 100)))//如果左面有棍儿且棍儿上最小的盘子比当前盘子大或者左面棍儿上没有盘子就转移状态
{
tmp.a[stick[i] - 1] = i - 1;
if(!vis[tmp.a[0]][tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]][tmp.a[5]][tmp.a[6]])
{
vis[tmp.a[0]][tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]][tmp.a[5]][tmp.a[6]] = 1;
q.push(node(tmp.a, tmp.step + 1));
}
tmp.a[stick[i] - 1] = i;
}
if(i < n && (stick[i] < stick[i + 1] || (stick[i] != 100 && stick[i + 1] == 100)))//同上
{
tmp.a[stick[i] - 1] = i + 1;
if(!vis[tmp.a[0]][tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]][tmp.a[5]][tmp.a[6]])
{
vis[tmp.a[0]][tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]][tmp.a[5]][tmp.a[6]] = 1;
q.push(node(tmp.a, tmp.step + 1));
}
tmp.a[stick[i] - 1] = i;
}
}
}
}
void init()
{
memset(ans, -1, sizeof ans);
int a[] = {1, 2, 3, 4, 5, 6, 7};
for(int i = 1; i < 8; i++)
bfs(a, i);
}
int main()
{
init();
int T;
scanf("%d", &T);
while(T--)
{
int n, a[10], input[10];
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", &input[i]);
map <int, int> m;
for(int i = 0; i < n; i++)
m[input[i]] = i + 1;
map <int, int> :: iterator ite = m.begin();
for(int i = 0; ite != m.end(); ite++, i++) a[i] = ite -> second;
int tmp = order(a, n);
if(~ans[n][tmp])
printf("%d\n", ans[n][tmp]);
else
printf("-1\n");
}
return 0;
}
hihocoder 1233 Boxes的更多相关文章
- hihoCoder 1233 : Boxes(盒子)
hihoCoder #1233 : Boxes(盒子) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 There is a strange ...
- ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)
hihoCoder挑战赛12 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There is a strange storehouse in PKU. In this ...
- 2015北京网络赛 G题 Boxes bfs
Boxes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingonl ...
- 2015年北京网赛 boxes(bfs)
题目链接: http://hihocoder.com/problemset/problem/1233 题目描述: 给定最多七个箱子,每个箱子的重量都不相同,每次都可以将一个箱子放在相邻的位置上,如果相 ...
- hihocoder -1121-二分图的判定
hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...
- Hihocoder 太阁最新面经算法竞赛18
Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...
- hihoCoder太阁最新面经算法竞赛15
hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...
- 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II
http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...
- 【hihocoder#1413】Rikka with String 后缀自动机 + 差分
搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...
随机推荐
- HDU4276 The Ghost Blows Light SPFA&&树dp
题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739 做这道题主要是为了加强自己对SPFA的代码的训练 ...
- SGU 114
114. Telecasting station time limit per test: 0.25 sec. memory limit per test: 4096 KB Every city in ...
- lintcode 中等题:find the missing number 寻找缺失的数
题目 寻找缺失的数 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数. 样例 N = 4 且序列为 [0, 1, 3] 时,缺失的数为2. 注意 可以改变序 ...
- 解决Eclipse10配置Pydev不成功的问题
本人在线配置还在本地配置后 重启Eclipse,Windows-Preferences中并无Python选项,新建项目也无Python可选 这个尝试了好多种方法,重新安装Eclipse10,重新安装j ...
- Matlab中编译C++文件
今天在跑<Robust Object Tracking via Sparsity-based Collaborative Model>这篇文章的代码时候,发现出现如下错误: 发现错误时由于 ...
- java小程序:求完全数
如果一个数等于它的不包括自身的所有因数之和,那么这个数就叫完全数.例如,6的不包括自身的所有因数为1,2,3,而且6=1+2+3,所以6是完全数. 大约2200多年前,欧几里德提出:如果2n-1是质数 ...
- pymongo 例子
import pymongo class dbUtil(object): def __init__(self, tablename='functional_testing'): con = pymon ...
- Python中的SET集合操作
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...
- QC、IQC、IPQC、FQC、OQC、QA分别的定义
QC:即英文(Quality Control)的简称,中文意义是品质控制,其在ISO8402:1994的定义是“为达到品质要求所采取的作业技术的活动”.有些推行ISO9000的组织会设置这样一个部门或 ...
- Java IDE 编辑器 --- IntelliJ IDEA 进阶篇 生成 hibernate 实体与映射文件
原文:转:Java IDE 编辑器 --- IntelliJ IDEA 进阶篇 生成 hibernate 实体与映射文件 2011-04-30 12:50 很多人不知道怎么用 IntelliJ IDE ...