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;
}
*这是杭电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;
}
相关文章推荐
- hdoj1035
- hdoj 1035 Robot Motion
- HDOJ1035 ( Robot Motion ) 【递归】
- hdoj 1035 Robot Motion (DFS+模拟)
- Hdoj 1035 Robot Motion
- HDOJ 1035 模拟 水
- HDOJ 1035
- hdoj 1035 Robot Motion 【DFS 暴力】
- hdoj-1035 Robot Motion
- HDOJ1035 Robot Motion 【模拟】
- HDOJ1035 Robot Motion(模拟题,字符串)
- HDOJ 1035 Robot Motion
- HDOJ-1035 搜索模拟问题[深搜]
- HDOJ HDU 1035 Robot Motion
- hdoj 1035(算是水题把)(屏蔽freopen)
- hdoj 1035 Robot Motion
- HDOJ 1035 模拟
- HDOJ 题目1035 Robot Motion(模拟)
- HDOJ---1035 Robot Motion
- hdoj 1035 Robot Motion 【DFS】