Comments (2)
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.
Thanks for telling.
from dsa.
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) {
}
}
}
}
from dsa.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dsa.