蓝桥杯-迷宫



标题:迷宫

X星球的一处迷宫游乐场建在某个小山坡上。
它是由10x10相互连通的小房间组成的。

房间的地板上写着一个很大的字母。
我们假设玩家是面朝上坡的方向站立,则:
L表示走到左边的房间,
R表示走到右边的房间,
U表示走到上坡方向的房间,
D表示走到下坡方向的房间。

X星球的居民有点懒,不愿意费力思考。
他们更喜欢玩运气类的游戏。这个游戏也是如此!


开始的时候,直升机把100名玩家放入一个个小房间内。
玩家一定要按照地上的字母移动。


迷宫地图如下:
------------
UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR
------------


请你计算一下,最后,有多少玩家会走出迷宫? 
而不是在里边兜圈子。


请提交该整数,表示走出迷宫的玩家数目,不要填写任何多余的内容。

#include <iostream>
#include <string.h>
using namespace std;

int vis[20][20];
char mmap[20][20];
int countt = 0;

int find_(int i, int j){
    while(1){

        if(i < 0 || i > 9 || j < 0 || j > 9 ){
            countt++;
            break;
        }
        if(vis[i][j]) {
            break;
        }
        vis[i][j] = 1;//判断当前位置是否有人存在,,当移动之后, 这个位置还是有人说在,说明不能走出去
        switch(mmap[i][j]){
            case 'U': i--;break;
            case 'L': j--;break;
            case 'R': j++;break;
            case 'D': i++;break;
            default: break;
        }

    }

    return 0;
}


int main(){
    freopen("A.txt", "r", stdin);
    for(int i = 0; i <10; i++){
        for(int j = 0; j < 10; j++){
            cin >> mmap[i][j];
      }
    }

    for(int i = 0; i < 10; i++){
        for(int j = 0; j < 10; j++){
            memset(vis, 0, sizeof(vis));
            find_(i, j);
        }
    }

    cout << countt << endl;
}

广度优先搜索实现--参考

#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define fi first
#define se second
#define pr make_pair
typedef pair<int, int> pii;
const int MAX_N = 1e1 + 5; // 对题目中出现的数据范围,可以用这种方法表示,避免重复使用时出错
                           // XeY 表示X * 10 ^ Y 也就是X乘以10的Y次方
                           // +5 是为了防止数据溢出而额外开的空间
const int d[5][3] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const char able[5] = "ULDR"; // 确定常量,方便书写
bool exitable[MAX_N][MAX_N];
char mp[MAX_N][MAX_N];
queue<pii> que;
int main() {
    int ans = 0;
    for (int i = 0; i < 10; i++) {
        scanf("%s", mp[i]);
    }
    for (int j = 0; j < 10; j++) {
        if (!exitable[0][j] && mp[0][j] == 'U') {
            que.push(pr(0, j));
            exitable[0][j] = true;
        }
        if (!exitable[9][j] && mp[9][j] == 'D') {
            que.push(pr(9, j));
            exitable[9][j] = true;
        }
        if (!exitable[j][0] && mp[j][0] == 'L') {
            que.push(pr(j, 0));
            exitable[j][0] = true;
        }
        if (!exitable[j][9] && mp[j][9] == 'R') {
            que.push(pr(j, 9));
            exitable[j][9] = true;
        }
    }
    // 下面的这种写法被称之为广度优先搜索
    while (!que.empty()) {
        ans++;
        int x = que.front().fi, y = que.front().se;
        que.pop();
        for (int i = 0; i < 4; i++) {
            int tx = x + d[i][0], ty = y + d[i][1];
            if (tx < 0 || tx > 9) continue;
            if (ty < 0 || ty > 9) continue;
            if (exitable[tx][ty]) continue;
            if (mp[tx][ty] == able[i]) {
                exitable[tx][ty] = true;
                que.push(pr(tx, ty));
            }
        }
    }
    printf("\nans = %d\n\n", ans);
    // 检验结果正确性
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            if (exitable[i][j]) printf("%c", mp[i][j]);
            else printf("*");
        }
        printf("\n");
    }
    return 0;
}












发布了37 篇原创文章 · 获赞 12 · 访问量 6532

猜你喜欢

转载自blog.csdn.net/weixin_38960774/article/details/79408339