火龙信奥
  • 首页
  • 课程
  • 题库
  • 打卡
    • 代码对战
    • 快速对战
  • 题单
  • 团队
  • 荣誉墙
  • 商城
  • 登录 / 注册

0301上课写过的代码

作者: 作者的头像   zheng , 时间:2025-03-01 16:24:00 , 所有人可见, 阅读  55

迷宫

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

const int N = 10;
int a[N][N];
int ans =0 ;
int n,m,k;
pair<int,int>s , t;
int vis[N][N];
int walk[4][2] ={{0,1},{0,-1},{1,0},{-1,0}};
void dfs(int x,int y){
    if(x == t.first && y == t.second){
        ans ++;
        return ;
    } 
    int dx,dy;
    for(int i =0 ; i<4;i++){
        dx = x + walk[i][0];
        dy = y + walk[i][1];
        if(dx >=1 && dx <= n &&dy>=1 &&dy<=m && !a[dx][dy] && !vis[dx][dy]){
            vis[dx][dy] =1;
            dfs(dx,dy);
            vis[dx][dy] =0;
        }
    }

}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    cin >> n >> m >> k;
    cin >> s.first >> s.second >> t.first>>t.second;
    for(int i=1;i<=k;i++){
        int x,y;cin >> x>>  y;
        a[x][y] = 1;
    }
    vis[s.first][s.second] = 1; 
    dfs(s.first,s.second);
    cout<<ans;
    return 0;
}

马的遍历

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

const int N = 410;
int ans =0 ;
int n,m, sx,sy;
int vis[N][N];
int walk[8][2] ={{2,1},{2,-1},{1,2},{-1,2},{-2,1},{-2,-1},{1,-2},{-1,-2}};
void bfs(){
    memset(vis,-1,sizeof vis);
    queue<pair<int,int>>q;
    q.push({sx,sy});
    vis[sx][sy] = 0;
    while(!q.empty()){
        auto [x,y] = q.front();q.pop();
        for(int k = 0 ;k < 8 ;k++){
            int dx = x + walk[k][0];
            int dy = y + walk[k][1];
            if(dx >=1 && dx <= n &&dy>=1 &&dy<=m && vis[dx][dy] == -1){
                q.push({dx,dy});
                vis[dx][dy] = vis[x][y] + 1;
            }
        } 
    }

}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    cin >> n >> m >> sx >> sy;
    bfs();
    for(int i=1;i<=n;i++){
        for(int j = 1; j<= m ;j++){
            cout<<vis[i][j]<<" ";
        }cout<<"\n";
    }
    return 0;
}

DFS 全排列

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

const int N = 10;
int n,m,k;
int vis[N];
vector<int>tmp;
vector<vector<int>>ans;
void dfs(){
//  for(auto i:tmp){
//      cout<<i<<" ";
//  }cout<<endl;
    if(tmp.size() == n){
        ans.push_back(tmp);
        return ;
    }
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            tmp.push_back(i);
            vis[i] = 1;
            dfs();
            tmp.pop_back(); 
            vis[i] = 0;
        }
    }
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    cin >> n ;
    dfs();
    for(auto i:ans){
        for(auto v:i){
            cout<<v <<" ";
        }cout<<"\n";
    }
    return 0;
}

—— 本文来自火龙信奥(义乌睿码科技):义乌青少年信息学奥赛与编程教育平台,专注 CSP-J/S、NOIP、GESP 竞赛培训,线上线下融合教学,助力编程升学。网址:hlcoding.com

©2026加盟我们 | 关于我们 | ACM课程 | 常见问题 | 成果墙 | 评测记录 | 浙ICP备2021013995号
在线画图 | OI WIki | 打字练习
火龙信奥
请输入登录信息


请完成安全验证
验证码底图 滑块
向右拖动滑块完成验证
请输入用户名 / 绑定的手机号码



请输入注册信息(手机号验证码注册)





验证码5分钟有效,60秒内不可重复获取,每日最多3次

微信登录

微信登录二维码

正在生成二维码...

账号已过期,请续期。
去续期

绑定手机号

📱

为了更好地保护您的账号安全,享受完整的平台服务

请您尽快绑定手机号码