Untitled

 avatar
unknown
plain_text
a year ago
1.7 kB
4
Indexable
package bfs.Ldgame;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.Scanner;

public class Solution {
	static Ld[] l;
	static int[][] map;

	static int n, b, m;

	public static void main(String[] args) throws Exception {
		for (int tc = 1; tc <= 10; tc++) {

			n = sc.nextInt();
			b = sc.nextInt();
			m = sc.nextInt();

			map = new int[n + 1][n + 1];
			l = new Ld[m + 1];
			for (int i = 1; i <= m; i++) {
				int x = sc.nextInt();
				int y = sc.nextInt();
				int x1 = sc.nextInt();
				int y1 = sc.nextInt();
				Hole h1 = new Hole(x, y);
				Hole h2 = new Hole(x1, y1);
				l[i] = new Ld(h1, h2);
				map[x][y] = 1;
				map[x1][y1] = 1;
			}

			for (int i = 1; i <= n; i++) {
				found = false;
				dfs(1, i);
				if (found) {
					System.out.println(i + " ");
				}
			}
		}
	}

	static boolean found = false;

	private static void dfs(int x, int y) {
		if (found)
			return;

		if (x == n) {
			if (y == b) {
				found = true;
			}
			return;
		}

		if (map[x][y] == 1) {
			int yy = getHole(x, y);
			dfs(x + 1, yy);
		} else {
			dfs(x + 1, y);
		}

	}

	private static int getHole(int x, int y) {
		for (int i = 1; i <= m; i++) {
			if (x == l[i].h1.x && y == l[i].h1.y) {
				return l[i].h2.y;
			}
			if (x == l[i].h2.x && y == l[i].h2.y) {
				return l[i].h1.y;
			}
		}
		return y;
	}


	static class Hole {
		int x, y;

		public Hole(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	static class Ld {
		Hole h1, h2;

		public Ld(Hole h1, Hole h2) {
			this.h1 = h1;
			this.h2 = h2;
		}
	}
}
Editor is loading...
Leave a Comment