今天的考核我最后!

今天的PKU上的题目我一题都没有完成,原因是我的程序大多超时。有时候我一看题目心里就会想,会不会超时呢?学数学才是王道,今天看了数论导引,华罗庚先生编著的。才几页,我就发现我是那么的渺小了。一切的算法都是围绕数学展开的。好的数学方法意味着好的算法。今天我算是体会到这一点了。

我的程序代码:

 

/*
Problem B:Basic Wall Maze
Time Limit:1000MS  Memory Limit:65536K
Total Submit:108 Accepted:62 Special Judged

Description


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

a 6 by 6 grid of unit squares
3 walls of length between 1 and 6 which are placed either horizontally or vertically to separate squares
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.


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
*/
#include "iostream"
#include "queue"
#include "stack"
using namespace std;

int main()
{
 int Map[15][15],flag=0,temp;
 char str[8],ch[100];
 int Dir[4][2]={{2,0},{-2,0},{0,-2},{0,2}};
 int d,i,x,y,len,X[10],j,x1,y1,z;
 int SPX,SPY,EPX,EPY,WSPX,WSPY,WEPX,WEPY,step;
 while(cin>>SPX>>SPY&&SPX&&SPY)
 {
  queue<int> q;
  flag=0;
  d=1;
  for(i=1;i<=14;i++)
   for(j=1;j<=14;j++)
    Map[i][j]=d++;
  cin>>EPX>>EPY;
  Map[2*SPY][2*SPX]=-1;
  Map[2*EPY][2*EPX]=-2;
  ch[0]=getchar();
  z=0;
  while(z!=3)
  {
   gets(str);
   len=strlen(str);
   if(len<7)
    break;
   WSPX=str[0]-'0';
   WSPY=str[2]-'0';
   WEPX=str[4]-'0';
   WEPY=str[6]-'0';
   if(WSPY==WEPY)
   {
    if(WSPX<WEPX)
     for(i=2*WSPX+1;i<=2*WEPX+1;i++)
      Map[2*WEPY+1][i]=-1;
    else
     for(i=2*WEPX+1;i<=2*WSPX+1;i++)
      Map[2*WEPY+1][i]=-1;
   }
   else if(WSPX==WEPX)
   {
    if(WSPY<WEPY)
     for(i=2*WSPY+1;i<=2*WEPY+1;i++)
      Map[i][2*WSPX+1]=-1;
    else
     for(i=2*WEPY+1;i<=2*WSPY+1;i++)
      Map[i][2*WSPX+1]=-1;
   }
   z++;
  }
  q.push(2*SPX);
  q.push(2*SPY);
  q.push(-1);
  while(!q.empty())
  {
   x=q.front();
   q.pop();
   y=q.front();
   q.pop();
   d=q.front();
   q.pop();
   for(i=0;i<4;i++)
   {
    if((x+Dir[i][0]>=2)&&(x+Dir[i][0]<14)&&(y+Dir[i][1]>=2)&&(y+Dir[i][1]<14))
    {
     if(Map[y+Dir[i][1]][x+Dir[i][0]]!=((y+Dir[i][1]-1)*14+(x+Dir[i][0]))||(Map[y+(Dir[i][1]/2)][x+(Dir[i][0]/2)]==-1))
     {
      if(Map[y+(Dir[i][1]/2)][x+(Dir[i][0]/2)]!=-1)
      {
       if(Map[y+Dir[i][1]][x+Dir[i][0]]==-2)
       {
        flag=1;
        Map[y+Dir[i][1]][x+Dir[i][0]]=(y-1)*14+x;
        break;
       }
      }
      continue;    
     }
     else
     {
      q.push(x+Dir[i][0]);
      q.push(y+Dir[i][1]);
      q.push(i);
      if(Map[y+Dir[i][1]][x+Dir[i][0]]==-2)
      {
       flag=1;
       Map[y+Dir[i][1]][x+Dir[i][0]]=(y-1)*14+x;
       break;
      }
      Map[y+Dir[i][1]][x+Dir[i][0]]=(y-1)*14+x;
     }

    }
   }
   if(flag)
    break;
  }
  x=2*EPX;
  y=2*EPY;
  i=0;
  while(Map[y][x]!=-1)
  {
   temp=Map[y][x];
   if(temp%14)
    x1=temp%14;
   else
    x1=14;
   y1 = ( temp - x1 ) / 14 + 1;
   if(x1==x)
   {
    if(y>y1)
     ch[i]='S';
    else
     ch[i]='N';
    i++;
   }
   if(y1==y)
   {
    if(x>x1)
     ch[i]='E';
    else
     ch[i]='W';
    i++;
   }
   y=y1;
   x=x1;
  }
  for(j=i-1;j>=0;j--)
   cout<<ch[j];
  cout<<endl;
 }
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值