ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NC15445 wyh的吃鸡

2022-07-16 19:36:01  阅读:180  来源: 互联网

标签:cur int st yy xx NC15445 dt wyh


题目链接

题目

题目描述

最近吃鸡游戏非常火,你们wyh学长也在玩这款游戏,这款游戏有一个非常重要的过程,就是要跑到安全区内,否则就会中毒持续消耗血量,我们这个问题简化如下

假设地图为n*n的一个图,图中有且仅有一块X的联通快代表安全区域,有一个起点S代表缩圈的时候的起点,图中C代表的是车(保证车的数量小于等于100),标记为.的代表空地,可以任意通过,O代表障碍物不能通过。每次没有车的时候2s可以走一个格(只能走自己的上下左右4个方向),有车的话时间为1s走一个格

现在告诉你最多能坚持的时间为t秒,问你在t秒内(含t秒)能否从s点到达安全区域,能的话输出YES,并且输出最短时间,不能的话输出NO

输入描述

输入第一行一个整数T(1<=T<=10)
接下来有T组测试数据,每组测试数据输入2个数n和k(1<=n<=100,1<=k<=10^9)
接下来n行,每行n个字符,代表对应的n*n的地图,每个字符都是上面的一种,并且保证只有一个起点,只有一块安全区域。

输出描述

对于每组测试数据,先输出能否到达,能的话输出YES,然后换行输出最短时间,如果不能的话直接输出NO

示例1

输入

3
2 3
.X
S.
2 3
.X
SC
2 4
.X
S.

输出

NO
YES
3
YES
4

题解

方法一

知识点:BFS,优先队列。

因为车的花费和走路花费不一致,会导致时间线混乱,因此可以考虑用优先队列维护时间线扩展,使得每次都是扩展最短时间的点,维持有序性。

同时,因为车的状态是持续的,非一次性的,因此状态需要多一维记录是否有车。

细节上,因为车的状态带来的花费影响是下一步才开始,不像传送门是即刻的,因此可以在扩展后直接锁点。

时间复杂度 \(O(?)\)

空间复杂度 \(O(nm)\)

方法二

知识点:BFS。

高维状态和方法一一样,但不使用优先队列维护时间线有序,因此锁点操作通过步数实现。如果当前扩展的步数小于扩展到点的历史步数,那就用本次步数覆盖这个状态的步数。注意要遍历完全才能得出答案。

时间复杂度 \(O(?)\)

空间复杂度 \(O(nm)\)

代码

方法一

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int n, t;
char dt[107][107];
bool vis[107][107][2];
const int dir[4][2] = { {1,0},{-1,0},{0,1}, { 0,-1 } };

struct node {
    int x;
    int y;
    bool c;
    int step;
    friend bool operator<(const node &a, const node &b) {
        return a.step > b.step;
    }
};

int bfs(node st) {
    priority_queue<node> pq;
    pq.push(st);
    vis[st.x][st.y][st.c] = 1;
    while (!pq.empty()) {
        node cur = pq.top();
        pq.pop();
        if (dt[cur.x][cur.y] == 'X') return cur.step;
        for (int i = 0;i < 4;i++) {
            int xx = cur.x + dir[i][0];
            int yy = cur.y + dir[i][1];
            bool cc = cur.c || dt[xx][yy] == 'C';
            int sstep = cur.step + (cur.c ? 1 : 2);///和之前一次有关
            if (xx < 0 || xx >= n || yy < 0 || yy >= n || vis[xx][yy][cc] || dt[xx][yy] == 'O' || sstep > t)continue;
            vis[xx][yy][cc] = 1;///因为不是在这次就改变时间线,所以依旧可以在扩展时直接锁定点
            pq.push({ xx,yy,cc,sstep });
        }
    }
    return 1e9 + 10;
}

bool solve() {
    cin >> n >> t;
    node st;
    for (int i = 0;i < n;i++) {
        for (int j = 0;j < n;j++) {
            cin >> dt[i][j];
            if (dt[i][j] == 'S') st.x = i, st.y = j, st.c = 0, st.step = 0;
            vis[i][j][0] = vis[i][j][1] = 0;
        }
    }
    int ans = bfs(st);
    if (ans > t) return false;
    else cout << "YES\n" << ans << '\n';
    return true;
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        if (!solve()) cout << "NO" << '\n';
    }
    return 0;
}

方法二

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int n, t;
char dt[107][107];
int step[107][107][2];
const int dir[4][2] = { {1,0},{-1,0},{0,1}, { 0,-1 } };

struct node {
    int x, y;
    bool c;
};

int bfs(node st, vector<node> &ed) {
    queue<node> q;
    q.push(st);
    step[st.x][st.y][0] = 0;
    while (!q.empty()) {
        node cur = q.front();
        q.pop();
        if (dt[cur.x][cur.y] == 'X') continue;
        for (int i = 0;i < 4;i++) {
            int xx = cur.x + dir[i][0];
            int yy = cur.y + dir[i][1];
            int delta = cur.c ? 1 : 2;
            if (xx < 0 || xx >= n || yy < 0 || yy >= n || dt[xx][yy] == 'O')continue;
            bool cc = cur.c || dt[xx][yy] == 'C';
            if (step[xx][yy][cc] <= step[cur.x][cur.y][cur.c] + delta) continue;
            ///不能用vis锁定点,因为扩展时间线混乱,不按照时间顺序扩展
            ///有可能时间晚的车先占了格子,早的车没扩展过来就无法覆盖了,因此只能用距离覆盖
            step[xx][yy][cc] = step[cur.x][cur.y][cur.c] + delta;
            q.push({ xx,yy,cc });
        }
    }
    int ans = 1e9;
    for (auto e : ed) ans = min({ ans, step[e.x][e.y][0],step[e.x][e.y][1] });///目的地是个连通块
    return ans;
}

bool solve() {
    cin >> n >> t;
    node st;
    vector<node> ed;
    for (int i = 0;i < n;i++) {
        for (int j = 0;j < n;j++) {
            cin >> dt[i][j];
            if (dt[i][j] == 'S') st.x = i, st.y = j, st.c = 0;
            if (dt[i][j] == 'X') ed.push_back({ i,j,0 });
            step[i][j][0] = step[i][j][1] = 1e9;
        }
    }
    int ans = bfs(st, ed);
    if (ans > t) return false;
    else cout << "YES\n" << ans << '\n';
    return true;
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        if (!solve()) cout << "NO" << '\n';
    }
    return 0;
}

标签:cur,int,st,yy,xx,NC15445,dt,wyh
来源: https://www.cnblogs.com/BlankYang/p/16484981.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有