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 | |||||||||||||
| |||||||||||||
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) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|
#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;
}
#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(),等,应该需要熟悉!!