魔方机器人matlab编程,C++实现超赞的解魔方的机器人代码
C++实现超赞的解魔方的机器人代码,这段代码精简实用,作者的脑子不知道是怎么长的,厉害。
/**********************************************************************
*
* A cube 'state' is a vector with 40 entries, the first 20
* are a permutation of {0,...,19} and describe which cubie is at
* a certain position (regarding the input ordering). The first
* twelve are for edges, the last eight for corners.
*
* The last 20 entries are for the orientations, each describing
* how often the cubie at a certain position has been turned
* counterclockwise away from the correct orientation. Again the
* first twelve are edges, the last eight are corners. The values
* are 0 or 1 for edges and 0, 1 or 2 for corners.
*
* http://www.sharejs.com
**********************************************************************/
#include
#include
#include
#include
#include
#include
using namespace std;
//----------------------------------------------------------------------
typedef vector vi;
//----------------------------------------------------------------------
int applicableMoves[] = { 0, 262143, 259263, 74943, 74898 };
// TODO: Encode as strings, e.g. for U use "ABCDABCD"
int affectedCubies[][8] = {
{ 0, 1, 2, 3, 0, 1, 2, 3 }, // U
{ 4, 7, 6, 5, 4, 5, 6, 7 }, // D
{ 0, 9, 4, 8, 0, 3, 5, 4 }, // F
{ 2, 10, 6, 11, 2, 1, 7, 6 }, // B
{ 3, 11, 7, 9, 3, 2, 6, 5 }, // L
{ 1, 8, 5, 10, 1, 0, 4, 7 }, // R
};
vi applyMove ( int move, vi state ) {
int turns = move % 3 + 1;
int face = move / 3;
while( turns-- ){
vi oldState = state;
for( int i=0; i<8; i++ ){
int isCorner = i > 3;
int target = affectedCubies[face][i] + isCorner*12;
int killer = affectedCubies[face][(i&3)==3 ? i-3 : i+1] + isCorner*12;;
int orientationDelta = (i<4) ? (face>1 && face<4) : (face<2) ? 0 : 2 - (i&1);
state[target] = oldState[killer];
//state[target+20] = (oldState[killer+20] + orientationDelta) % (2 + isCorner);
state[target+20] = oldState[killer+20] + orientationDelta;
if( !turns )
state[target+20] %= 2 + isCorner;
}
}
return state;
}
int inverse ( int move ) {
return move + 2 - 2 * (move % 3);
}
//----------------------------------------------------------------------
int phase;
//----------------------------------------------------------------------
vi id ( vi state ) {
//--- Phase 1: Edge orientations.
if( phase < 2 )
return vi( state.begin() + 20, state.begin() + 32 );
//-- Phase 2: Corner orientations, E slice edges.
if( phase < 3 ){
vi result( state.begin() + 31, state.begin() + 40 );
for( int e=0; e<12; e++ )
result[0] |= (state[e] / 8) << e;
return result;
}
//--- Phase 3: Edge slices M and S, corner tetrads, overall parity.
if( phase < 4 ){
vi result( 3 );
for( int e=0; e<12; e++ )
result[0] |= ((state[e] > 7) ? 2 : (state[e] & 1)) << (2*e);
for( int c=0; c<8; c++ )
result[1] |= ((state[c+12]-12) & 5) << (3*c);
for( int i=12; i<20; i++ )
for( int j=i+1; j<20; j++ )
result[2] ^= state[i] > state[j];
return result;
}
//--- Phase 4: The rest.
return state;
}
//----------------------------------------------------------------------
int main ( int argc, char** argv ) {
//--- Define the goal.
string goal[] = { "UF", "UR", "UB", "UL", "DF", "DR", "DB", "DL", "FR", "FL", "BR", "BL",
"UFR", "URB", "UBL", "ULF", "DRF", "DFL", "DLB", "DBR" };
//--- Prepare current (start) and goal state.
vi currentState( 40 ), goalState( 40 );
for( int i=0; i<20; i++ ){
//--- Goal state.
goalState[i] = i;
//--- Current (start) state.
string cubie = argv[i+1];
while( (currentState[i] = find( goal, goal+20, cubie ) - goal) == 20){
cubie = cubie.substr( 1 ) + cubie[0];
currentState[i+20]++;
}
}
//--- Dance the funky Thistlethwaite...
while( ++phase < 5 ){
//--- Compute ids for current and goal state, skip phase if equal.
vi currentId = id( currentState ), goalId = id( goalState );
if( currentId == goalId )
continue;
//--- Initialize the BFS queue.
queue q;
q.push( currentState );
q.push( goalState );
//--- Initialize the BFS tables.
map predecessor;
map direction, lastMove;
direction[ currentId ] = 1;
direction[ goalId ] = 2;
//--- Dance the funky bidirectional BFS...
while( 1 ){
//--- Get state from queue, compute its ID and get its direction.
vi oldState = q.front();
q.pop();
vi oldId = id( oldState );
int& oldDir = direction[oldId];
//--- Apply all applicable moves to it and handle the new state.
for( int move=0; move<18; move++ ){
if( applicableMoves[phase] & (1 << move) ){
//--- Apply the move.
vi newState = applyMove( move, oldState );
vi newId = id( newState );
int& newDir = direction[newId];
//--- Have we seen this state (id) from the other direction already?
//--- I.e. have we found a connection?
if( newDir && newDir != oldDir ){
//--- Make oldId represent the forwards and newId the backwards search state.
if( oldDir > 1 ){
swap( newId, oldId );
move = inverse( move );
}
//--- Reconstruct the connecting algorithm.
vi algorithm( 1, move );
while( oldId != currentId ){
algorithm.insert( algorithm.begin(), lastMove[ oldId ] );
oldId = predecessor[ oldId ];
}
while( newId != goalId ){
algorithm.push_back( inverse( lastMove[ newId ] ));
newId = predecessor[ newId ];
}
//--- Print and apply the algorithm.
for( int i=0; i
cout << "UDFBLR"[algorithm[i]/3] << algorithm[i]%3+1;
currentState = applyMove( algorithm[i], currentState );
}
//--- Jump to the next phase.
goto nextPhasePlease;
}
//--- If we've never seen this state (id) before, visit it.
if( ! newDir ){
q.push( newState );
newDir = oldDir;
lastMove[ newId ] = move;
predecessor[ newId ] = oldId;
}
}
}
}
nextPhasePlease:
;
}
}
魔方机器人matlab编程,C++实现超赞的解魔方的机器人代码相关推荐
- 用matlab编程仿真分析多智能体群集控制和多机器人系统编队,多机器人编队(三)基于关联矩阵的多智能体编队稳定性分析...
作者在读学校Singapore University of Technology and Design 参考文献 _Dimarogonas D V, Johansson K H. Stability ...
- abb机器人离线编程软件解密_一文看懂最新机器人离线编程软件【详细】
今天小萌无意间看到了数控老武写的一篇关于国内外离线编程软件对比的文章,看了之后感觉有些方面写的不错,比如大家有所耳闻的离线编程软件都做了介绍,而且还发表了一些个人的观点,小萌就对数控老武的一些个人观点 ...
- 米家app扫描不到石头机器人_2000元档新擂主?详解石头扫地机器人 P5战力值
原标题:2000元档新擂主?详解石头扫地机器人 P5战力值 透过这个令人不堪回首的长假,方了解自己的懒惰底线在哪,晨起的生物钟被调到了自然醒,每天饿到下午吃挂面--望着地板上日益渐厚的灰尘,悄然打起了 ...
- kuka机器人焊接编程入门教程_【行业干货】KUKA机器人的操作与基本运动编程
原标题:[行业干货]KUKA机器人的操作与基本运动编程 世界坐标系(大地坐标系) *世界坐标系是一个固定定义的笛卡尔坐标系,是用于ROBROOT坐标系和基础坐标系的原点坐标系: *在默认配置中,世界坐 ...
- 2017 robotart x86_RobotArt:机器人离线编程仿真软件领航者
厉害了,近年来人工智能火了!智能产业,随着近年科技业的炒作而成了媒体的新宠儿.人工智能,不同的时代对它有着不同的诠释. 人工智能最早可以追溯到百年,甚至千年以前的知识学.到了八十世纪,第一波工业革命接 ...
- 米兔机器人自主编程_米兔积木机器人编程编写指南.pdf
米兔积木机器人 非官方编程手册 基础篇 第一章 简单造型的搭建 小米推出的米兔积木机器人是一款可以自由 DIY 的积木机器人,它通过让 玩家自主的进行造型设计.结构设计,电子模块搭建以及程序设计等几个 ...
- 米兔机器人自主编程_米兔积木机器人编程指南.pdf
米兔积木机器人编程指南 米兔积木机器人 非官方编程手册 基础篇 第一章 简单造型的搭建 小米推出的米兔积木机器人是一款可以自由 DIY 的积木机器人,它通过让 玩家自主的进行造型设计.结构设计,电子模 ...
- 英文数据集txt_Delmia机器人仿真编程 点焊仿真 第1节 焊点坐标数据提取
在进行Delmia点焊项目仿真之前,如果焊点数量较多,则可以通过宏命令将焊点数据提取出来,保存为xls或者txt格式文件,仿真项目创建完成后再将焊点数据导入使用.当然如果焊点数量较少,则可以直接在仿真 ...
- 基于Arduino、树莓派的两款解魔方机器人
历史的车轮一直在前进,时代总是在发展,在互联网时代,各种新技术层出不穷.其中,最具有实用性和创新性的便是21世纪比较热门的领域--机器人.本次将要制作的即为解魔方机器人,旨在成功地实现不同机械结构情况 ...
最新文章
- 安装百分之80卡住_石家庄铜铝80*80散热器品牌
- 循环语句练习题2(打印三角形和菱形)
- CC.Net 全接触系列之三: CQ.Net: CC.Net 最佳伴侣
- Algorithm:位运算的这些小技巧你知道吗?
- windows/browser ---- cmd命令/powershell命令/chrome插件vimuim命令
- 谷歌Chrome浏览器开发者工具教程—基础功能篇
- mysql 数据库 方案_数据库mysql优化方案
- HTTP权威指南 文字版 带目录
- java项目---------------------------嗖嗖移动大厅
- Win10下配置PHP环境变量
- python匿名函数优点_python匿名函数定义及实例解析
- 线性代数--线性方程组
- 学术规范与论文写作(雨课堂)研究生 全部答案
- python动力学建模与仿真_发动机悬置python仿真计算
- Accuracy, Precision, Recall和F1-score解释
- Strongly Connected Tournament
- 2个实用的解决R语言中文乱码方法
- 88 java反射_4 _注解
- Win7安装系统,无猫腻
- HttpClient 301问题