1.
package
contestVolumes.volume118;
import java.util.Scanner;
/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest,
SS10
* Problem: 11831 - Sticker Collector Robot
* Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=226&problem=2931&mosmsg=Submission+received+with+ID+8260030
*
* @author Siegfried Ippisch
* @version 1.0
*
* Method : swicth
* Status : Accepted
* Runtime: 0.736
*/
public class StickerCollectorRobots {
public static final int NORTH = 0; // N
public static final int EAST = 1; // L
public static final int SOUTH = 2; // S
public static final int WEST = 3; // O
public static class Robot {
private int x;
private int y;
private int dir;
private Arena a;
public Robot(int x, int y, int
dir){
this.x = x;
this.y = y;
this.dir = dir;
}
public void setArena(Arena a){
this.a = a;
}
public void instruction(char[]
ch){
for(char c: ch)
this.instruction(c);
}
public void instruction(char c){
switch(c){
case
'D': this.dir++;
this.dir =
this.dir == 4 ? this.dir = 0: this.dir;
break;
case
'E': this.dir--;
this.dir =
this.dir == -1 ? this.dir = 3: this.dir;
break;
case
'F': this.move();
break;
}
}
private void move(){
int x = this.x;
int y = this.y;
switch(this.dir){
case
NORTH: y--; break;
case
EAST: x++; break;
case
SOUTH: y++; break;
case
WEST: x--; break;
}
if(a.enter(x,
y)){
this.x = x;
this.y = y;
}
}
}
public static class Arena {
private char[][] a;
private int collected = 0;
public Arena(char[][] a){
this.a = a;
}
public boolean enter(int x, int
y){
if(x < 0 ||
x >= this.a[0].length || y < 0 || y >= this.a.length)
return false;
switch(this.a[y][x]){
case
'.': return true;
case
'*': this.a[y][x] = '.';
collected++;
return true;
case
'#': return false;
}
return false;
}
}
public static void main(String[] args){
Scanner in = new
Scanner(System.in);
int n = in.nextInt(); // lines
int m = in.nextInt(); // chars
int s = in.nextInt(); //
instructions
while(n != 0 && m != 0
&& s != 0){
in.nextLine();
Robot r = null;
char[][] a =
new char[n][];
for(int i=0;
i<n; i++){
char[] c = in.nextLine().toCharArray();
for(int j=0; j<m; j++)
switch(c[j]){
case 'N': r =
new Robot(j,i,NORTH); c[j] = '.'; break;
case 'L': r =
new Robot(j,i,EAST ); c[j] = '.'; break;
case 'S': r =
new Robot(j,i,SOUTH); c[j] = '.'; break;
case 'O': r =
new Robot(j,i,WEST ); c[j] = '.'; break;
}
a[i] = c;
}
Arena arena =
new Arena(a);
r.setArena(arena);
r.instruction(in.next().toCharArray());
System.out.println(arena.collected);
n =
in.nextInt();
m =
in.nextInt();
s =
in.nextInt();
}
in.close();
}
}