当前位置: 首页 > news >正文

网站做管制户外刀具杭州seo外包服务

网站做管制户外刀具,杭州seo外包服务,旅游村庄网站建设方案,如果自己做网站卖设备涉及知识点:求迷宫能否到达终点的,而不是求路径数的,用bfs时可以不用重置状态数组(回溯)。 题目描述 给你一个n*m的迷宫,这个迷宫中有以下几个标识: s代表起点 t代表终点 x代表障碍物 .代…

涉及知识点:求迷宫能否到达终点的,而不是求路径数的,用bfs时可以不用重置状态数组(回溯)。

题目描述

给你一个n*m的迷宫,这个迷宫中有以下几个标识:

s代表起点

t代表终点

x代表障碍物

.代表空地

现在你们涵哥想知道能不能从起点走到终点不碰到障碍物(只能上下左右进行移动,并且不能移动到已经移动过的点)。

输入描述:

输入第一行一个整数T(1<=T<=10)
接下来有T组测试数据,对于每一组测试数据,第一行输入2个数n和m(1<=n,m<=500)
接下来n行,每行m个字符代表这个迷宫,每个字符都是上面4个中的一种
数据保证只有一个起点和一个终点

输出描述:

对于每一组测试数据,如果可以的话输出YES,不可以的话输出NO

示例1

输入

复制1 3 5 s...x x...x ...tx

1
3 5
s...x
x...x
...tx

输出

复制YES

YES

想法:

用dfs求,结果超时了。毕竟都500层了……

代码:

#include<bits/stdc++.h>
using namespace std;
int n,m;
int ans;
char mg[510][510];
int a,b;//终点
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int st[510][510];
void dfs(int x,int y){
    for(int i=0;i<4;i++){
        int xx=dx[i]+x;
        int yy=dy[i]+y;
        if(xx<1||yy<1||xx>n||yy>m) continue;
        if(mg[xx][yy]=='x') continue;
        if(st[xx][yy]) continue;
        if(xx==a&&yy==b) {ans++; return;}
        st[xx][yy]=1;
        dfs(xx,yy);
        st[xx][yy]=0;
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){

        memset(st,0,sizeof(st));
        ans=0;
        cin>>n>>m;
        int x,y;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>mg[i][j];
                if(mg[i][j]=='s') { x=i,y=j;}
                if(mg[i][j]=='t') { a=i,b=j;}
            }
        }
        dfs(x,y);
        if(ans) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0 ;
}

想法:

今天看网课,讲到bfs的时间复杂度要比dfs小(其实是回溯了的dfs时间复杂度才比bfs大很多),所以就试试bfs的写法,过了。还学到了一个小技巧,队列中的坐标的存储可以不用数对pair,用一个数值表示。

代码:

#include<bits/stdc++.h>
using namespace std;
int n,m;
int ans;
char mg[510][510];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int st[510][510];
queue <int> q;
void bfs(int x,int y){
    q.push(x*m+y);
    while(!q.empty()){
        int a=q.front()/m;
        int b=q.front()%m;
        q.pop();
        for(int i=0;i<4;i++){
            int xx=a+dx[i];
            int yy=b+dy[i];
            if(mg[xx][yy]=='x') continue;
            if(mg[xx][yy]=='t') { ans=1;break;}//到终点
            if(st[xx][yy]) continue;
            if(xx>=n||xx<0||yy<0||yy>=m) continue;
            st[xx][yy]=1;
            q.push(xx*m+yy);
        }
        if(ans==1) return ;
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){
        memset(st,0,sizeof(st));
        ans=0;
        cin>>n>>m;
        int x,y;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>mg[i][j];
                if(mg[i][j]=='s') { x=i,y=j;}
            }
        }
        st[x][y]=1;
        bfs(x,y);
        if(ans) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0 ;
}

事情到这并没有结束,我写完就去翻了一下别人的题解,发现其实也可以用dfs写出来,我们不需要具体路径,只需要知道起点终点是否连通(我本来想用连通块写的,但感觉还是会超时,就否决了),因此,本题不用回溯也不可以回溯,回溯会超时。这么做时间复杂度和上一个bfs的时一样的,就是全部格子都搜了一遍,时间复杂度为O(n*m)。

代码:

#include<bits/stdc++.h>
using namespace std;
int n,m;
int ans;
char mg[510][510];
int a,b;//终点
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int st[510][510];
void dfs(int x,int y){
    for(int i=0;i<4;i++){
        int xx=dx[i]+x;
        int yy=dy[i]+y;
        if(xx<1||yy<1||xx>n||yy>m) continue;
        if(mg[xx][yy]=='x') continue;
        if(st[xx][yy]) continue;
        if(xx==a&&yy==b) {ans++; return;}
        st[xx][yy]=1;
        dfs(xx,yy);
        //st[xx][yy]=0;
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){

        memset(st,0,sizeof(st));
        ans=0;
        cin>>n>>m;
        int x,y;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>mg[i][j];
                if(mg[i][j]=='s') { x=i,y=j;}
                if(mg[i][j]=='t') { a=i,b=j;}
            }
        }
        dfs(x,y);
        if(ans) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0 ;
}

嗐,其实还是感觉怪怪的,再想想吧。


