poj_2935 Basic Wall Maze

將每個格子根據所在行列都給一個編號,從1-36,這些編號的點之間有邊相連,若有墻隔開則沒有邊,根據所給的墻的編號算出其能阻隔的點,然後從起點到終點做bfs即可得到最短路。
2935 Accepted 584K 0MS G++ 2558B 2014-07-16 00:16:06
題目:
Basic Wall Maze
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2797   Accepted: 1273   Special Judge

Description

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:

poj_2935 Basic Wall Maze_第1张图片

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.

Input

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.

Output

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.

Sample Input

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

Sample Output

NEEESWW

Source

Ulm Local 2006
 
代碼:
#include
#include
#include
#include
using namespace std;
int dirx[5]={0,1,0,-1},diry[5]={1,0,-1,0};
int visit[40],g[40][40];;
char dir[5]={'N','S','W','E'};
struct road{
   int p,d;
}r[40];
void bfs(int s1,int e1,int s2,int e2)
{
    queueq;
    int dis[40],i;
    memset(dis,0,sizeof(dis));
    for(i=0;i<40;i++) r[i].p=0;
    while(!q.empty()) q.pop();
    int t=s1+(e1-1)*6;
    int aim=s2+(e2-1)*6;
    q.push(t);
    visit[t]=1;
    int ca=0;
    while(!q.empty())
    {
//       printf("ca=%d\n",++ca);
     int reach;
     int h=q.front();
         q.pop();
     int hx=h%6;
     int hy=h/6;
     if(hx==0) hx=6;
      else hy++;
     for(i=0;i<4;i++)
     {
      int u=hx+dirx[i];
      int v=hy+diry[i];
      reach=0;
      if(u>0 && u<7 && v>0 && v<7)
         {
         int t=u+(v-1)*6;
         if(!visit[t] && !g[h][t])
            {
               visit[t]=1;
               r[t].p=h;
               q.push(t);
               if(dirx[i]==0 && diry[i]==1)  r[t].d=1;
                  else if(dirx[i]==0 && diry[i]==-1) r[t].d=0;
                  else if(dirx[i]==1 && diry[i]==0)   r[t].d=3;
                  else if(dirx[i]==-1 && diry[i]==0)  r[t].d=2;
               if(aim==t) {reach=1; break;}
            }
         }
     }
     if(reach) break;
    }
    return;
}
void print(int k)
{
//    printf("k=%d en\n",k);
   if(r[k].p==0) return;
   print(r[k].p);
   int u=r[k].d;
   printf("%c",dir[u]);

}
int main()
{
   freopen("in.txt","r",stdin);
   int i,j,n,m,sx,sy,ex,ey;
   while(1)
   {
      scanf("%d%d",&sx,&sy);
      if(!sx && !sy) break;
//      printf("%d  %d\n",sx,sy);
      scanf("%d%d",&ex,&ey);
      memset(g,0,sizeof(g));
      for(i=0;i<3;i++)
      {
       int x1,y1,x2,y2;
       scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
       if(x1==x2 && x1>0 && x1<6)
       {
         for(j=y1+1;j<=y2;j++)
         {
            int u=(j-1)*6;
            int k1=u+x1;
            int k2=u+x1+1;
            g[k1][k2]=1;
            g[k2][k1]=1;
         }
       }
       else if(y1==y2 && y1>0 && y1<6)
            {
             int u=(y1-1)*6;
             for(j=x1+1;j<=x2;j++)
             {
               int k1=u+j;
               int k2=u+j+6;
               g[k1][k2]=1;
               g[k2][k1]=1;
             }
            }
      }
      memset(visit,0,sizeof(visit));
      bfs(sx,sy,ex,ey);
      print(ex+(ey-1)*6);
      printf("\n");
   }
   return 0;
}

你可能感兴趣的:(day5,圖論_bfs)