-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labyrinth code in Graph Algorithms does not work!!!! #1
Comments
Sorry Bro, Kuch kuch code AC nahi hai lekin save the files too sath me commit ho gaye. Kabhi fir CSES kiya too changes karunga. |
Here is the AC Code for Labyrinth: import java.io.*;
import java.util.*;
@SuppressWarnings("all")
public class Main {
static int endi, endj;
static void solve() throws IOException{
StringTokenizer st;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
char mat[][] = new char[n][m];
char dir[][] = new char[n][m];
fill2D(dir, '#');
int si = 0, sj = 0;
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
mat[i] = st.nextToken().toCharArray();
for (int j = 0; j < m; j++) {
if (mat[i][j] == 'A') {
si = i;
sj = j;
} else if (mat[i][j] == 'B') {
endi = i;
endj = j;
}
}
}
boolean f = false;
ArrayDeque<int[]> q = new ArrayDeque<>();
q.offerLast(new int[]{si, sj});
while (!q.isEmpty()) {
int[] tmp = q.pollFirst();
// System.out.println(tmp);
if (tmp[0] == endi && tmp[1] == endj) {
f = true;
break;
}
int nx, ny;
// Left
nx = tmp[0];
ny = tmp[1] - 1;
if (!(nx < 0 || nx >= n || ny < 0 || ny >= m || mat[nx][ny] == '#')) {
q.offerLast(new int[]{nx,ny});
dir[nx][ny] = 'L';
mat[nx][ny] = '#';
}
// Right
nx = tmp[0];
ny = tmp[1] + 1;
if (!(nx < 0 || nx >= n || ny < 0 || ny >= m || mat[nx][ny] == '#')) {
q.offerLast(new int[]{nx,ny});
dir[nx][ny] = 'R';
mat[nx][ny] = '#';
}
// Up
nx = tmp[0] - 1;
ny = tmp[1];
if (!(nx < 0 || nx >= n || ny < 0 || ny >= m || mat[nx][ny] == '#')) {
q.offerLast(new int[]{nx,ny});
dir[nx][ny] = 'U';
mat[nx][ny] = '#';
}
// Down
nx = tmp[0] + 1;
ny = tmp[1];
if (!(nx < 0 || nx >= n || ny < 0 || ny >= m || mat[nx][ny] == '#')) {
q.offerLast(new int[]{nx,ny});
dir[nx][ny] = 'D';
mat[nx][ny] = '#';
}
// System.out.println(q);
}
StringBuilder s = new StringBuilder();
if (!f)
System.out.println("NO");
else {
int nx = endi, ny = endj;
while(nx!=si || ny!=sj)
{
s.append(dir[nx][ny]);
switch(dir[nx][ny])
{
case 'L':{ny++;break;}
case 'R':{ny--;break;}
case 'U':{nx++;break;}
case 'D':{nx--;break;}
}
}
s.reverse();
output.write("YES\n");
output.write(s.length() + "\n");
output.write(s.toString());
}
output.flush();
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// sc.print(dir[i][j]);
// }
// sc.println();
// }
/*
* for(int i = 0;i<n;i++) { for(int j = 0; j<m;j++) { sc.print(mat[i][j]); }
* sc.println(); }
*/
}
public static void main(String[] args) throws IOException{
// FastReader(true) for File I/O
// FastReader() for terminal I/O
if (args.length > 0 && args[0].equals("local")) {
//FastReader sc = new FastReader(true);
// CODE BEGIN
// for(int T = sc.nextInt();T > 0;T--)
solve();
// CODE END
//sc.closer();
} else {
//FastReader sc = new FastReader();
// CODE BEGIN
// for(int T = sc.nextInt();T > 0;T--)
solve();
// CODE END
//sc.closer();
}
}
static int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void fill2D(char arr[][], char n) {
for (char[] row : arr)
Arrays.fill(row, n);
}
static class pair {
int x;
int y;
pair(int i, int j) {
x = i;
y = j;
}
public String toString() {
return x + " " + y;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
PrintWriter pw;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
public FastReader(boolean b) {
try {
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter("output.txt");
} catch (Exception e) {
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] nextArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
pw.print(' ');
pw.print(objects[i]);
}
}
void println(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
pw.print(' ');
pw.print(objects[i]);
}
pw.println();
}
void viewArray1D(int a[]) {
println(Arrays.toString(a));
}
void viewArray2D(int arr[][]) {
for (int[] row : arr)
viewArray1D(row);
}
void closer() {
try {
br.close();
pw.flush();
pw.close();
} catch (Exception e) {
}
}
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bhai AC wala code daalo 🤣
The text was updated successfully, but these errors were encountered: