您的位置:首页 > 编程语言 > C语言/C++

笨笨熊搬家之交通篇

2014-10-06 12:10 267 查看
    (据说是华为的上机题,觉得挺有意思,拿来做做)
森林里的苯苯熊要乔迁新喜,上次他已经将物品打包完成,并约了朋友来帮忙。接下来他要选定一个搬家的时间,想了很久,就决定在国庆节进行,因为国庆放假朋友们都有时间啦。但是在森林里,从他现在房子到新豪宅,所经之地有山有水,路途曲折,甚至有些道路是不通的。

       请你和他一起查看指定的地图,看看从笨笨熊现在的房子到新宅之间,道路是否是畅通的呢?

       地图是R行、C列的矩阵,矩阵的每一个格子刚好是一天的行程。

       矩阵由“B”、“-”、“#”、“H”四种字符成员组成,其中:

       B: 代表苯苯熊现在的房子;

       H: 代表笨笨熊新的豪宅;

       -: 代表可以通行的道路;

       #: 代表无法通过的障碍(高山、大河等);

       此外,森林里也有交通规则地:在任务位置,只能向“上、下、左、右”四个方向中的其中一个方向行走。

输入:
4  // R的数值

4  // C的数值,下面是地图。

--##---

B-----H

#---#--

-------
输出:
Y //代表道路可达

或 

N //代表道路不通
样例输入:
1
5
-B-H#
样例输出:
Y
代码如下(已通过编译):

#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;
}


里面输出了一些有助于理解和验证程序正确性的信息,运行已通过。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  笔试面试 华为 C++