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

BFS最短路

1.飞跃原野

#include <bits/stdc++.h>
using namespace std;
int n,m,ox,oy,D,k;
char a[105][105];
queue<pair<int,pair<int,int> > > q;
int ans[105][105],now=INT_MAX;
vector<int> out;
int dx[505]={1,0,0,-1};
int dy[505]={0,-1,1,0};
int p[505];
/*
这道题目方向数组有多种可能,飞行不一定要飞满D格再降落。
*/
/* TODO (K101#1#): 修改方向数组 */
void bfs(int bx,int by)
{q.push({bx,{by,D}});//ans[bx][by]=1;int x,y,s,ld;while(!q.empty()){x=q.front().first;y=q.front().second.first;s=q.front().second.second;q.pop();for(int i=1; i<=n; i++){for(int j=1; j<=m; j++) cout<<setw(2)<<ans[i][j]<<" ";cout<<'\n';}for(int i=1; i<=n; i++){for(int j=1; j<=m; j++) cout<<setw(2)<<a[i][j]<<" ";cout<<'\n';}system("pause");if(x==n&&y==m){//now=min(now,ans[x][y]);cout<<ans[x][y];exit(0);}for(int i=0; i<k; i++){int lx=x+dx[i];int ly=y+dy[i];int ld;if(i>3) ld=s-(abs(dx[p[i]]-lx)+abs(dy[p[i]]-ly))+1;else ld=s;if(lx<1||lx>n||ly<1||ly>m||a[lx][ly]=='#'||ans[lx][ly]!=0||ld<0) continue;q.push({lx,{ly,ld}});ans[lx][ly]=ans[x][y]+1;}	}cout<<"impossible";exit(0);
}
int main()
{
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);cin>>n>>m>>D;for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){char x;cin>>x;if(x=='L') a[i][j]='#';else a[i][j]='.';}}k=4;for(int i=1; i<=D; i++){for(int j=0; j<4; j++){if(dx[j]==0) dx[k]=0;else if(dx[j]<0) dx[k]=dx[j]-i;else dx[k]=dx[j]+i;if(dy[j]==0) dy[k]=0;else if(dy[j]<0) dy[k]=dy[j]-i;else dy[k]=dy[j]+i;p[k]=j;k++;}}bfs(1,1);if(now==INT_MAX) cout<<"impossible";else cout<<now;return 0;
}

#include <bits/stdc++.h>
using namespace std;
int n,m,ox,oy,D,k;
char a[105][105];
queue<pair<int,pair<int,int> > > q;
int ans[105][105],now=INT_MAX;
vector<int> out;
int dx[505]={1,0,0,-1};
int dy[505]={0,-1,1,0};
int p[505];
/*
这道题目方向数组有多种可能,飞行不一定要飞满D格再降落。
*/
/* TODO (K101#1#): 修改方向数组 */
void bfs(int bx,int by)
{
    q.push({bx,{by,D}});
    //ans[bx][by]=1;
    int x,y,s,ld;
    while(!q.empty())
    {
        x=q.front().first;
        y=q.front().second.first;
        s=q.front().second.second;
        q.pop();
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++) cout<<setw(2)<<ans[i][j]<<" ";
            cout<<'\n';
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++) cout<<setw(2)<<a[i][j]<<" ";
            cout<<'\n';
        }
        system("pause");
        if(x==n&&y==m)
        {
            //now=min(now,ans[x][y]);
            cout<<ans[x][y];
            exit(0);
        }
        for(int i=0; i<k; i++)
        {
            int lx=x+dx[i];
            int ly=y+dy[i];
            int ld;
            if(i>3) ld=s-(abs(dx[p[i]]-lx)+abs(dy[p[i]]-ly))+1;
            else ld=s;
            if(lx<1||lx>n||ly<1||ly>m||a[lx][ly]=='#'||ans[lx][ly]!=0||ld<0) continue;
            q.push({lx,{ly,ld}});
            ans[lx][ly]=ans[x][y]+1;
        }    
    }
    cout<<"impossible";
    exit(0);
}
int main()
{
//    ios::sync_with_stdio(0);
//    cin.tie(0);cout.tie(0);
    cin>>n>>m>>D;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            char x;
            cin>>x;
            if(x=='L') a[i][j]='#';
            else a[i][j]='.';
        }
    }
    k=4;
    for(int i=1; i<=D; i++)
    {
        for(int j=0; j<4; j++)
        {
            if(dx[j]==0) dx[k]=0;
            else if(dx[j]<0) dx[k]=dx[j]-i;
            else dx[k]=dx[j]+i;
            if(dy[j]==0) dy[k]=0;
            else if(dy[j]<0) dy[k]=dy[j]-i;
            else dy[k]=dy[j]+i;
            p[k]=j;
            k++;
        }
    }
    bfs(1,1);
    if(now==INT_MAX) cout<<"impossible";
    else cout<<now;
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
int n,m,ox,oy;
char a[505][505];
queue<pair<int,int> > q;
queue<string> ans2;
int ans[505][505];
int dx[5]={1,0,0,-1};
int dy[5]={0,-1,1,0};
string s="DLRU";
string now="";
int bfs(int bx,int by)
{q.push({bx,by});ans2.push({""});int x,y;string road="";while(!q.empty()){for(int i=1; i<=n; i++){for(int j=1; j<=m; j++) cout<<setw(2)<<ans[i][j]<<" ";cout<<'\n';}for(int i=1; i<=n; i++){for(int j=1; j<=m; j++) cout<<setw(2)<<a[i][j]<<" ";cout<<'\n';}cout<<ans2.front()<<'\n';system("pause");x=q.front().first;y=q.front().second;road=ans2.front();q.pop();ans2.pop();if(x==n&&y==m){/*for(int i=1+500; i<=10+500; i++){for(int j=1+500; j<=10+500; j++) cout<<setw(2)<<ans[i][j]<<" ";cout<<'\n';}*/cout<<ans[x][y]<<'\n'<<road;exit(0);}for(int i=0; i<4; i++){cout<<i<<"启动>>";int lx=x+dx[i];int ly=y+dy[i];if(lx<1||lx>n||ly<1||ly>m||a[lx][ly]=='#'||ans[lx][ly]!=0){cout<<">>] "<<i<<"正常"<<'\n';continue;}q.push({lx,ly});ans2.push(road+s[i]);ans[lx][ly]=ans[x][y]+1;cout<<">>] "<<i<<"正常"<<'\n';}}return -1;
}
int main()
{//ios::sync_with_stdio(0);//cin.tie(0);cout.tie(0);cin>>n>>m;for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){int x;cin>>x;if(x==1) a[i][j]='#';else a[i][j]='.';}}cout<<bfs(1,1)<<'\n'<<ans2.front();return 0;
}

#include <bits/stdc++.h>
using namespace std;
int n,m,ox,oy;
char a[505][505];
queue<pair<int,int> > q;
queue<string> ans2;
int ans[505][505];
int dx[5]={1,0,0,-1};
int dy[5]={0,-1,1,0};
string s="DLRU";
string now="";
int bfs(int bx,int by)
{
    q.push({bx,by});
    ans2.push({""});
    int x,y;
    string road="";
    while(!q.empty())
    {
        
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++) cout<<setw(2)<<ans[i][j]<<" ";
            cout<<'\n';
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++) cout<<setw(2)<<a[i][j]<<" ";
            cout<<'\n';
        }
        cout<<ans2.front()<<'\n';
        system("pause");
        x=q.front().first;
        y=q.front().second;
        road=ans2.front();
        q.pop();
        ans2.pop();
        if(x==n&&y==m)
        {
            /*
            for(int i=1+500; i<=10+500; i++)
            {
                for(int j=1+500; j<=10+500; j++) cout<<setw(2)<<ans[i][j]<<" ";
                cout<<'\n';
            }
            */
            cout<<ans[x][y]<<'\n'<<road;
            exit(0);
        }
        for(int i=0; i<4; i++)
        {
            cout<<i<<"启动>>";
            int lx=x+dx[i];
            int ly=y+dy[i];
            if(lx<1||lx>n||ly<1||ly>m||a[lx][ly]=='#'||ans[lx][ly]!=0)
            {
                cout<<">>] "<<i<<"正常"<<'\n';
                continue;
            }
            q.push({lx,ly});
            ans2.push(road+s[i]);
            ans[lx][ly]=ans[x][y]+1;
            cout<<">>] "<<i<<"正常"<<'\n';
        }
    }
    return -1;
}
int main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            int x;
            cin>>x;
            if(x==1) a[i][j]='#';
            else a[i][j]='.';
        }
    }
    cout<<bfs(1,1)<<'\n'<<ans2.front();
    return 0;
}
 

