博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ZOJ_1708 Robot Motion 机器人走路

Posted on 2010-08-22 10:48  还好  阅读(1638)  评论(2编辑  收藏  举报
Robot Motion

Time Limit: 1 Second      Memory Limit: 32768 KB

A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are

N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)

For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.

Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.

You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.


Input

There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.


Output

For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.


Sample Input

3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0


Sample Output

10 step(s) to exit
3 step(s) before a loop of 8 step(s)

其实在写这篇文章的时候我正准备跟同学dota开黑,我们的开黑口号叫"009领衔之无敌开黑小分队" !鄙人叫006!以上是插曲,大家别介意!题目大概意思是问机器人经过多少步会走出这个Grid,或者是经过多少步会出现循环!

题目相对来说还是比较简单的,但别看他简单,如果你能一次性通过测试的,其实也不容易了,我提交了三次,前两次都是一些小问题方面处理的不是很好,导致一直Wrong Answer!下面说一下我的思路:

首先定义一个二维数组grid[10][10],用来接收输入的gird数据,其次是定义一个bool型的二维数组:hasVisit[10][10],用来标记grid[10][10]中那些已经被访问过的数组元素,如果在机器人还没走出去之前,又重新回到了之前走的点,那么就会出现循环,最后就是再定义一个二维数组:steps[10][10], 用来记录到每个点的时候需要多少步!代码主要就是一个While循环!

下面是我的C++代码, RUN TIME:0MS, RUN MEMOEY:184KB

 

代码
#include<iostream>
using namespace std;

int main(void)
{
    
char grid[10][10];
    
bool hasVisit[10][10];
    
int steps[10][10];
    
int i,j;
    
int rowsNum,columnsNum;
    
int startColumn,startRow;
    
while(cin>>rowsNum>>columnsNum>>startColumn)
    {
        
if(rowsNum==0&&columnsNum==0&&startColumn==0)
        {
            
return 0;
        }
        
for(i=0;i<rowsNum;i++)
        {
            
for(j=0;j<columnsNum;j++)
            {
                cin
>>grid[i][j];
                hasVisit[i][j]
=false;
                steps[i][j]
=0;
            }
        }
        startRow
=0;
        startColumn
--;
        
int totalSteps=0;
        
while(startRow>=0&&startRow<rowsNum&&startColumn>=0&&startColumn<columnsNum&&!hasVisit[startRow][startColumn])
        {
            hasVisit[startRow][startColumn]
=true;
            steps[startRow][startColumn]
=totalSteps;
            
switch(grid[startRow][startColumn])
            {
                
case 'N':startRow--;
                    
break;
                
case 'S':startRow++;
                    
break;
                
case 'E':startColumn++;
                    
break;
                
case 'W':startColumn--;
                    
break;
            }
            totalSteps
++;
        }
        
if(startRow<0||startRow>=rowsNum||startColumn<0||startColumn>=columnsNum)
        {
            cout
<<totalSteps<<" step(s) to exit"<<endl;
        }
        
else
        {
            cout
<<steps[startRow][startColumn]<<" step(s) before a loop of "<<totalSteps-steps[startRow][startColumn]<<" step(s)"<<endl;
        }
    }

}

 

哎,这个文章写完了,这个VS房间还是没能挤进去,我用的可是专业的挤房工具,其实这个工具主要原理就是修改电脑的系统时间!难怪上次我挤进去的时候怎么系统时间一下子比我手机快了30分钟!