您的位置:首页 > 其它

HDOJ1035

2008-10-15 09:08 330 查看
/*

*这是杭电OJ上的一道简单题

*C++实现

*第一次尝试用面向对象的思想去做题,感觉很不错

*/

#include <iostream>

using namespace std;

class Node

{

public:

Node()//construction

{

time=0;

flag=false;

}

void SetAction(char a)

{

action=a;

}

char GetAction()

{

return action;

}

void SetTime(int t)

{

time=t;

}

int GetTime()

{

return time;

}

void SetFlag(bool f)

{

flag=f;

}

int GetFlag()

{

return flag;

}

private:

char action;

int time;

bool flag;

};

int main()

{

int i,j;

int row,col,sCol,curRow,curCol,nextRow,nextCol;

int exitTime,loopStartT,loopTime;

char action;

int flag;

enum{exit=1,notExit=2};

while(cin>>row>>col)

{

if(row==0 && col==0)

break;

cin>>sCol;

Node nodes[10][10];

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)

{

cin>>action;

nodes[i][j].SetAction(action);

}

}

exitTime=-1;

curRow=0;

curCol=sCol-1;

nodes[curRow][curCol].SetTime(0);

nodes[curRow][curCol].SetFlag(true);

flag=0;

while(1)

{

switch(nodes[curRow][curCol].GetAction())

{

case 'N':

nextRow=curRow-1;

nextCol=curCol;

break;

case 'W':

nextRow=curRow;

nextCol=curCol-1;

break;

case 'S':

nextRow=curRow+1;

nextCol=curCol;

break;

case 'E':

nextRow=curRow;

nextCol=curCol+1;

break;

}

if(nextRow<0 || nextRow>=row || nextCol<0 || nextCol>=col)//out of grid

{

exitTime=nodes[curRow][curCol].GetTime()+1;

flag=exit;

break;

}

if(nodes[nextRow][nextCol].GetFlag())//loop

{

loopStartT=nodes[nextRow][nextCol].GetTime();

loopTime=nodes[curRow][curCol].GetTime()-loopStartT+1;

flag=notExit;

break;

}

nodes[nextRow][nextCol].SetTime(nodes[curRow][curCol].GetTime()+1);

nodes[curRow][curCol].SetFlag(true);

curRow=nextRow;

curCol=nextCol;

}

if(flag==exit)

cout<<exitTime<<" step(s) to exit"<<endl;

if(flag==notExit)

cout<<loopStartT<<" step(s) before a loop of "<<loopTime<<" step(s)"<<endl;

}

return 0;

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