SRM397 div2 500point(use bfs algorithm)

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 vector <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: vector <int>, int
Returns: int
Method signature: int fewestMoves(vector <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
 
#include < iostream >
#include
< stdlib.h >
#include
< vector >
#include
< sstream >
#include
< string >
#include
< queue >
#include
< map >
#include
< stack >
#include
< math.h >
#include
< algorithm >
using   namespace  std;

/*BSF algorithm */
//  rec to give the change sequence;
class  SortingGame
{
    
public:
        
int fewestMoves(vector <int> board, int k);
    
}
;

int  SortingGame::fewestMoves(vector  < int >  board,  int  k)
{
    
int i(0);
    
int res(0);
    stack
<int>seq;
    map
<vector<int>,int> path;
    path[board] 
= 0;
    queue
< vector<int> > Q;
    Q.push(board);
    vector
<int>u;
    vector
<int> w ;
    map
< vector<int> ,string > rec; 
    
string s = "";
    
char c;
    rec[board] 
= "0";
    
while(!Q.empty())
    
{
        u 
= Q.front();
        Q.pop();
        res 
= path[u];
        
        
for(i = 1;i<u.size();i++)
        
{
            
if(u[i]<u[i-1])
                
break;
        }

        
if(i == u.size())
        
{
            cout
<<rec[u]<<endl;
            
return res;
        }

        
        
for(i = 0;i+k<=u.size();i++)
        
{
            w 
= u;
            s 
= rec[w];
            reverse(w.begin() 
+ i, w.begin() + i + k);
            c 
= '0'+i;
            s 
+= c;
            
if(path.count(w) == 0)
            
{
                rec[w] 
= s;
                path[w] 
= res+1;
                Q.push(w);
            }

        }

    }

    
return -1;
}

        
        
        

广度优先算法,搜索空间比较大,当规模较少时,可以利用bfs去得到最优解(由初始状态经过一系列变换得到目标状态),bfs主要利用了队列,实行先进先出,从而使每个节点最多只访问一次,每次当访问子结点时,都同时访问其兄弟节点,然后继续下一层访问子结点,逐层处理!

上面的算法我利用了c++的stl,map,queue,来实现了题目所要求内容,发现map还是挺好用的,上面利用一个map,来保留每次翻转过程,使得最后可以看到rec【u】保留地是u是如何经过变换由初始状态得到的!

其中估计要经常用到的几个algorithm有:find(),count(),pop(),front(),top(),等,应该需要熟悉!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值