TC-SRM391-div2-SortingGame(BFS,STL)
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)的更多相关文章
- POJ 3126 Prime Path(BFS算法)
思路:宽度优先搜索(BFS算法) #include<iostream> #include<stdio.h> #include<cmath> #include< ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- nyoj 21三个水杯(BFS + 栈)
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=21 思想: 看了一下搜索就来写了这题(BFS 找出最短路径 所以用此来进行搜索) 这题在 ...
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- hdu-1026(bfs+优先队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:输入n,m和一个n*m的矩阵, .表示通路: x表示墙: n表示有一个怪物,消灭它需要n个 ...
- HDU 3533 Escape(BFS+预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...
- CSU 1726: 你经历过绝望吗?两次!(bfs+优先队列)
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1726 1726: 你经历过绝望吗?两次! Submit Page Summar ...
- Meteor Shower POJ - 3669 (bfs+优先队列)
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26455 Accepted: 6856 De ...
随机推荐
- Fidder抓包设置
1, 谷歌浏览器中:
- Feign声明式服务调用
Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. Spring Clo ...
- 3d旋转焦点图
在线演示 本地下载
- macOS Sierra 如何安装任何来源的软件
为了安全性考虑,macos是要手动勾选来自任何来源的选项才可以安装第三方应用软件,系统升级后,在新的系统中这一项是默认不显示的,如果想要出现和这一勾选选项,可以从终端中输入 sudo spctl -- ...
- golang(5):struct & 链表 & 二叉树 & 接口
struct : 结构体 // 1. 用来自定义复杂数据结构 // 2. struct里面可以包含多个字段(属性) // 3. struct类型可以定义方法,注意和函数的区分 // 4. struct ...
- python发起post请求获取json数据使用requests方法
最普通的答案 我一直就觉得GET和POST没有什么除了语义之外的区别,自打我开始学习Web编程开始就是这么理解的 . 可能很多人都已经猜到了答案是: 1.GET 使用URL或Cookie传参.而POS ...
- JavaScript金字塔打印
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 正则替换replace中$1的用法
一.repalce定义 用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 1 2 3 4 5 stringObject.replace(regexp/substr,repla ...
- Idea java 程序打jar包(maven)
1.准备好控制台程序 2.引用的项目打包(公共类接口) 3.开发打包 点击运行 打包结果如下
- Excel 曝Power Query安全漏洞
近日,Mimecast 威胁中心的安全研究人员,发现了微软 Excel 电子表格应用程序的一个新漏洞,获致 1.2 亿用户易受网络攻击.其指出,该安全漏洞意味着攻击者可以利用 Excel 的 Powe ...