#include <bits/stdc++.h>
using namespace std;
int n,m;
int x1,y1,x2,y2;
int a[105][105];
queue<pair<int,pair<int,int> > > q;
queue<int>last;
int dx[5]={0,0,1,-1};
int dy[5]={1,-1,0,0};
int bfs(int bx,int by)
{q.push({bx,{by,0}});int x,y,z,f;while(!q.empty()){x=q.front().first;y=q.front().second.first;z=q.front().second.second;f=last.front();q.pop();if(x==x2&&y==y2) return z;for(int i=0; i<4; i++){int lx=x+dx[i];int ly=y+dy[i];if(lx<1||lx>n||ly<1||ly>m||a[lx][ly]==1) continue;a[lx][ly]=1;if(f!=i) z++;q.push({lx,{ly,z}});last.push(i);z--;}}return -1;
}
int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n>>m;for(int i=1; i<=n; i++){for(int j=1; j<=m; j++) cin>>a[i][j];}cin>>x1>>y1>>x2>>y2;cout<<bfs(x1,y1);return 0;
}

#include <bits/stdc++.h>
using namespace std;
int n,m;
int x1,y1,x2,y2;
int a[105][105];
queue<pair<int,pair<int,int> > > q;
queue<int>last;
int dx[5]={0,0,1,-1};
int dy[5]={1,-1,0,0};
int bfs(int bx,int by)
{
    q.push({bx,{by,0}});
    int x,y,z,f;
    while(!q.empty())
    {
        x=q.front().first;
        y=q.front().second.first;
        z=q.front().second.second;
        f=last.front();
        q.pop();
        if(x==x2&&y==y2) return z;
        for(int i=0; i<4; i++)
        {
            int lx=x+dx[i];
            int ly=y+dy[i];
            if(lx<1||lx>n||ly<1||ly>m||a[lx][ly]==1) continue;
            a[lx][ly]=1;
            if(f!=i) z++;
            q.push({lx,{ly,z}});
            last.push(i);
            z--;
        }
    }
    return -1;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++) cin>>a[i][j];
    }
    cin>>x1>>y1>>x2>>y2;
    cout<<bfs(x1,y1);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int n,m,ox,oy,D,k;
char a[105][105];
queue<pair<int,pair<int,int> > > q;
int ans[105][105],now=INT_MAX;
vector<int> out;
int dx[505]={1,0,0,-1};
int dy[505]={0,-1,1,0};
int p[505];
/*
这道题目方向数组有多种可能,飞行不一定要飞满D格再降落。
*/
/* TODO (K101#1#): 修改方向数组 */
void bfs(int bx,int by)
{q.push({bx,{by,D}});//ans[bx][by]=1;int x,y,s,ld;while(!q.empty()){x=q.front().first;y=q.front().second.first;s=q.front().second.second;q.pop();for(int i=1; i<=n; i++){for(int j=1; j<=m; j++) cout<<setw(2)<<ans[i][j]<<" ";cout<<'\n';}for(int i=1; i<=n; i++){for(int j=1; j<=m; j++) cout<<setw(2)<<a[i][j]<<" ";cout<<'\n';}system("pause");if(x==n&&y==m){//now=min(now,ans[x][y]);cout<<ans[x][y];exit(0);}for(int i=0; i<k; i++){int lx=x+dx[i];int ly=y+dy[i];int ld;if(i>3) ld=s-(abs(dx[p[i]]-lx)+abs(dy[p[i]]-ly))+1;else ld=s;if(lx<1||lx>n||ly<1||ly>m||a[lx][ly]=='#'||ans[lx][ly]!=0||ld<0) continue;q.push({lx,{ly,ld}});ans[lx][ly]=ans[x][y]+1;}	}cout<<"impossible";exit(0);
}
int main()
{
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);cin>>n>>m>>D;for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){char x;cin>>x;if(x=='L') a[i][j]='#';else a[i][j]='.';}}k=4;for(int i=1; i<=D; i++){for(int j=0; j<4; j++){if(dx[j]==0) dx[k]=0;else if(dx[j]<0) dx[k]=dx[j]-i;else dx[k]=dx[j]+i;if(dy[j]==0) dy[k]=0;else if(dy[j]<0) dy[k]=dy[j]-i;else dy[k]=dy[j]+i;p[k]=j;k++;}}bfs(1,1);if(now==INT_MAX) cout<<"impossible";else cout<<now;return 0;
}

