zoukankan      html  css  js  c++  java
  • Light OJ 1175

    题目链接


    题意: 在一个二维平面,J只能上下左右移动,F表示火,每过一秒火势会向四周扩散,问J能在几秒之内逃出去(到达边界)。

    思路:
    BFS每次到了一个新的时间就更新地图就好了。其他和地图寻路一样。

    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <vector>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <set>
    #include <stack>
    #include <string>
    #include <math.h>
    #include <bitset>
    #include <ctype.h>
    using namespace std;
    typedef pair<int,int> P;
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-9;
    const int N = 225 + 5;
    const int mod = 1e9 + 7;
    int t, kase = 0;
    int n,m;
    char mp[N][N];
    int vis[N][N];
    int dirx[] = {0,1,0,-1};
    int diry[] = {1,0,-1,0};
    struct Node
    {
        int x,y;
        int t;
        Node(int a, int b, int c):x(a),y(b),t(c) {}
    };
    void updatemap()
    {
        char mmp[N][N];
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                mmp[i][j] = mp[i][j];
            }
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(mp[i][j] == 'F')
                {
                    int x =i, y = j;
                    for(int k = 0; k < 4; k++)
                    {
                        int nx = x + dirx[k], ny = y + diry[k];
                        if(nx < 0 || ny < 0 || nx >= n || ny >= m || mmp[nx][ny] != '.') continue;
                        mmp[nx][ny] = 'F';
                    }
                }
            }
        }
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                mp[i][j] = mmp[i][j];
    }
    int bfs(int x, int y)
    {
        queue<Node> Q;
        memset(vis, 0, sizeof(vis));
        vis[x][y] = 1;
        int curtime = 0;
        Q.push(Node(x,y,0));
        while(!Q.empty())
        {
            Node node = Q.front(); Q.pop();
            int x = node.x, y = node.y, t = node.t;
            if(t == curtime)
            {
                updatemap();
                curtime++;
            }
            if(x == n-1 || y == m-1 || x == 0 || y == 0) return t+1;
            for(int i = 0; i < 4; i++)
            {
                int nx = x + dirx[i], ny = y + diry[i], nt = t + 1;
                if(nx < 0 || ny < 0 || nx >= n || ny >= m || mp[nx][ny] != '.') continue;
                if(vis[nx][ny]) continue;
                vis[nx][ny] = 1;
                Q.push(Node(nx,ny,nt));
            }
        }
        return -1;
    }
    
    int sx,sy;
    int main()
    {
        scanf("%d", &t);
        while(t--)
        {
            scanf("%d%d", &n, &m);
            getchar();
            for(int i = 0; i < n; i++)
                gets(mp[i]);
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < m; j++)
                    if(mp[i][j] == 'J')
                        sx = i, sy = j;
            }
            int ans = bfs(sx,sy);
            printf("Case %d: ", ++kase);
            if(ans == -1)
                printf("IMPOSSIBLE
    ");
            else
                printf("%d
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    使用react hook做一个小型完整项目(包括二级路由,动态路由,redux,tab切换,列表跳详情,登录, 守卫)
    项目实战【vue,react,微信小程序】(1705E)
    Vue(1706E)
    加入购物车动画(css)
    React从入门到精通(1704B)
    React(1702H)文章管理-cms系统
    React(1702H)文件上传和分页查找
    React (1702H) 点击复制、滚动条、图形验证码、ridis、密码rsa加密、token、发邮件、文件上传、sql语句
    位图算法
    def跨域+jwt
  • 原文地址:https://www.cnblogs.com/Alruddy/p/7515470.html
Copyright © 2011-2022 走看看