哈尔滨理工大学第七届程序设计竞赛初赛(高年级组)G(BFS)

题目链接:https://www.nowcoder.com/acm/contest/27/G

思路:预处理S点和*点,按顺序push进队列,然后BFS,判断对头是否有火,若有则扩展火的移动,若无则扩展人的移动。这里建一个huo[][]数组来存储每个点火的状态,如果已经是1了就不能再进队列了!

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=1000;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
char g[35][35],vis[35][35],huo[35][35];
struct point
{
    int x,y,step,ifh;
};
int hdx[]= {0,0,-1,-1,-1,1,1,1};
int hdy[]= {-1,1,-1,0,1,-1,0,1};
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
point s,e,h;
int n,m;
void bfs()
{
    memset(vis,0,sizeof(vis));
    memset(huo,0,sizeof(huo));
    queue<point> q;
    q.push(s);
    q.push(h);
    vis[s.x][s.y]=1;
    huo[h.x][h.y]=1;
    while(!q.empty())
    {
        point f=q.front();
        q.pop();
        if(f.ifh==0)
        {
            if(g[f.x][f.y]=='E')
            {
                cout<<f.step<<endl;
                return ;
            }
            if(huo[f.x][f.y])
                continue;
            for(int i=0; i<4; i++)
            {
                point te=f;
                int x=f.x+dx[i];
                int y=f.y+dy[i];
                if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&g[x][y]!='#'&&!huo[x][y])
                {
                    vis[x][y]=1;
                    te.x=x;
                    te.y=y;
                    te.step++;
                    q.push(te);
                }
            }
        }
        else
        {
            for(int i=0; i<8; i++)
            {
                point te=f;
                int x=f.x+hdx[i];
                int y=f.y+hdy[i];
                if(x>=0&&x<n&&y>=0&&y<m&&!huo[x][y])
                {
                    huo[x][y]=1;
                    te.x=x;
                    te.y=y;
                    q.push(te);
                }
            }
        }
        /*    for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    cout<<g[i][j]<<" ";
                }
                cout<<endl;
            }*/
    }

        cout<<"T_T"<<endl;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>g[i][j];
                if(g[i][j]=='S')
                {
                    s.x=i;
                    s.y=j;
                    s.step=0;
                    s.ifh=0;
                }
                if(g[i][j]=='*')
                {
                    h.x=i;
                    h.y=j;
                    h.ifh=1;
                }
            }
        }

        bfs();
        /*     for(int i=0; i<n; i++)
             {
                 for(int j=0; j<m; j++)
                 {
                     cout<<g[i][j]<<" ";
                 }
                 cout<<endl;
             }*/
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值