笨笨熊搬家之交通篇
2014-10-06 12:10
267 查看
(据说是华为的上机题,觉得挺有意思,拿来做做) 森林里的苯苯熊要乔迁新喜,上次他已经将物品打包完成,并约了朋友来帮忙。接下来他要选定一个搬家的时间,想了很久,就决定在国庆节进行,因为国庆放假朋友们都有时间啦。但是在森林里,从他现在房子到新豪宅,所经之地有山有水,路途曲折,甚至有些道路是不通的。 请你和他一起查看指定的地图,看看从笨笨熊现在的房子到新宅之间,道路是否是畅通的呢? 地图是R行、C列的矩阵,矩阵的每一个格子刚好是一天的行程。 矩阵由“B”、“-”、“#”、“H”四种字符成员组成,其中: B: 代表苯苯熊现在的房子; H: 代表笨笨熊新的豪宅; -: 代表可以通行的道路; #: 代表无法通过的障碍(高山、大河等); 此外,森林里也有交通规则地:在任务位置,只能向“上、下、左、右”四个方向中的其中一个方向行走。
#include<iostream> using namespace std; //笨笨熊搬家之交通篇 //B - # H //B代表老家, -代表可以通行,#代表不能通行,H代表新家 //总共有R行,C列 void initGraph(char **graph,int Rows,int Cols) { cout<<"please input the graph:"<<endl; int i,j; for(i=0;i<Rows;i++) for(j=0;j<Cols;j++) cin>>graph[i][j]; ///////////////////////////////////////////// cout<<"ok, the graph is:\n"; for(i=0;i<Rows;i++) { for(j=0;j<Cols;j++) cout<<graph[i][j]; cout<<endl; } //////////////////////////////////////// } void FindHouseXY(char **graph,int Rows,int Cols,int &houseX,int &houseY,char HouseName) { int i,j; for(i=0;i<Cols;i++) for(j=0;j<Cols;j++) if(graph[i][j]==HouseName) { houseX=i; houseY=j; return; } } bool MoveHouseCore(char **graph,int Rows,int Cols,int startX,int startY,bool * isvisit) { if(startX>=0&&startX<Rows&&startY>=0&&startY<Cols&&graph[startX][startY]=='H') { cout<<"wow! find the bear's new house in graph["<<startX<<"]["<<startY<<"]"<<endl; return true; } bool find=false; if(startX>=0&&startX<Rows&&startY>=0&&startY<Cols&&graph[startX][startY]!='#'&&isvisit[startX*Cols+startY]==false) { isvisit[startX*Cols+startY]=true; find=MoveHouseCore(graph,Rows,Cols,startX+1,startY,isvisit)||MoveHouseCore(graph,Rows,Cols,startX-1,startY,isvisit)||MoveHouseCore(graph,Rows,Cols,startX,startY+1,isvisit)||MoveHouseCore(graph,Rows,Cols,startX,startY-1,isvisit); if(!find) isvisit[startX*Cols+startY]=false; } return find; } void MoveToNewhouse(char **graph,int Rows,int Cols) { int OldX=-1,OldY=-1,NewX=0,NewY=0;//老房子坐标,新房子坐标 bool IsFind=false; //是否能够找到新家 bool *IsVisit=new bool[Cols*Rows]; for(int i=0;i<Cols*Rows;i++) IsVisit[i]=false; FindHouseXY(graph,Rows,Cols,OldX,OldY,'B'); //FindHouseXY(graph,Rows,Cols,NewX,NewY,'H'); cout<<"the old hous is in graph["<<OldX<<"]["<<OldY<<"].and the new house is in graph["<<NewX<<"]["<<NewY<<"]\n"; if(OldX==-1||OldY==-1||NewX==-1||NewY==-1) throw exception("Can not find the house address!\n"); IsFind=MoveHouseCore(graph,Rows,Cols,OldX,OldY,IsVisit); if(IsFind) cout<<"good! the bear can move to its new house!\n\n\n\\n"; else cout<<"Oops! the bear can not access the way to its new house!\n\n\n\n"; } /*****************************************************************************************/ int main() { /*********************************************************************/ //小熊搬家题目 int Rows,Cols; cout<<"please input the row:\n"; cin>>Rows; cout<<"please input the colume:\n"; cin>>Cols; char **map=new char*[Rows]; for(int i=0;i<Rows;i++) map[i]=new char[Cols]; initGraph(map,Rows,Cols); MoveToNewhouse(map,Rows,Cols); /********************************************************************/ return 0; } 里面输出了一些有助于理解和验证程序正确性的信息,运行已通过。 |
相关文章推荐
- 笨笨熊搬家——交通(java)
- 笨笨熊搬家——交通(java)-2
- 2014华为编程大赛题目:笨笨熊搬家打包篇
- 华为编码比赛(笨笨熊搬家打包篇,打牌,扑克牌排序)
- 华为机试——笨笨熊搬家(打包篇)
- 笨笨熊搬家问题 填充思想--怎么输出结果--递归--每日一练(六)
- 华为编程大赛,笨笨熊搬家,OJ平台网络问题提交不上代 a21d 码。先将代码到这。
- 笨笨熊搬家打包篇
- 华为机试之笨笨熊搬家
- 2015华为实习笨笨熊搬家交通篇JAVA/C++
- 2014华为编程大赛题目2:笨笨熊搬家打包篇
- 笨笨熊搬家问题深入--迷宫问题--深度搜索--还是填充思想并且打印出路线--递归--结构体数组---编程随笔(4)
- 笨笨熊搬家打包篇
- 华为编程——笨笨熊搬家
- 办公室搬家
- BLOG搬家了
- 虽然博客搬家了,但对于重要内容还会在这里留一个备份
- 搬家通知
- 手机看交通监视器实时录像
- 今天公司搬家