Fire!

文章链接

  • 分析:
    火和人的行动是互不影响的,应该独立进行。先进行火的bfs,记录一下火到每个点的步数,在人的bfs时到相应的格子时必须小于这个数
const int MAXN = 1100;

struct Node
{
    int x, y, foot;
    Node (int x = 0, int y = 0, int foot = 0) : x(x), y(y), foot(foot){}
} people;
char m[MAXN][MAXN];
int step[MAXN][MAXN];
queue<Node> q;
int kase, a, b;

int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

inline void init()
{
    while (!q.empty()) q.pop();
}

void bfs_fire()
{
    Node t;
    while (!q.empty())
    {
        t = q.front();
        q.pop();
        REP(i, 4)
        {
            int tx = t.x + dx[i];
            int ty = t.y + dy[i];
            if (m[tx][ty] == '.' && step[tx][ty] == INF)
            {
                step[tx][ty] = t.foot + 1;
                q.push(Node(tx, ty, t.foot + 1));
            }
        }
    }
}

bool bfs_people(Node& ans)
{
    while (!q.empty())
    {
        ans = q.front();
        q.pop();
        if (ans.x == 1 || ans.x == a || ans.y == 1 || ans.y == b)
            return true;
        REP(i, 4)
        {
            int tx = ans.x + dx[i];
            int ty = ans.y + dy[i];
            if (m[tx][ty] == '.' && ans.foot + 1 < step[tx][ty])
            {
                m[tx][ty] = 'J';
                q.push(Node(tx, ty, ans.foot + 1));
            }
        }
    }
    return false;
}

int main()
{
//    freopen("in.txt", "r", stdin);
    RI(kase);
    REP(kk, kase)
    {
        CLR(step, INF);
        CLR(m, '#');
        init();

        RII(a, b);
        FE(i, 1, a) FE(j, 1, b)
        {
            cin >> m[i][j];
            if (m[i][j] == 'J')
            {
                people.x = i, people.y = j;
                people.foot = 0;
                m[i][j] = '.';
            }
            else if (m[i][j] == 'F')
            {
                step[i][j] = 0;
                q.push(Node(i, j, 0));
                m[i][j] = '.';
            }
        }
        bfs_fire();

        init();
        q.push(people);
        m[people.x][people.y] = 'J';
        if (bfs_people(people))
            cout << people.foot + 1 << endl;
        else
            cout << "IMPOSSIBLE" << endl;
    }
    return 0;
}


下面是补充完整的代码: ``` // 你的代码将嵌入这里 class Bullet { private int count = 0; private boolean loaded = false; public synchronized void addBullet() { while (loaded) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } count++; System.out.println("lock and load~~~~"); loaded = true; notifyAll(); } public synchronized void shootBullet() { while (!loaded) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("fire!!!"); loaded = false; count--; notifyAll(); } public synchronized boolean isEmpty() { return count == 0; } } class AddBullet implements Runnable { private Bullet bullet; public AddBullet(Bullet bullet) { this.bullet = bullet; } @Override public void run() { while (true) { if (bullet.isEmpty()) { bullet.addBullet(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class ShootBullet implements Runnable { private Bullet bullet; public ShootBullet(Bullet bullet) { this.bullet = bullet; } @Override public void run() { int count = 0; while (count < 3) { if (!bullet.isEmpty()) { bullet.shootBullet(); count++; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main { public static void main(String[] args) { Bullet bullet = new Bullet(); AddBullet ab = new AddBullet(bullet); ShootBullet sb = new ShootBullet(bullet); Thread t1 = new Thread(ab); Thread t2 = new Thread(sb); t1.start(); t2.start(); } } ``` 运行结果: ``` lock and load~~~~ fire!!! lock and load~~~~ fire!!! lock and load~~~~ fire!!! ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值