Problem Statement for SortingGame


Problem Statement

In The Sorting Game, you are given a sequence containing a permutation of the integers between 1 and n, inclusive. In one move, you can take any k consecutive elements of the sequence and reverse their order. The goal of the game is to sort the sequence in ascending order. You are given a int[] board describing the initial sequence. Return the fewest number of moves necessary to finish the game successfully, or -1 if it's impossible.

Definition

Class:
SortingGame

Method:
fewestMoves

Parameters:
int[], int

Returns:
int

Method signature:
int fewestMoves(int[] board, int k)

(be sure your method is public)

Constraints

-
board will contain between 2 and 8 elements, inclusive.

-
Each integer between 1 and the size of board, inclusive, will appear in board exactly once.

-
k will be between 2 and the size of board, inclusive.

Examples

0)

{1,2,3}
3
Returns: 0

The sequence is already sorted, so we don't need any moves.

1)

{3,2,1}
3
Returns: 1

We can reverse the whole sequence with one move here.

2)

{5,4,3,2,1}
2
Returns: 10

This one is more complex.

3)

{3,2,4,1,5}
4
Returns: -1

4)

{7,2,1,6,8,4,3,5}
4
Returns: 7

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

This problem was used for:
Single Round Match 397 Round 1 - Division I, Level One
Single Round Match 397 Round 1 - Division II, Level Two

思路:

因为每组数据最多只有8个数,可以直接进行BFS

在BFS过程中遇到了一个问题:如何记录状态?这是一个最多8维的状态

我想法到了用HASH来解决这个问题,可是构造不出这个HASH函数

实在没办法了,找到了官方题解,竟然发现了一个map的奇技淫巧

map<vector<int>,int>

这TM也行。。。是在下输了。。。。

C++是最好的语言!!!

代码:

  1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 class SortingGame
6 {
7 public:
8 int fewestMoves(vector <int> board, int k)
9 {
10 queue<vector<int>> q;
11 map<vector<int>,int> vis;
12 q.push(board);vis[board]=0;
13 while(!q.empty()){
14 cout<<11111<<endl;
15 vector<int> vec=q.front();
16 q.pop();
17 int num=vis[vec];
18 int r=vec.size();
19 int i=0;
20 for(i=0;i<r-1;i++){
21 if(vec[i+1]<vec[i]) break;
22 }
23 if(i==r-1) return num;
24 for(int i=0;i+k<=r;i++){
25 vector<int> tmp=vec;
26 reverse(tmp.begin()+i,tmp.begin()+i+k);
27 if(vis.count(tmp)==0){
28 q.push(tmp);
29 vis[tmp]=num+1;
30 }
31 }
32 }
33 return -1;
34 }
35 };

TC-SRM391-div2-SortingGame(BFS,STL)的更多相关文章

  1. POJ 3126 Prime Path(BFS算法)

    思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...

  2. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  3. nyoj 21三个水杯(BFS + 栈)

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=21 思想: 看了一下搜索就来写了这题(BFS 找出最短路径 所以用此来进行搜索) 这题在 ...

  4. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  5. POJ 3669 Meteor Shower (BFS+预处理)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  6. hdu-1026(bfs+优先队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:输入n,m和一个n*m的矩阵, .表示通路: x表示墙: n表示有一个怪物,消灭它需要n个 ...

  7. HDU 3533 Escape(BFS+预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...

  8. CSU 1726: 你经历过绝望吗?两次!(bfs+优先队列)

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1726 1726: 你经历过绝望吗?两次! Submit Page    Summar ...

  9. Meteor Shower POJ - 3669 (bfs+优先队列)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26455   Accepted: 6856 De ...

随机推荐

  1. Tomcat解析XML和反射创建对象原理

    Tomcat解析XML和反射创建对象原理 import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Me ...

  2. [Bzoj1001][BeiJing2006]狼抓兔子(网络流/对偶图)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 看到大佬们都是对偶图过的,写了个最大流水过去了QAQ,网络流的无向图直接建双向边( ...

  3. SYN攻击源码

    一.linux下源代码实现/* syn flood by wqfhenanxc. * random soruce ip and random sourec port. * use #include & ...

  4. B.super_log(The Preliminary Contest for ICPC Asia Nanjing 2019)

    同:https://www.cnblogs.com/--HPY-7m/p/11444923.html #define IOS ios_base::sync_with_stdio(0); cin.tie ...

  5. 双指针(最大删除子串)Codeforces Round #579 (Div. 3)--Remove the Substring (hard version)

    题目链接:https://codeforces.com/contest/1203/problem/D2 题意: 给你S串.T串,问你最长删除多长的子串使得S串里仍然有T的子序列. 思路: 想了好久,先 ...

  6. 批量操作checkbox

    通过post可获取选中的checkbox的value值,然后可以action到某一页面通过$_POST[]处理得到的checkbox的值,然后进行批量化增删改查等操作. // 关键性语句: <i ...

  7. expdp使用

    原文:https://blog.csdn.net/zftang/article/details/6387325 ORACLE EXPDP命令使用详细相关参数以及导出示例: 1. DIRECTORY指定 ...

  8. 113、stack的优势 (Swarm20)

    参考https://www.cnblogs.com/CloudMan6/p/8157391.html   stack 将应用所包含的service,依赖的secret volume 等资源,以及他们之 ...

  9. 部署Flannel网络

    部署Flannel网络 部署flannel网络需要执行以下步骤: 1)写入分配的子网段到etcd,供flanneld使用 2)下载二进制包 3)配置Flannel 4)systemd管理Flannel ...

  10. kubernetes基本了解

    初识Kubernetes----k8s以及功能 kubernetes是由google公司开发的容器集群管理系统.采用go语言开发.也称为k8s,原因为k后面直到s这中间有8个字母,所以叫k8s.它主要 ...