zoukankan      html  css  js  c++  java
  • 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 };
    View Code
  • 相关阅读:
    decimal赋值
    "errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest hint: [d0tQ_02368635
    scope.row中属性值展示
    echarts.baidu 官网
    PowerDesigner使用
    解决 office2016 意外的严重错误 不能启动程序
    Jenkins 关闭和重启
    解决 weblogic poi3.9 报错 a different type with name "javax/xml/namespace/QName"
    WINDOWS7 下 xclient 802.1x 客户端 停止运行的解决办法
    前台进程转后台运行
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/6913025.html
Copyright © 2011-2022 走看看