2013
12-11

# 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

#include<stdio.h>
#include<queue>
using namespace std;
int sx,sy,ex,ey;
int h[4]={1,-1,0,0};
int g[4]={0,0,1,-1};
int dir[8][8][5];
bool visit[7][7];
struct point{
int x;
int y;
int time;
int pre;
char dir;
}que[1000];
void limdir(int a,int b,int c,int d){
int i;
if(a==c){
for(i=b+1;i<=d;i++){
dir[c][i][0]=1;
dir[c+1][i][1]=1;
}
}
else if(b==d){
for(i=a+1;i<=c;i++){
dir[i][b][2]=1;
dir[i][b+1][3]=1;
}
}
}
char cal(int i){
if(i==0)
return 'S';
else if(i==1)
return 'N';
else if(i==2)
return 'E';
else
return 'W';
}
void print(int tem){
if(que[tem].pre!=-1){
print(que[tem].pre);
printf("%c",que[tem].dir);
}
}
int bfs(){
int front=0,rear=0,xx,yy,i,j,k;
struct point cur;
cur.x=sx;
cur.y=sy;
cur.time=0;
cur.pre=-1;
que[rear++]=cur;

while(front<rear){
struct point tem;
tem=que[front];

for(i=0;i<4;i++){
xx=tem.x+h[i];
yy=tem.y+g[i];

if(xx<=0 || xx>6 || yy<=0 || yy>6)
continue;
if(dir[tem.x][tem.y][i])
continue;
if(visit[xx][yy])
continue;

struct point pp;
pp.x=xx;pp.y=yy;pp.time=tem.time+1;
pp.pre=front;pp.dir=cal(i);
que[rear]=pp;
visit[xx][yy]=1;
if(xx==ex && yy==ey){
print(rear);
printf("\n");
return 1;
}
rear++;
}
front++;
}
return 0;
}

int main(){
int i,j,k,a,b,c,d;
while(scanf("%d %d",&sy,&sx) && !(sx==0 && sy==0)){
scanf("%d %d",&ey,&ex);
memset(dir,0,sizeof(dir));
memset(visit,0,sizeof(visit));
for(i=1;i<=3;i++){
scanf("%d %d %d %d",&b,&a,&d,&c);
limdir(a,b,c,d);
}
visit[sx][sy]=1;
bfs();
}
}

1. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术，也不懂什么是算法，从而也不要求程序员懂什么算法，做程序从来不考虑性能问题，只要页面能显示出来就是好程序，这是国内的现状，很无奈。

2. 5.1处，反了；“上一个操作符的优先级比操作符ch的优先级大，或栈是空的就入栈。”如代码所述，应为“上一个操作符的优先级比操作符ch的优先级小，或栈是空的就入栈。”

3. 我还有个问题想请教一下，就是感觉对于新手来说，递归理解起来有些困难，不知有没有什么好的方法或者什么好的建议？