蓝桥杯算法提高 -- 学霸的迷宫
2015-12-04 15:53
323 查看
思路 : A* 寻路配合优先队列
#include <iostream> #include <cmath> #include <vector> #include <cstdio> #include <queue> using namespace std; const int MAXINT = 0x70FFFFF; // 格子类 class Cell { public: static int DestRow,DestCol; public: bool IsVisited; bool IsOpened; int NowStep; Cell* prevSite; int op; private: int Row,Col; int EstimateValue; public: Cell():NowStep(MAXINT),prevSite(NULL){ op = Row = Col = -1; IsOpened = IsVisited = false; } void SetCoordinate( int row, int col ){ Row = row++; Col = col++; EstimateValue = abs( Cell::DestRow - row ) + abs( Cell::DestCol - col); } int GetEstimate()const{ return EstimateValue; } int RowIndex()const{return Row;} int ColIndex()const{return Col;} }; struct CellCompare { public: bool operator()( const Cell* b, const Cell* a ) { if( a->NowStep < b->NowStep ) return true; else if( a->NowStep == b->NowStep ) { if( a->GetEstimate() < b->GetEstimate() ) return true; else if(a->GetEstimate() < b->GetEstimate()) return a->op < b->op; else return false; } else return false; } }; int Cell::DestCol = 0; int Cell::DestRow = 0; typedef vector<Cell> RowType; typedef vector<RowType> MatrixType; typedef priority_queue<Cell*,vector<Cell*>,CellCompare> PQueue; class Maze : public MatrixType { private: int m_row,m_col; public: Maze( int row, int col ) :MatrixType( row, *(new RowType(col)) ) { Maze& M = *this; m_row = this->size(); m_col = row>0?M[0].size():0; for( int r = 0; r < m_row; ++r) for( int c=0; c < m_col;++c) M[r][c].SetCoordinate(r,c); } int RowCount()const{return m_row;} int ColCount()const{return m_col;} }; const char OP[]="DLRU"; int AStar( Maze& M ) { const static int direct[4][3] = {{1,0,0/*D*/},{0,-1,1/*L*/},{0,1,2/*R*/},{-1,0,3/*U*/}}; PQueue Q; M[0][0].NowStep = 0; Q.push( &M[0][0] ); Cell* Dest = &M[ M.RowCount()-1 ][ M.ColCount()-1]; while( !Q.empty() ) { Cell* C = Q.top(); Q.pop(); if( C->IsVisited ) continue; C->IsVisited = true; for( int i = 0; i < 4; ++i ) { int r = C->RowIndex() + direct[i][0]; int c = C->ColIndex() + direct[i][1]; if( r>=0 && r < M.RowCount() && c >= 0 && c < M.ColCount() ) { Cell* nextC = &M[r][c]; if( nextC->IsVisited ) continue; if( nextC->IsOpened ) { if( nextC->NowStep > C->NowStep+1) { nextC->NowStep = C->NowStep+1; nextC->prevSite = C; nextC->op = direct[i][2]; Q.push( nextC ); } } else { nextC->IsOpened = true; nextC->NowStep = C->NowStep + 1; nextC->prevSite = C; nextC->op = direct[i][2]; Q.push( nextC ); } if( nextC == Dest ) return nextC->NowStep; } } } return -1; } int main(int argc, char** argv) { cin >> Cell::DestRow >> Cell::DestCol; Maze M( Cell::DestRow,Cell::DestCol ); char *rowSet = new char[M.ColCount()+1]; for( int i = 0; i < M.RowCount(); ++i) { cin >> rowSet; for( int j = 0; j < M.ColCount();++j) { M[i][j].IsVisited = (rowSet[j]=='1'); } } delete rowSet; int step = AStar( M ); char *opSet = new char[step+1]; opSet[ step ]=0; Cell* C = &M[M.RowCount()-1][M.ColCount()-1]; for( int i = 0; i < step; ++i ) { opSet[step-i-1] = OP[C->op]; C = C->prevSite; } cout <<step<<endl << opSet; delete opSet; return 0; }
相关文章推荐
- Java项目如何记录日志
- 获取微博用户数据
- C语言的A*算法
- spring mvc使用Servlet3异步要注意的几个问题
- plsql破解的办法
- Jquery 工具类函数
- 36、重分布配置实验之route-map
- js原生创建封装ajax的方法,附XMLHttpRequest状态码
- Python yield 官方详解(基于2.x)
- poj 2376 Cleaning Shifts 贪心 区间问题
- LeetCode 171 Excel Sheet Column Number
- 图片轮播器
- gnuradio模块的数据输出
- 启动 asmca 时出现 No protocol specified 错误的解决办法
- android button设计模拟器
- python入门常见错误
- Bean的初始化和销毁
- IOS与ReactNative
- IOS 启动画面设置
- H5页面audio多声音解决方法