Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Point;
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Scanner;
- public class E {
- Scanner in;
- boolean map[][];
- HashMap<Character, Char> chars;
- public static void main(String args[]) throws IOException {
- new E().doIt();
- }
- public void doIt() throws IOException {
- in = new Scanner(new File("E.in"));
- PrintStream out = new PrintStream("E.out");
- int tc = in.nextInt();
- int n;
- for(int c=1; c<=tc; c++) {
- n = in.nextInt();
- chars = new HashMap<Character, Char>();
- map = new boolean[in.nextInt()][in.nextInt()];
- for(boolean[] mr: map) {
- Arrays.fill(mr,true);
- }
- String line = in.nextLine();
- // System.out.println(line + "\t" + map.length + "\t" + map.length);
- for(int i=0; i<map.length; i++) {
- line = in.nextLine();
- // System.out.println(line);
- for(int j=0; j<map[0].length; j++) {
- if(line.charAt(j) == '#') {
- map[i][j] = false;
- continue;
- }
- if(line.charAt(j) != '.') {
- chars.put(line.charAt(j), new Char(j, i));
- }
- }
- }
- String s;
- char character;
- for(int i=0; i<n; i++) {
- s = in.next();
- character = s.charAt(0);
- // System.out.println(s + character);
- chars.get(character).read();
- }
- int time = in.nextInt();
- for(Character ch : chars.keySet()) {
- chars.get(ch).move(time);
- }
- char[][] fin = new char[map.length][map[0].length];
- for(char[] ch : fin) {
- Arrays.fill(ch, '.');
- }
- for(int i=0; i<map.length; i++) {
- for(int j=0; j<map[0].length; j++) {
- if(!map[i][j]) fin[i][j] = '#';
- }
- }
- for(Character ch : chars.keySet()) {
- System.out.println(ch);
- System.out.println(chars.get(ch).pos);
- fin[chars.get(ch).pos.y][chars.get(ch).pos.x] = ch;
- }
- out.println("DATA SET #" + c);
- for(int i=0; i<map.length; i++) {
- for(int j=0; j<map[0].length; j++) {
- out.print(fin[i][j]);
- }
- out.println();
- }
- }
- }
- boolean valid(Point p) {
- if(p.x < 0 || p.y < 0 || p.y >= map.length || p.x >= map[0].length) {
- return false;
- }
- return map[p.y][p.x];
- }
- class Char {
- ArrayList<String> moves;
- Point pos;
- Char(int x, int y) {
- moves = new ArrayList<String>();
- pos = new Point(x, y);
- }
- void read() {
- int l = in.nextInt();
- String t;
- for(int i=0; i<l; i++) {
- t = in.next();
- for(int j=in.nextInt(); j>=0; j--) {
- moves.add(t);
- }
- }
- Point np = new Point(pos.x, pos.y);
- Point nnp = new Point(pos.x, pos.y);
- for(int i=0; i<moves.size(); i++) {
- t = moves.get(i);
- if(t.equals("EAST"))
- nnp.x++;
- else if(t.equals("WEST"))
- nnp.x--;
- else if(t.equals("NORTH"))
- nnp.y--;
- else if(t.equals("SOUTH"))
- nnp.y++;
- if(!valid(nnp)) {
- nnp.x = np.x;
- nnp.y = np.y;
- moves.set(i, "PAUSE");
- } else {
- np.x = nnp.x;
- np.y = nnp.y;
- }
- }
- if(!pos.equals(np)) {
- for(int i=moves.size()-1; i >= 0; i--) {
- moves.add(moves.get(i));
- }
- }
- }
- void move(int time) {
- time %= moves.size();
- String t;
- for(int i=0; i< time; i++) {
- t = moves.get(i);
- if(t.equals("EAST"))
- pos.x++;
- else if(t.equals("WEST"))
- pos.x--;
- else if(t.equals("NORTH"))
- pos.y--;
- else if(t.equals("SOUTH"))
- pos.y++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement