hrbust 逃脱

http://acm-software.hrbust.edu.cn/problem.php?cid=1084&pid=6
hrbust 逃脱_第1张图片
hrbust 逃脱_第2张图片
hrbust 逃脱_第3张图片

一道bfs题 , 首先用bfs求出每个格子什么时间燃烧。 主要是细心。代码有点烂,懒得改了== 。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int N = 35;
int book[N][N],book2[N][N],r,c,r2,c2;
int fx[4] = {0,0,1,-1};
int fy[4] = {1,-1,0,0};
char mapp[N][N];
typedef struct Q
{
    int x,y,num;
};
Q que[10000];
void init_bfs(int x, int y);
int bfs(int x,int y);
int main()
{
    int t,i,j,r1,c1;
    cin>>t;
    while(t--)
    {
        cin>>r>>c;
        r2=-1 , c2 = -1;
        memset(book2,0,sizeof(book2));
        memset(book,0,sizeof(book));
        for(i=1; i<=r; i++)
          for(j=1; j<=c; j++)
          {
             cin>>mapp[i][j];
             if(mapp[i][j] == 'S')
                r1 = i , c1 = j;
             else if(mapp[i][j] == '*')
                r2 = i , c2 = j;
          }
          if(r2 !=-1 && c2 != -1)
             init_bfs(r2,c2);
          /*for(i=1; i<=r; i++)
          {
              for(j=1; j<=c; j++)
                cout<
          int ans = bfs(r1,c1);
          if(ans == -1)
            printf("T_T\n");
          else
            printf("%d\n",ans);
    }
    return 0;
}
void init_bfs(int x, int y)
{
    memset(que,0,sizeof(que));
    int head = 0 , tail = 1;
    que[0].x = x;
    que[0].y = y;
    que[0].num = 0;
    book2[x][y] = 1;
    while(head <= tail)
    {
        for(int i=-1; i<=1; i++)
          for(int j=-1; j<=1; j++)
         {
             int ax = que[head].x + i;
             int ay = que[head].y + j;
             if(ax>=1 && ax<=r && ay>=1 && ay<=c && book2[ax][ay]==0)
             {
                 que[tail].x = ax;
                 que[tail].y = ay;
                 que[tail].num = que[head].num + 1;
                 book2[ax][ay] = que[tail].num;
                 tail++;
             }
         }
         head++;
    }
    book2[x][y] = 0;
}
int bfs(int x,int y)
{
    memset(que,0,sizeof(que));
    int tail = 1 , head = 0,i,j;
    que[0].x = x; que[0].y = y;
    que[0].num = 0;
    book[x][y] = 1;
    while(head <= tail)
    {
        if(mapp[que[head].x][que[head].y] == 'E')
            return que[head].num;
        for(i=0; i<=3; i++)
        {
            int ax = que[head].x + fx[i];
            int ay = que[head].y + fy[i];
            int num = que[head].num + 1;
            if(ax>=1&&ax<=r && ay>=1 && ay<=c && mapp[ax][ay]!='#')
            {
                if(mapp[ax][ay] == 'E' && (num <= book2[ax][ay] || (r2==-1 && c2==-1)))
                    return num;
                if(book[ax][ay]!=1 && (num < book2[ax][ay] || (r2==-1 && c2==-1)) )
                {
                    que[tail].x = ax;
                    que[tail].y = ay;
                    que[tail].num = num;
                    book[ax][ay] = 1;
                    tail++;
                }
            }

        }
        head++;
    }

    return -1;
}

你可能感兴趣的:(BFS)