1. 


/**
* Angewandte Mathematik, SS09, IFB 2C
* ACM Problem #10116 Robot Motion
* http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1057 *
*
* @author Fabian Seidl
* @author Marcel Sachse
* @version 1.0, 24.06.2009
*
* Status : Accepted
* Runtime: 0.088
*/


import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
BufferedInputStream bin = new BufferedInputStream(System.in);
Scanner scanner = new Scanner(bin);
int height;// höhe des feldes
int width; // breite des feldes
int start; // einstieg am oberen Rand
while((height=scanner.nextInt())!=0)
{
width=scanner.nextInt();
start=scanner.nextInt();
String line;
int counter =0;
char[][]fieldC = new char[height][width];
int[][]fieldI = new int[height][width];
for (int i =0 ; i <height; i++) //einlesen in eim 2d feld von char
{
line=scanner.next();
for (int j=0; j<(width);j++)
{
fieldC[i][j]=line.charAt(j);
}
}
// einhstiegs position festlegen
int posX=start-1;
int posY=0;
int beforLoop=0;
boolean isLoop=false;
// überprüfen ob außerhalb des feldes oder in einem kreis
while(posX>=0&&posY>=0&&posX<width&&posY<height&&fieldC[posY][posX]!='X')
{

fieldI[posY][posX]=counter;
char route =fieldC[posY][posX];
fieldC[posY][posX]='X';
// Richtung erfassen
switch(route)
{
case'N': posY--; break;
case'E': posX++; break;
case'S': posY++; break;
case'W': posX--; break;
}
counter++;
// vormerken ob es ein kreis ist
if((posX>=0&&posY>=0&&posX<width&&posY<height))
if(fieldC[posY][posX]=='X')
{
beforLoop = fieldI[posY][posX];
isLoop =true;
}
}
// ausgabe
int loop=counter-beforLoop;
if (!isLoop)System.out.println(counter+" step(s) to exit");
else System.out.println(beforLoop+" step(s) before a loop of "+loop+" step(s)");
}
}
}

2.

/*
* 10116 - Robotic Motion
* Studiengruppe: IFB2C
* Martin Pesl
* Robert Reichart
* Elvin Uzeirovic
*
* Run Time Submission Date
* 0.092 2009-06-17 16:58:23
*/


import java.io.*;

class Main{
public static void main(String... args)throws Exception{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
while(true){
String line = input.readLine();
String[] init = line.split(" ");
if (init[0].equals("0")){break;}
int rows = Integer.parseInt(init[0]);
int columns = Integer.parseInt(init[1]);
int IndexOfStart = Integer.parseInt(init[2])-1;
String [][] field = new String[rows][columns];

//Feld mit Buchstaben füllen
for (int i=0; i<rows;i++){
line = input.readLine();
char[] WNSE = line.toCharArray();
for (int k=0;k<WNSE.length;k++){
field[i][k]=Character.toString(WNSE[k]);
}
}

int X=IndexOfStart;
int Y=0;
int walkcounter=0;

boolean[][] walkedover = new boolean[rows][columns];
int [][] stepcount = new int[rows][columns];
boolean loop=true;
while (!walkedover[Y][X]){
walkedover[Y][X]=true;
stepcount[Y][X]=walkcounter;
if (field[Y][X].equals("W")){X-=1;}else if(field[Y][X].equals("E")){X+=1;}else if (field[Y][X].equals("N")){Y-=1;}else if (field[Y][X].equals("S")){Y+=1;}
walkcounter++;
if (X<0 || Y<0 || X>=columns || Y>=rows){loop=false; break;}
}

if(loop){
int looplenght=walkcounter-stepcount[Y][X];
System.out.println(stepcount[Y][X] + " step(s) before a loop of " + looplenght + " step(s)");
}else{
System.out.println(walkcounter + " step(s) to exit");
}
}
}
}