#include <bits/stdc++.h>
using namespace std;
int n,m,ox,oy,D,k;
char a[105][105];
queue<pair<int,pair<int,int> > > q;
int ans[105][105],now=INT_MAX;
vector<int> out;
int dx[505]={1,0,0,-1};
int dy[505]={0,-1,1,0};
int p[505];
/*
这道题目方向数组有多种可能,飞行不一定要飞满D格再降落。
*/
/* TODO (K101#1#): 修改方向数组 */
void bfs(int bx,int by)
{
    q.push({bx,{by,D}});
    //ans[bx][by]=1;
    int x,y,s,ld;
    while(!q.empty())
    {
        x=q.front().first;
        y=q.front().second.first;
        s=q.front().second.second;
        q.pop();
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++) cout<<setw(2)<<ans[i][j]<<" ";
            cout<<'\n';
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++) cout<<setw(2)<<a[i][j]<<" ";
            cout<<'\n';
        }
        system("pause");
        if(x==n&&y==m)
        {
            //now=min(now,ans[x][y]);
            cout<<ans[x][y];
            exit(0);
        }
        for(int i=0; i<k; i++)
        {
            int lx=x+dx[i];
            int ly=y+dy[i];
            int ld;
            if(i>3) ld=s-(abs(dx[p[i]]-lx)+abs(dy[p[i]]-ly))+1;
            else ld=s;
            if(lx<1||lx>n||ly<1||ly>m||a[lx][ly]=='#'||ans[lx][ly]!=0||ld<0) continue;
            q.push({lx,{ly,ld}});
            ans[lx][ly]=ans[x][y]+1;
        }    
    }
    cout<<"impossible";
    exit(0);
}
int main()
{
//    ios::sync_with_stdio(0);
//    cin.tie(0);cout.tie(0);
    cin>>n>>m>>D;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            char x;
            cin>>x;
            if(x=='L') a[i][j]='#';
            else a[i][j]='.';
        }
    }
    k=4;
    for(int i=1; i<=D; i++)
    {
        for(int j=0; j<4; j++)
        {
            if(dx[j]==0) dx[k]=0;
            else if(dx[j]<0) dx[k]=dx[j]-i;
            else dx[k]=dx[j]+i;
            if(dy[j]==0) dy[k]=0;
            else if(dy[j]<0) dy[k]=dy[j]-i;
            else dy[k]=dy[j]+i;
            p[k]=j;
            k++;
        }
    }
    bfs(1,1);
    if(now==INT_MAX) cout<<"impossible";
    else cout<<now;
    return 0;
}
AI

