Submission #50354105


Source Code Expand

// Main.java is converted from Abc341c.java by genmain.rb:
//   https://github.com/unnohideyuki/jatcoder/blob/main/work/genmain.rb
import java.util.*;
import java.io.PrintWriter;


public class Main {
    public static void main(final String[] args){
      final Abc341c abc341c = new Abc341c();
      abc341c.solve();
      abc341c.flush();
    }
}

class Abc341c {
  private final FastScanner sc;
  private final PrintWriter out;
  public Abc341c(){
    sc = new FastScanner();
    out = new PrintWriter(System.out);
  }

  public void flush() {
    out.flush();
  }

  private int H;
  private int W;
  private int N;
  private String T;
  private char[][] g;

  private int dy, dx;

  private boolean check(int y, int x) {
    if (g[y][x] == '#') {
      return false;
    }
    final int gy = y + dy;
    final int gx = x + dx;
    if (gy < 0 || gy >= H || gx < 0 || gx >= W || g[gy][gx] == '#') {
      return false;
    }
    for (int i = 0; i < N; i++) {
      final char c = T.charAt(i);
      switch(c) {
        case 'L': x--; break;
        case 'R': x++; break;
        case 'U': y--; break;
        case 'D': y++; break;
      }
      if (y < 0 || y >= H || x < 0 || x >= W || g[y][x] == '#') {
        return false;
      }
    }
    return true;
  }

  public void solve() {
    // Input
    H = sc.nextInt();
    W = sc.nextInt();
    N = sc.nextInt();
    T = sc.next();
    g = new char[H][W];
    for (int i = 0; i < H; i++){
      final String s = sc.next();
      for (int j = 0; j < W; j++) {
        g[i][j] = s.charAt(j);
      }
    }
    // calc dy, dx
    dy = 0;
    dx = 0;
    for (int i = 0; i < N; i++) {
      final char c = T.charAt(i);
      switch(c) {
        case 'L': dx--; break;
        case 'R': dx++; break;
        case 'U': dy--; break;
        case 'D': dy++; break;
      }
    }

    long ans = 0;
    for (int i = 1; i < H - 1; i++) {
      for (int j = 1; j < W - 1; j++) {
        if (check(i, j)) {
          ans++;
        }
      }
    }
    out.println(ans);
  }
}


class FastScanner {
    private final Scanner sc = new Scanner(System.in);
    public String next() { return sc.next(); }
    public int nextInt() { return Integer.parseInt(sc.next()); }
    public long nextLong() { return Long.parseLong(sc.next()); }
    public double nextDouble() { return Double.parseDouble(sc.next()); }
}

Submission Info

Submission Time
Task C - Takahashi Gets Lost
User unnohideyuki
Language Java (OpenJDK 17)
Score 250
Code Size 2454 Byte
Status AC
Exec Time 455 ms
Memory 42404 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 250 / 250
Status
AC × 2
AC × 40
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 339 ms 42404 KiB
001.txt AC 393 ms 41100 KiB
002.txt AC 156 ms 41608 KiB
003.txt AC 149 ms 41708 KiB
004.txt AC 151 ms 41800 KiB
005.txt AC 155 ms 41672 KiB
006.txt AC 455 ms 42196 KiB
007.txt AC 184 ms 40568 KiB
008.txt AC 133 ms 40356 KiB
009.txt AC 98 ms 39472 KiB
010.txt AC 105 ms 39796 KiB
011.txt AC 157 ms 41396 KiB
012.txt AC 208 ms 41532 KiB
013.txt AC 170 ms 42200 KiB
014.txt AC 216 ms 41456 KiB
015.txt AC 403 ms 42124 KiB
016.txt AC 355 ms 41652 KiB
017.txt AC 222 ms 42028 KiB
018.txt AC 236 ms 42152 KiB
019.txt AC 375 ms 41616 KiB
020.txt AC 146 ms 41372 KiB
021.txt AC 159 ms 41772 KiB
022.txt AC 159 ms 41372 KiB
023.txt AC 143 ms 41424 KiB
024.txt AC 146 ms 41400 KiB
025.txt AC 198 ms 41316 KiB
026.txt AC 189 ms 41868 KiB
027.txt AC 172 ms 41572 KiB
028.txt AC 198 ms 41736 KiB
029.txt AC 302 ms 41624 KiB
030.txt AC 318 ms 42104 KiB
031.txt AC 354 ms 42112 KiB
032.txt AC 388 ms 42036 KiB
033.txt AC 389 ms 41844 KiB
034.txt AC 400 ms 41812 KiB
035.txt AC 406 ms 42012 KiB
036.txt AC 409 ms 41620 KiB
037.txt AC 409 ms 41420 KiB
example0.txt AC 70 ms 37892 KiB
example1.txt AC 71 ms 37752 KiB