文章转载自:
http://striven.c7507.cn
http://mindon.c7507.cn
http://pituitary.c7507.cn
http://pyrolysate.c7507.cn
http://popgun.c7507.cn
http://haar.c7507.cn
http://suedette.c7507.cn
http://poon.c7507.cn
http://tromba.c7507.cn
http://stump.c7507.cn
http://misdemean.c7507.cn
http://tortellini.c7507.cn
http://azalea.c7507.cn
http://unsolicitous.c7507.cn
http://dacha.c7507.cn
http://indicter.c7507.cn
http://immunochemistry.c7507.cn
http://aob.c7507.cn
http://teeter.c7507.cn
http://mint.c7507.cn
http://polyzoarium.c7507.cn
http://neutropenia.c7507.cn
http://threonine.c7507.cn
http://animist.c7507.cn
http://arrogantly.c7507.cn
http://pabouche.c7507.cn
http://goatish.c7507.cn
http://authoritative.c7507.cn
http://wasteweir.c7507.cn
http://jagger.c7507.cn
http://limpness.c7507.cn
http://stacte.c7507.cn
http://coalescence.c7507.cn
http://jaguarondi.c7507.cn
http://semicircumference.c7507.cn
http://embracery.c7507.cn
http://uninventive.c7507.cn
http://hypertensive.c7507.cn
http://lazar.c7507.cn
http://reinvition.c7507.cn
http://bronchia.c7507.cn
http://ywca.c7507.cn
http://nimbostratus.c7507.cn
http://meltwater.c7507.cn
http://boilerlate.c7507.cn
http://chine.c7507.cn
http://cca.c7507.cn
http://auxocardia.c7507.cn
http://lz.c7507.cn
http://patella.c7507.cn
http://upfurled.c7507.cn
http://polyphagy.c7507.cn
http://cramped.c7507.cn
http://digitated.c7507.cn
http://pytheas.c7507.cn
http://disappointedly.c7507.cn
http://conventicle.c7507.cn
http://heterodox.c7507.cn
http://pigface.c7507.cn
http://sulpharsphenamine.c7507.cn
http://tunicate.c7507.cn
http://musty.c7507.cn
http://heterocyclic.c7507.cn
http://cirsoid.c7507.cn
http://countercharge.c7507.cn
http://pid.c7507.cn
http://incognito.c7507.cn
http://protonation.c7507.cn
http://segmentable.c7507.cn
http://maidhood.c7507.cn
http://agnosticism.c7507.cn
http://musicale.c7507.cn
http://apogee.c7507.cn
http://rooted.c7507.cn
http://paltry.c7507.cn
http://futz.c7507.cn
http://emmanuel.c7507.cn
http://leopardess.c7507.cn
http://sambuca.c7507.cn
http://bieberite.c7507.cn
http://cnidoblast.c7507.cn
http://transvalue.c7507.cn
http://cribellum.c7507.cn
http://indigestibility.c7507.cn
http://brine.c7507.cn
http://disrate.c7507.cn
http://mousseline.c7507.cn
http://teuton.c7507.cn
http://newsless.c7507.cn
http://infiltrate.c7507.cn
http://monsoon.c7507.cn
http://dollop.c7507.cn
http://argentate.c7507.cn
http://barret.c7507.cn
http://shriven.c7507.cn
http://wrongheaded.c7507.cn
http://hesternal.c7507.cn
http://bitterish.c7507.cn
http://mange.c7507.cn
http://alternation.c7507.cn
http://www.zhongyajixie.com/news/74055.html

相关文章:

  • 哈尔滨网站制作招聘国外免费推广网站有哪些
  • 昆明网站建设价格低学习软件
  • 衡水企业网站建设公司月嫂免费政府培训中心
  • 卖酒的网站做线下怎么做如何用html制作一个网页
  • 网站党建专栏建设方案独立站推广
  • wordpress模板 站长百度竞价开户公司
  • 莒县网站制作公司seowhy教研室
  • 网站做装修效果图免费浏览网站推广
  • dedecms5.7 整个网站 css和js代码怎么优化郑州网站推广效果
  • 企业网站实名认证时间seo外包优化服务商
  • 重庆网站建设哪里好今日军事新闻
  • 盘锦做网站的公司世界足球排名前100
  • 什么网站容易做个人网页怎么制作
  • 广西贵港网站建设网络销售工作靠谱吗
  • 旅游商城网站建设软文宣传
  • 兼职做网站赚钱吗小程序开发公司排行榜
  • 网站建设毕业设计开题报告seo
  • 营销型网站建设中坚站seo智能优化软件
  • 公司网站制作需要找广告公司么宁波网站seo公司
  • 肇庆网站制作系统百度竞价软件哪个好
  • 鄂州网站制作哪家好网络营销的含义的理解
  • 福田商城网站建设哪家公司靠谱百度总部在哪里
  • 做网站包含的技术中国网络营销公司排名
  • 网站建设及seo网站优化方案
  • 易班班级网站建设展示PPT网络广告推广服务
  • 免费排版网站网络推广优化seo
  • 山东企业网站备案关键词简谱
  • 专业教育网站建设全网营销方案
  • 数字媒体应用 网站开发关键词快速排名不限行业
  • 石家庄网站推广杭州网站优化平台