#include <bits/stdc++.h>
using namespace std;int n, m, D;
char a[105][105];
int ans[105][105];
int dx[4] = {1, 0, 0, -1};  // 下、左、右、上
int dy[4] = {0, -1, 1, 0};struct State {int x, y;int dist;int fly_used;
};void bfs(int bx, int by) {queue<State> q;q.push({bx, by, 0, 0});ans[bx][by] = 0;while (!q.empty()) {State current = q.front();q.pop();if (current.x == n && current.y == m) {cout << current.dist;exit(0);}// 普通移动(步长为1)for (int i = 0; i < 4; ++i) {int nx = current.x + dx[i];int ny = current.y + dy[i];if (nx < 1 || nx > n || ny < 1 || ny > m)continue;if (a[nx][ny] != '.')continue;if (ans[nx][ny] != -1 && ans[nx][ny] <= current.dist + 1)continue;ans[nx][ny] = current.dist + 1;q.push({nx, ny, current.dist + 1, 0});}// 飞行移动(步长为1到D)if (current.fly_used < D) {for (int i = 0; i < 4; ++i) {for (int step = 1; step <= D; ++step) {int nx = current.x + dx[i] * step;int ny = current.y + dy[i] * step;if (nx < 1 || nx > n || ny < 1 || ny > m)break;  // 超出边界,停止该方向的飞行if (a[nx][ny] != '.')break;  // 遇到湖泊,停止该方向的飞行if (ans[nx][ny] != -1 && ans[nx][ny] <= current.dist + 1)continue;ans[nx][ny] = current.dist + 1;q.push({nx, ny, current.dist + 1, current.fly_used + step});}}}}cout << "impossible";
}int main() {cin >> n >> m >> D;// 读取地图for (int i = 1; i <= n; ++i) {string row;cin >> row;for (int j = 1; j <= m; ++j) {a[i][j] = (row[j-1] == 'L') ? '#' : '.';}}// 初始化ans数组memset(ans, -1, sizeof(ans));bfs(1, 1);return 0;
}

#include <bits/stdc++.h>
using namespace std;

int n, m, D;
char a[105][105];
int ans[105][105];
int dx[4] = {1, 0, 0, -1};  // 下、左、右、上
int dy[4] = {0, -1, 1, 0};

struct State {
    int x, y;
    int dist;
    int fly_used;
};

void bfs(int bx, int by) {
    queue<State> q;
    q.push({bx, by, 0, 0});
    ans[bx][by] = 0;

    while (!q.empty()) {
        State current = q.front();
        q.pop();

        if (current.x == n && current.y == m) {
            cout << current.dist;
            exit(0);
        }

        // 普通移动(步长为1)
        for (int i = 0; i < 4; ++i) {
            int nx = current.x + dx[i];
            int ny = current.y + dy[i];

            if (nx < 1 || nx > n || ny < 1 || ny > m)
                continue;
            if (a[nx][ny] != '.')
                continue;
            if (ans[nx][ny] != -1 && ans[nx][ny] <= current.dist + 1)
                continue;

            ans[nx][ny] = current.dist + 1;
            q.push({nx, ny, current.dist + 1, 0});
        }

        // 飞行移动(步长为1到D)
        if (current.fly_used < D) {
            for (int i = 0; i < 4; ++i) {
                for (int step = 1; step <= D; ++step) {
                    int nx = current.x + dx[i] * step;
                    int ny = current.y + dy[i] * step;

                    if (nx < 1 || nx > n || ny < 1 || ny > m)
                        break;  // 超出边界,停止该方向的飞行
                    if (a[nx][ny] != '.')
                        break;  // 遇到湖泊,停止该方向的飞行
                    if (ans[nx][ny] != -1 && ans[nx][ny] <= current.dist + 1)
                        continue;

                    ans[nx][ny] = current.dist + 1;
                    q.push({nx, ny, current.dist + 1, current.fly_used + step});
                }
            }
        }
    }
    cout << "impossible";
}

int main() {
    cin >> n >> m >> D;

    // 读取地图
    for (int i = 1; i <= n; ++i) {
        string row;
        cin >> row;
        for (int j = 1; j <= m; ++j) {
            a[i][j] = (row[j-1] == 'L') ? '#' : '.';
        }
    }

    // 初始化ans数组
    memset(ans, -1, sizeof(ans));
    bfs(1, 1);

    return 0;
}

相关文章:

  • 深入分析OpenCV技术原理:计算机视觉的核心力量
  • 数字化浪潮下的工业变革:企业转型的战略机遇与挑战
  • Open WebUI 设置通过硅基流动访问 DeepSeek v3 教程​
  • 偶然发现Git文件夹非常大,使用BGF来处理Git历史Blob文件
  • AI Agent 孵化器?开源框架CAMEL
  • 驱动开发硬核特训 · Day 24(上篇):走进Linux内核时钟子系统 —— 硬件基础全解析
  • 【自然语言处理与大模型】LangChain大模型应用框架入门①
  • Electron Forge【实战】桌面应用 —— 将项目配置保存到本地
  • 考OCM证书前需要有OCP证书
  • VSCode Verilog环境搭建
  • JVM调优实战(JVM Tuning Pactice)
  • 深入解析 Linux 进程池:原理、实现与高并发优化
  • 【AI面试准备】模型自动化评估
  • 【数据结构与算法】哈希表实现:闭散列 开散列
  • Qt5与现代OpenGL学习(四)X轴方向旋转60度
  • DevExpressWinForms-XtraMessageBox-使用教程
  • Java信任证书
  • 前缀和 --- 二维前缀和
  • SVN子路径权限设置及登录方法详解
  • Prometheus使用Recoding Rules优化性能
  • 南部战区位南海海域进行例行巡航
  • 修订占比近30%收录25万条目,第三版《英汉大词典》来了
  • 事关稳就业稳经济,10张海报看懂这场发布会的政策信号
  • IPO周报|4月最后2只新股周一申购,今年以来最低价股来了
  • 大学2025丨专访北邮校长徐坤:工科教育要真正回归工程本质
  • 阿联酋启动第三届全球航空奖评选,奖金总额达百万美元