百度编程大赛的9宫图代码及解析
2012-07-01 13:57
232 查看
先放代码,算法设计过程我随后再放上去
//author:1025679612@qq.com
//csdn:http://blog.csdn.net/wind_2008_06_29/article/details/7706531
#include <iostream> #include <list> using std::cout; using std::endl; using std::cin; using std::list; bool bVisitTag[387420489]= {false}; //9^9用来表示其中的一种状态是否被访问过 //========================================================================= //begin class Point define //========================================================================= class Point{ public: Point(); Point(const Point& rhs); public: bool operator==(const Point& rhs); bool operator!=(const Point& rhs); Point& operator= (const Point& rhs); public: int m_iRow; // int m_iCol; }; Point::Point(){} Point::Point(const Point& rhs){ m_iRow =rhs.m_iRow; m_iCol =rhs.m_iCol; } bool Point::operator!=(const Point& rhs){ return !operator==(rhs); } bool Point::operator==(const Point& rhs){ return (m_iRow== rhs.m_iRow)&&(m_iCol== rhs.m_iCol); } Point& Point::operator= (const Point& rhs){ m_iRow = rhs.m_iRow; m_iCol = rhs.m_iCol; return *this; } //========================================================================= //end of class Point define //========================================================================= //========================================================================= //begin class CState define //========================================================================= class CState{ //存储9宫的状态 public: CState(const CState& rhs); CState(); public: CState& operator=(const CState& rhs); bool operator==(const CState& rhs); public: int vValue[3][3]; Point m_Center; //我们设0的位置是Center }; CState::CState(const CState& rhs):m_Center(rhs.m_Center){ for(int iRow= 0; iRow< 3; ++iRow){ for(int iCol= 0; iCol< 3; ++iCol) vValue[iRow][iCol] = rhs.vValue[iRow][iCol]; } } CState::CState(){} CState& CState::operator= (const CState& rhs){ m_Center = rhs.m_Center; for(int iRow= 0; iRow< 3; ++iRow){ for(int iCol= 0; iCol< 3; ++iCol) vValue[iRow][iCol] = rhs.vValue[iRow][iCol]; } return *this; } bool CState::operator==(const CState& rhs){ if(m_Center != rhs.m_Center) return false; for(int iRow= 0; iRow< 3; ++iRow){ for(int iCol= 0; iCol< 3; ++iCol) if(vValue[iRow][iCol] != rhs.vValue[iRow][iCol]) return false; } return true; } //========================================================================= //end of class CState define //========================================================================= CState StateBegin, StateEnd; //开始和结束状态 int Search(); int StateToInt(const CState& state);//编码 CState IntToState(int iState); //解码 int main(){ for(int iRow= 0; iRow< 3; ++iRow){ for(int iCol= 0; iCol< 3; ++iCol){ cin>>StateBegin.vValue[iRow][iCol]; if(StateBegin.vValue[iRow][iCol]== 0){ StateBegin.m_Center.m_iRow= iRow; StateBegin.m_Center.m_iCol= iCol; } } } for(int iRow= 0; iRow< 3; ++iRow){ for(int iCol= 0; iCol< 3; ++iCol){ cin>>StateEnd.vValue[iRow][iCol]; if(StateEnd.vValue[iRow][iCol]== 0){ StateEnd.m_Center.m_iRow= iRow; StateEnd.m_Center.m_iCol= iCol; } } } cout<<Search()<<endl; } int StateToInt(const CState& state){//编码 int iValue = 0; for(int iRow= 2; iRow>= 0; --iRow){ for(int iCol=2; iCol>= 0; --iCol){ iValue*= 9; iValue+= state.vValue[iRow][iCol]; } } return iValue; } CState IntToState(int iState){ //解码 CState state; for(int iRow= 0; iRow< 3; ++iRow){ for(int iCol= 0; iCol< 3; ++iCol){ state.vValue[iRow][iCol] = iState%9; iState/= 9; if(state.vValue[iRow][iCol]== 0){ state.m_Center.m_iRow = iRow; state.m_Center.m_iCol = iCol; } } } return state; } //========================================================================= //BFS search //========================================================================= int Search(){ list<CState> buffer1, buffer2; int iStep= 0; list<CState> *lpListUsed; //存放当前这一步 list<CState> *lpListNextStep; //存放下一步 if(StateBegin== StateEnd) return 0; //直接到达的话就是0了 lpListUsed = &buffer1; lpListNextStep = &buffer2; lpListUsed->push_back(StateBegin); int iPosition = StateToInt(StateBegin); bVisitTag[iPosition] = true; //bVisitTag[StateToInt(StateBegin)]= true; //打标记 while(!lpListUsed->empty()){ CState StateNow; CState StateNext; ++iStep; while(!lpListUsed->empty()){ StateNow= lpListUsed->front(); lpListUsed->pop_front(); if(StateNow== StateEnd){ return iStep-1; } if(StateNow.m_Center.m_iRow-1>= 0){ StateNext = StateNow; --StateNext.m_Center.m_iRow; StateNext.vValue[StateNow.m_Center.m_iRow][StateNow.m_Center.m_iCol] =StateNow.vValue[StateNext.m_Center.m_iRow][StateNext.m_Center.m_iCol]; StateNext.vValue[StateNext.m_Center.m_iRow][StateNext.m_Center.m_iCol] = 0; int iPosition = StateToInt(StateNext); if(!bVisitTag[iPosition]){ bVisitTag[iPosition]= true; lpListNextStep->push_back(StateNext); } } if(StateNow.m_Center.m_iRow+1< 3){ StateNext = StateNow; ++StateNext.m_Center.m_iRow; StateNext.vValue[StateNow.m_Center.m_iRow][StateNow.m_Center.m_iCol] =StateNow.vValue[StateNext.m_Center.m_iRow][StateNext.m_Center.m_iCol]; StateNext.vValue[StateNext.m_Center.m_iRow][StateNext.m_Center.m_iCol] = 0; int iPosition = StateToInt(StateNext); if(!bVisitTag[iPosition]){ bVisitTag[iPosition]= true; lpListNextStep->push_back(StateNext); } } if(StateNow.m_Center.m_iCol-1>= 0){ StateNext = StateNow; --StateNext.m_Center.m_iCol; StateNext.vValue[StateNow.m_Center.m_iRow][StateNow.m_Center.m_iCol] =StateNow.vValue[StateNext.m_Center.m_iRow][StateNext.m_Center.m_iCol]; StateNext.vValue[StateNext.m_Center.m_iRow][StateNext.m_Center.m_iCol] = 0; int iPosition = StateToInt(StateNext); if(!bVisitTag[iPosition]){ bVisitTag[iPosition]= true; lpListNextStep->push_back(StateNext); } } if(StateNow.m_Center.m_iCol+1< 3){ StateNext = StateNow; ++StateNext.m_Center.m_iCol; StateNext.vValue[StateNow.m_Center.m_iRow][StateNow.m_Center.m_iCol] =StateNow.vValue[StateNext.m_Center.m_iRow][StateNext.m_Center.m_iCol]; StateNext.vValue[StateNext.m_Center.m_iRow][StateNext.m_Center.m_iCol] = 0; int iPosition = StateToInt(StateNext); if(!bVisitTag[iPosition]){ bVisitTag[iPosition]= true; lpListNextStep->push_back(StateNext); } } } std::swap(lpListUsed, lpListNextStep); } return -1; }
相关文章推荐
- 共享一段CMPP编程时候,移动网关返回CMPP_SUBMIT_RESP消息中,解析Msg_Id字段的代码
- 代码解析深度学习系统编程模型:TensorFlow vs. CNTK
- 97年世界编程大赛一等奖作品代码
- 第一届文思在线编程大赛题解析。
- C语言编程狼追兔子问题代码解析
- 代码解析深度学习系统编程模型:TensorFlow vs. CNTK
- 25行AS3代码编程大赛的第一名!25行代码构造的AS3游戏
- 共享一段CMPP编程时候,移动网关返回CMPP_SUBMIT_RESP消息中,解析Msg_Id字段的代码
- 结合51代码代码解析rfid读卡器的编程思想
- jrtplib编程步骤文档代码解析
- 百度编程大赛试题----类似九格宫(C++算法实现)
- 97年世界编程大赛冠军代码
- 一段代码:一九九七年世界编程大赛冠军作品
- Linux之系统编程、TCP_IP通信原理、sock编程代码解析
- 百度编程大赛试题----数7 加强版游戏(C++算法实现)
- Qt下的OpenGL 编程(11)Qt实例hellogl代码解析
- 混乱C大赛代码解析
- 世界黑客编程大赛冠军的代码,很震撼~~(
- 97年世界编程大赛一等奖作品代码--太牛了,偶像!
- 国际混乱C代码大赛代码解析:IOCCC 1984 —— anonymous