Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import static java.lang.Math.*;
- public class Labyrinth implements Runnable {
- final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
- BufferedReader in;
- PrintWriter out;
- StringTokenizer tok;
- @Override
- public void run() {
- try {
- long startTime = System.currentTimeMillis();
- if (ONLINE_JUDGE) {
- in = new BufferedReader(new InputStreamReader(System.in));
- out = new PrintWriter(System.out);
- } else {
- in = new BufferedReader(new FileReader("input.txt"));
- out = new PrintWriter("output.txt");
- }
- tok = new StringTokenizer("");
- Locale.setDefault(Locale.US);
- solve();
- in.close();
- out.close();
- long endTime = System.currentTimeMillis();
- long totalMemory = Runtime.getRuntime().totalMemory();
- long freeMemory = Runtime.getRuntime().freeMemory();
- System.err.println("Time = " + (endTime - startTime) + " ms");
- System.err.println("Memory = " + ((totalMemory - freeMemory) / 1024) + " KB");
- } catch (Throwable e) {
- e.printStackTrace(System.err);
- System.exit(-1);
- }
- }
- String readString() throws IOException {
- while (!tok.hasMoreTokens()) {
- tok = new StringTokenizer(in.readLine());
- }
- return tok.nextToken();
- }
- int readInt() throws IOException {
- return Integer.parseInt(readString());
- }
- long readLong() throws IOException {
- return Long.parseLong(readString());
- }
- double readDouble() throws IOException {
- return Double.parseDouble(readString());
- }
- void debug(Object... o) {
- if (!ONLINE_JUDGE) {
- System.err.println(Arrays.deepToString(o));
- }
- }
- public static void main(String[] args) {
- new Thread(null, new Labyrinth(), "", 256 * 1024 * 1024).start();
- }
- //------------------------------------------------------------------------------
- final int[] dx = {0, 1, 0, -1};
- final int[] dy = {-1, 0, 1, 0};
- int n, m;
- char[][] a;
- int[][] wx, wy;
- boolean[][] vis;
- void solve() throws IOException {
- n = readInt();
- m = readInt();
- a = new char[n][];
- for (int i = 0; i < n; i++) {
- a[i] = readString().toCharArray();
- }
- int sx = -1, sy = -1;
- loop: for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (a[i][j] == 'S') {
- sx = i;
- sy = j;
- break loop;
- }
- }
- }
- wx = new int[n][m];
- wy = new int[n][m];
- vis = new boolean[n][m];
- if (dfs(sx, sy, 0, 0)) {
- out.println("Yes");
- } else {
- out.println("No");
- }
- }
- private boolean dfs(int x, int y, int shiftX, int shiftY) {
- if (vis[x][y]) {
- if (wx[x][y] != shiftX || wy[x][y] != shiftY) {
- return true;
- } else {
- return false;
- }
- }
- vis[x][y] = true;
- wx[x][y] = shiftX;
- wy[x][y] = shiftY;
- for (int k = 0; k < 4; k++) {
- int newX = x + dx[k];
- int newY = y + dy[k];
- int newShiftX = shiftX;
- int newShiftY = shiftY;
- if (newX >= n) { newX = 0; newShiftX++; }
- if (newY >= m) { newY = 0; newShiftY++; }
- if (newX < 0) { newX = n-1; newShiftX--; }
- if (newY < 0) { newY = m-1; newShiftY--; }
- if (a[newX][newY] != '#') {
- if (dfs(newX, newY, newShiftX, newShiftY)) {
- return true;
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement