2013
11-12

# Basic Wall Maze

In this problem you have to solve a very simple maze consisting of:

1. a 6 by 6 grid of unit squares
2. 3 walls of length between 1 and 6 which are placed either horizontally or vertically to separate squares
3. one start and one end marker

A maze may look like this:

You have to find a shortest path between the square with the start marker and the square with the end marker. Only moves between adjacent grid squares are allowed; adjacent means that the grid squares share an edge and are not separated by a wall. It is not allowed to leave the grid.

The input consists of several test cases. Each test case consists of five lines: The first line contains the column and row number of the square with the start marker, the second line the column and row number of the square with the end marker. The third, fourth and fifth lines specify the locations of the three walls. The location of a wall is specified by either the position of its left end point followed by the position of its right end point (in case of a horizontal wall) or the position of its upper end point followed by the position of its lower end point (in case of a vertical wall). The position of a wall end point is given as the distance from the left side of the grid followed by the distance from the upper side of the grid.

You may assume that the three walls don’t intersect with each other, although they may touch at some grid corner, and that the wall endpoints are on the grid. Moreover, there will always be a valid path from the start marker to the end marker. Note that the sample input specifies the maze from the picture above.

The last test case is followed by a line containing two zeros.

For each test case print a description of a shortest path from the start marker to the end marker. The description should specify the direction of every move (‘N’ for up, ‘E’ for right, ‘S’ for down and ‘W’ for left).

There can be more than one shortest path, in this case you can print any of them.

1 6
2 6
0 0 1 0
1 5 1 6
1 5 3 5
0 0

NEEESWW

//* @author: [email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */
import java.io.*;
import java.util.*;
public class Main
{
static int x1,y1,x2,y2,l;
static boolean[][] p=new boolean[8][8];
static int[] ax1,ay1,ax2,ay2;
public static void main(String[] args) throws IOException
{
String[] ss;
while(true)
{
for(int i=0;i< 8;i++)
for(int j=0;j< 8;j++)
p[i][j]=false;
for(int i=0;i< 8;i++)
{
p[0][i]=true;
p[7][i]=true;
p[i][0]=true;
p[i][7]=true;
}
ax1=new int[50];
ay1=new int[50];
ax2=new int[50];
ay2=new int[50];
l=0;
y1=Integer.parseInt(ss[0]);
x1=Integer.parseInt(ss[1]);
if(x1==0&&y1==0)break;
y2=Integer.parseInt(ss[0]);
x2=Integer.parseInt(ss[1]);
int a1,a2,b1,b2;
for(int i=0;i< 3;i++)
{
a1=Integer.parseInt(ss[0]);
b1=Integer.parseInt(ss[1]);
a2=Integer.parseInt(ss[2]);
b2=Integer.parseInt(ss[3]);
if(a1==a2)
{
if(b1>b2)
{
int temp=b1;
b1=b2;
b2=temp;
}
for(int j=b1+1;j<=b2;j++)
{
ay1[l]=a1;
ay2[l]=a1+1;
ax1[l]=ax2[l]=j;
l++;
}
}
if(b1==b2)
{
if(a1>a2)
{
int temp=a1;
a1=a2;
a2=temp;
}
for(int j=a1+1;j<=a2;j++)
{
ax1[l]=b1;
ax2[l]=b1+1;
ay1[l]=ay2[l]=j;
l++;
}
}
}
bfs();
System.out.println();
}

}

static boolean f(int x,int y,int x1,int y1)
{
for(int i=0;i< l;i++)
{
if(x==ax1[i]&&y==ay1[i]&&x1==ax2[i]&&y1==ay2[i])
return false;
if(x1==ax1[i]&&y1==ay1[i]&&x==ax2[i]&&y==ay2[i])
return false;
}
return true;
}

static void ff(ri tt)
{
if(tt.root!=null)ff(tt.root);
if(tt.root==null)return;
if(tt.x==tt.root.x&&tt.y==tt.root.y+1)
System.out.print("E");
else if(tt.x==tt.root.x&&tt.y==tt.root.y-1)
System.out.print("W");
else if(tt.x==tt.root.x+1&&tt.y==tt.root.y)
System.out.print("S");
else if(tt.x==tt.root.x-1&&tt.y==tt.root.y)
System.out.print("N");
}

static void bfs()
{
ri orz=new ri(x1,y1,null);
p[x1][y1]=true;
while(!qu.isEmpty())
{
ri temp=qu.poll();
if(temp.x==x2&&temp.y==y2)
{
ff(temp);
return;
}
int x=temp.x;
int y=temp.y;
if(!p[x+1][y])
{
if(f(x,y,x+1,y))
{
ri uu=new ri(x+1,y,temp);
p[x+1][y]=true;
}
}
if(!p[temp.x-1][temp.y])
{
if(f(x,y,x-1,y))
{
ri uu=new ri(x-1,y,temp);
p[x-1][y]=true;
}
}
if(!p[temp.x][temp.y+1])
{
if(f(x,y,x,y+1))
{
ri uu=new ri(x,y+1,temp);
p[x][y+1]=true;
}
}
if(!p[temp.x][temp.y-1])
{
if(f(x,y,x,y-1))
{
ri uu=new ri(x,y-1,temp);
p[x][y-1]=true;
}
}
}
}
}
class ri
{
int x,y;
ri root;
public ri(int xx,int yy,ri t)
{
x=xx;
y=yy;
root =t;
}
}