nefu 674HELP(bfs)

HELP

Time Limit 1000ms

Memory Limit 65536K

description

    You are a warrior, but your dear princess was caught by big devil. you want to save the princess. each round, you can go around (up ,down ,left, right) ,if the place you stand is a slime or snake you will dead, if the place you stand is a boat or a sword you will take it always, if the place you stand is princess then you save her success . You can also kill a Slime or snake around you in one round, then the place become empty space. at first you can only beat over slime, if you got the sword, you can beat over snake. You can not stand on the river, only when you get a boat. Please find the minimum round to save princess.
							

input

    The input consists of several test cases. The first line of each test case contains two integers M and N (2 <= M, N <= 300). Each of the following M lines contains N uppercase letters, each of which is one of 'Y' (you), 'P'(princess), 'S' (snake), 'L' (Slime), 'R' (river), 'W' (sword), 'B' (boat) and 'E' (empty space). Both 'Y' and 'P' appear only once, 'B' and 'W' appear at most once. A test case of M = N = 0 indicates the end of input, and should not be processed.
							

output

For each test case, please output the turns you take at least in a separate line. If you can't arrive at the target, output "-1" instead.
							

sample_input

3 4
YLEL
EERE
SSPE
5 5
YEEER
ESSRR
BSSSP
RRRRR
EEEEE
0 0
							

sample_output

8
8
							

hint

拿到sword或boat后地图的怪重置,标程是这样的= =
								
							

source

思路:进行深搜,广搜,用优先队列优化,每次找step最大的,然后,找出你到公主的路,你到BOAT->pricess y->w->p y->b->w->p y->w->b->p;

取最优值即可。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int mm=311;
const int oo=1e9;
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
class node
{
  public:int x,y,step;

};
bool operator<(const node&a,const node&b)///重载优先队列
{
    return a.step>b.step;
}
char g[mm][mm];
bool vis[mm][mm];
int mx,my,px,py,bx,by,wx,wy,n,m;
priority_queue<node>q;
int bfs(int sx,int sy,int ex,int ey,int op)
{ memset(vis,0,sizeof(vis));
  node t,p;t.x=sx;t.y=sy;t.step=0;
  while(!q.empty())q.pop();
  q.push(t);
  vis[sx][sy]=1;
  while(!q.empty())
  { t=q.top();q.pop();
    for(int i=0;i<4;++i)
    {
      p.x=t.x+dx[i];p.y=t.y+dy[i];
      if(p.x<0||p.x>=n||p.y<0||p.y>=m||vis[p.x][p.y])continue;
      if(g[p.x][p.y]=='S'&&!(op&1))continue;
      if(g[p.x][p.y]=='R'&&!(op&2))continue;
      if(p.x==ex&&p.y==ey)return t.step+1;
      if(g[p.x][p.y]=='S'||g[p.x][p.y]=='L')p.step=t.step+2;
      if(g[p.x][p.y]=='E'||g[p.x][p.y]=='R')p.step=t.step+1;
      if(g[p.x][p.y]=='W'||g[p.x][p.y]=='Y'||g[p.x][p.y]=='B'||g[p.x][p.y]=='P')
       continue;
      q.push(p);vis[p.x][p.y]=1;
    }
  }
  return oo;
}
int main()
{ freopen("data.in","r",stdin);
  while(scanf("%d%d",&n,&m)&&(n||m))
  {
    for(int i=0;i<n;++i)
      scanf("%s",g[i]);
     bx=-1;wx=-1;
    for(int i=0;i<n;++i)
      for(int j=0;j<m;++j)
      if(g[i][j]=='Y')mx=i,my=j;
      else if(g[i][j]=='P')px=i,py=j;
      else if(g[i][j]=='B')bx=i,by=j;
      else if(g[i][j]=='W')wx=i,wy=j;
      int ans=oo,tmp;
     tmp=bfs(mx,my,px,py,0);
     ans=min(ans,tmp);
     if(bx>=0)///有船
     {
       tmp=bfs(mx,my,bx,by,0);
       tmp+=bfs(bx,by,px,py,2);
       ans=min(ans,tmp);
       if(wx>=0)
       {
         tmp=bfs(mx,my,bx,by,0);
         tmp+=bfs(bx,by,wx,wy,2);
         tmp+=bfs(wx,wy,px,py,3);
         ans=min(ans,tmp);///ren->boat->sword->pricess
       }
     }
     if(wx>=0)
      {
        tmp=bfs(mx,my,wx,wy,0);
        tmp+=bfs(wx,wy,px,py,1);
        ans=min(ans,tmp);
        if(bx>=0)
        {
          tmp=bfs(mx,my,wx,wy,0);
          tmp+=bfs(wx,wy,bx,by,1);
          tmp+=bfs(bx,by,px,py,3);
          ans=min(ans,tmp);///ren->sword->boat->pricess
        }
      }
    if(ans<oo)
      printf("%d\n",ans);
    else printf("-1\n");
  }
}




posted @ 2013-04-29 21:08  剑不飞  阅读(147)  评论(0编辑  收藏  举报