双端队列广搜

分层图 01/BFS
https://ac.nowcoder.com/acm/contest/45670/D
#include<bits/stdc++.h>
using namespace std;

const int N = 2009;
int n,m,k;
int sx,sy,ex,ey;
int a[N][N];
int dist[N][N][2];
const int dx[] = {1,-1,0,0},dy[] = {0,0,1,-1};
void bfs() {
    memset(dist,0x3f,sizeof dist);
    queue<tuple<int,int,int>> qu;
    dist[sx][sy][0] = 0;
    qu.push({sx,sy,0});
    while(!qu.empty()) {
        auto [x,y,z] = qu.front();
        qu.pop();
        if(z == 0) {
            if(a[x][y] > k) {
                dist[x][y][1] = dist[x][y][0];
                z = 1;
            }
        }
        for(int i=0; i<4; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && a[xx][yy] != -1) {
                if(dist[xx][yy][z] > dist[x][y][z] + 1) {
                    dist[xx][yy][z] = dist[x][y][z] + 1;
                    qu.push({xx,yy,z});
                }
            }
        }
    } 
    if(dist[ex][ey][1] == 0x3f3f3f3f) puts("-1");
    else cout<<dist[ex][ey][1]<<"\n";
}

int main() {
    cin >> n >> m >> k;
    cin >> sx >> sy >> ex >> ey;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) scanf("%d",&a[i][j]);
    }
    bfs();
    return 0;
}
https://ac.nowcoder.com/acm/contest/38711/G
双端队列广搜!
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
inline int read()
{
    int x = 0;
    char c =getchar();
    while(!isdigit(c)) c = getchar();
    while(isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48),c = getchar();
    return x;
}

int n,m;
const int N = 2e3 + 9;
char s[N][N];
int dist[N][N];
const int dx[] = {1,-1,0,0},dy[] = {0,0,1,-1};
void solve1()
{
    n = read(),m = read();
    for(int i=1; i<=n; i++) scanf("%s",s[i] + 1);
    deque<PII> qu;
    qu.push_front({1,1});
    memset(dist,0x3f,sizeof dist);
    dist[1][1] = 0;
    while(!qu.empty())
    {
        auto t = qu.front();
        qu.pop_front();
        for(int i=0; i<4; i++)
        {
            int xx = t.first + dx[i];
            int yy = t.second + dy[i];
            if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && s[xx][yy] != '#')
            {
                int d = (s[xx][yy] == '*' ? 0 : 1);
                if(dist[xx][yy] > dist[t.first][t.second] + d)
                {
                    dist[xx][yy] = dist[t.first][t.second] + d;
                    if(d == 0) qu.push_front({xx,yy});
                    else qu.push_back({xx,yy});
                }
            }
        }
    }
    int ans = (dist[n][m] == 0x3f3f3f3f) ? -1 : dist[n][m];
    cout<<ans<<"\n";
}

int main()
{
//    int _ = read();
//    while(_--)
        solve1();
    return 0;
}

全部评论

相关推荐

已经入职字节快一个月了,突然想起来之前那段时间的面经没有发,先发一下timeline吧。Tiktok&nbsp;内容安全平台(人才库电话捞我):电话10.28&nbsp;-&gt;&nbsp;一面10.30(我觉得你跟我们组业务挺match的,然后过了三天问hr挂了,应该是别人流程更快)阿里淘天:投递11.11-&gt;约面11.12-&gt;一面11.14(问得很简单,30分钟,手撕八股全过无后续)Kpi面腾讯wxg&nbsp;微信小程序:投递11.13&nbsp;-&gt;约面11.14-&gt;&nbsp;一面11.17&nbsp;(究极无敌拷打,问我多模态大模型涉及的算法?但是人很好)-&gt;11.19流程终止小红书&nbsp;风控平台:投递11.16&nbsp;—约面11.17&nbsp;&nbsp;-&gt;一面11.18&nbsp;(抽象的面试官,面试感觉一般,问得前端网页安全相关的,确实没准备)-&gt;11.20挂百度&nbsp;百家号:投递11.14&nbsp;—&gt;约面11.14&nbsp;-&gt;一面11.14(当场约2面)-&gt;二面11.24-&gt;口头告知offer,&nbsp;拒绝(原因是业务不太好)美团&nbsp;技术平台投递11.17&nbsp;-&gt;&nbsp;约面(忘记了,没多久)&nbsp;-&gt;一面11.19&nbsp;-&gt;二面11.21&nbsp;(字节offer不想面了)快手&nbsp;电商业务投递11.17&nbsp;-&gt;&nbsp;约面11.18&nbsp;-&gt;一面11.19&nbsp;-&gt;&nbsp;二面11.21(拒了)腾讯wxg&nbsp;微信支付(被捞):(直接发面试邮件)技术一面12.05&nbsp;-&gt;技术二面12.11&nbsp;-&gt;技术三面12.17&nbsp;-&gt;&nbsp;hr面已拒绝(了解业务后拒绝,但是有转正hc,感觉还蛮可惜)字节跳动&nbsp;xxxx:东家就不放具体的时间线了,大概是面完第二天就能知道结果,除了有几天ld请假了没填面评。不去wxg还有个原因是还在期末周,深圳学校来回太麻烦了,至少现在在的组感觉能学到很多的东西,自己的选择应该也没错。还是感概一下,一年前大二的时候投简历海投基本上石沉大海,无论大小厂约面比例很少。现在基本上投了就有面试,还都是以前梦寐以求的大厂,现在自己也有了更多的选择,也没有投太多简历。也感谢上一段实习的经历,很有意思的项目,无论是字节,腾讯,还是美团基本都有聊这个项目。面经需要等一下,也许等周末有空了再发给各位uu,感兴趣可以关注一下~有想要交流学习的同学也可以私信我,目前人在北京大钟寺~,可以找搭子~
正能量的牛可乐:这么多大厂面试下来,不仅摸清了不同公司的面试风格,还能精准避雷业务不匹配的岗位,血赚
实习简历求拷打
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务