C++闯关游戏

以下是一个简单的C++闯关游戏示例,玩家需要在地图上找到宝藏并安全返回起点。游戏中有多个障碍和宝藏,玩家需要避开障碍并收集宝藏,最后返回起点即可通关。

#include <iostream>  
#include <vector>  
#include <algorithm>  
using namespace std;  
struct Point {  
    int x, y;  
};  
bool cmp(const Point &a, const Point &b) {  
    return a.x < b.x || (a.x == b.x && a.y < b.y);  
}  
int main() {  
    const int N = 10; // 地图大小为 10x10 
    const int M = 10; // 障碍数量为 10  
    const int T = 1; // 宝藏数量为 1  
    vector<Point> treasure(T); // 宝藏位置  
    vector<Point> obstacle(M); // 障碍位置  
    for (int i = 0; i < M; i++) {  
        obstacle[i].x = rand() % N;  
        obstacle[i].y = rand() % N;  
    }  
    for (int i = 0; i < T; i++) {  
        treasure[i].x = rand() % N;  
        treasure[i].y = rand() % N;  
    }  
    sort(obstacle.begin(), obstacle.end(), cmp); // 按 x 坐标升序排序障碍位置  
    sort(treasure.begin(), treasure.end(), cmp); // 按 x 坐标升序排序宝藏位置  
    Point start = {0, 0}; // 起点位置为 (0, 0)  
    bool can_pass[N][N] = {false}; // 记录每个位置是否可以通过  
    for (int i = 0; i < M; i++) {  
        can_pass[obstacle[i].x][obstacle[i].y] = true; // 设置障碍位置为不可通过  
    }  
    vector<Point> path; // 记录路径的向量  
    bool visited[N][N] = {false}; // 记录每个位置是否被访问过  
    if (dfs(start, can_pass, visited, path)) { // 使用深度优先搜索算法搜索路径  
        cout << "找到一条路径:" << endl;  
        for (int i = 0; i < path.size(); i++) {  
            cout << "(" << path[i].x << ", " << path[i].y << ") "; // 输出路径上的点坐标  
        }  
        cout << endl;  
    } else {  
        cout << "无法找到路径。" << endl;  
    }  
    return 0;  
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值