西工大算法设计与分析 noj1653 僵尸又来了(实验三)

#include<iostream>
#include<queue>
using namespace std;

#define _for(i, a, b)   for(int i = (a); i < (b); i ++ )
#define _rep(i, a, b)   for(int i = (a); i <= (b); i ++ )

struct state
{
    int x, y, t_f, t;
    state() {}
    state(int a, int b, int c, int d) : x(a), y(b), t_f(c), t(d) {}
}in, out;

const int N = 210;
int m, n, tfl, p;
int g[N][N];
bool st[N][N][15];

int update(int f_t)
{
    if(f_t > 0) f_t -= 1;
    return f_t;
}

int bfs()
{
    int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
    queue<state> q;
    q.push(in);

    while(q.size())
    {
        state t = q.front();
        q.pop();

        if(t.x == out.x && t.y == out.y)
            return t.t;

        if(st[t.x][t.y][t.t_f]) continue;
        st[t.x][t.y][t.t_f] = 1;
		
        _for(i, 0, 4)
        {
            int x = t.x + dx[i], y = t.y + dy[i];
            if(x > 0 && x <= m && y > 0 && y <= n && t.t < p)
            {
                if(g[x][y] == 1) continue;
                else if(!g[x][y])
				    q.push(state(x, y, update(t.t_f), t.t + 1));
                else if(g[x][y] == 2)
                {
                	t.t_f = update(t.t_f);
                    if(t.t_f > 0)
                    	q.push(state(x, y, -1, t.t + 1));
                    else if(t.t_f == 0)
                    	q.push(state(in.x, in.y, tfl, t.t + 1));
                    else if(t.t_f == -1)
                    	q.push(state(x, y, -1, t.t + 1));
                }
            }       
        }
    }
    return -1;
}

int main()
{
    cin >> m >> n >> tfl >> p;
    _rep(i, 1, m)
        _rep(j, 1, n)
        {
            char c;
            cin >> c;
            if(c == '#')    g[i][j] = 1;//墙
            else if(c == '*')   g[i][j] = 0;//通路
            else if(c == '!')   in = state(i, j, 0, 0);
            else if(c == '+')   out = state(i, j, 0, 0);
            else g[i][j] = 2;//大嘴花
        }
    cout << bfs() << endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值