In fact, is BFS find the shortest way, first explain why BFS can find the shortest way. Because BFS is a breadth of search, is a layer, the first to reach the agreed conditions of the layer must be the shortest.
The complex in the direction of judgment, the first need to use array Has_edge[r][c][dir][trun] to record from the direction of Dir to R,c Point, turn to turn is feasible.
Then use D[r][c][dir] to record the length from direction dir to R,c, P[r][c][dir] records the parent node from direction dir to R,c.
With respect to steering, you can use a for (int i=0;i<3;i++) at each point, when i==0 represents a straight line, i==1 to turn left, and i==2 to turn right. Judge each direction, because we use a Has_edge array to record whether a shift at the point r,c is feasible.
Import java.util.LinkedList;
Import Java.util.Queue;
Import Java.util.Scanner;
Import Java.util.Vector;
public class Main {static String dirs = "NESW";
Static String turns = "FLR";
static int[] Dr = { -1,0,1,0};
Static int[] dc = {0,1,0,-1};
static int r0,r1,r2,c0,c1,c2,dir;
Static int[][][][] Has_edge = new Int[10][10][4][3];
Static int[][][] D = new int[10][10][4];
Static node[][][] p = new NODE[10][10][4];
public static void Main (string[] args) {Scanner scan = new Scanner (system.in);
while (true) {String name = Scan.next ();
if (' End '. Equals (name)) break;
for (int i=1;i<=9;i++) {to (int j=1;j<=9;j++) {for (int k=0;k<4;k++) {for (int l=0;l<3;l++) {
Has_edge[i][j][k][l] = 0;
}}} R0 = Scan.nextint ();
C0 = Scan.nextint ();
dir = dir_id (Scan.next (). CharAt (0));
r2 = Scan.nextint ();
C2 = Scan.nextint ();
R1 = R0+dr[dir];
C1 = C0+dc[dir];
while (true) {int r = Scan.nextint (); if (r==0) BreAk
int c = Scan.nextint ();
while (true) {String str = scan.next ();
char dd = str.charat (0);
if (dd== ' * ') break;
for (int i=1;i<str.length (); i++) {has_edge[r][c][dir_id (dd)][turn_id (Str.charat (i))]=1;
}} System.out.println (name);
Solve ();
} public static void Solve () {for (int i=1;i<=9;i++) {for (int j=1;j<=9;j++) {for (int k=0;k<4;k++) {
D[i][j][k] = 0;
P[I][J][K] = null;
}} queue<node> q = new linkedlist<> ();
node U = new node (r1,c1,dir);
D[u.r][u.c][u.dir] = 0;
Q.add (U);
while (!q.isempty ()) {u = Q.poll ();
if (U.R==R2&&U.C==C2) {Print_ans (U);
Return
}else{for (int i=0;i<3;i++) {Node v = walk (u,i); if (Has_edge[u.r][u.c][u.dir][i]==1&&inside (V.R,V.C) &&d[v.r][v.c][v.dir]==0) {D[v.r][v.c][v.dir]
= d[u.r][u.c][u.dir]+1;
P[v.r][v.c][v.dir] = u;
Q.add (v); }}} System.out.println (" No Solution Possible ");
The public static void Print_ans (Node u) {vector<node> v = new vector<> (); for (;;)
{v.add (U);
if (d[u.r][u.c][u.dir]==0) break;
U = P[u.r][u.c][u.dir];
} v.add (New Node (R0,c0,dir));
int cnt = 0;
for (int i=v.size () -1;i>=0;i--) {if (cnt%10==0) System.out.print ("");
System.out.printf ("(%d,%d)", V.get (i) r,v.get (i). c);
if (++cnt%10==0) System.out.println ();
} if (V.size ()%10!=0) {System.out.println ();
} public static Boolean inside (int r,int c) {return r>=1&&r<=9&&c>=1&&c<=9;
public static node Walk (Node u,int turn) {int dir = U.dir;
if (turn==1) dir = (dir+3)%4;
if (turn==2) dir = (dir+1)%4;
return new Node (U.r+dr[dir],u.c+dc[dir],dir);
public static int dir_id (char c) {return dirs.indexof (c);
public static int turn_id (char c) {return turns.indexof (c);
Static class node{int r,c,dir;
Public Node (int r,int c,int dir) {this.r = R; THIS.C = C;
This.dir = dir; }
}
}