Blue Bridge Cup - Maze



Title: Labyrinth

Planet X is a maze built on a hillside playground.
It is a small room 10x10 interconnected thereof.

I read a lot of letters on the floor of the room.
We assume that a player is the direction facing uphill standing, then:
L represents the left side of the room went,
R represents went to the room on the right,
U represents the room went uphill direction,
D represents the room went downhill direction.

Residents X planet a little lazy, I do not want to think laborious.
They prefer to play games like luck. This game, too!


Initially, 100 players put the helicopter into a small room.
Players must move in alphabetical earth.


Maze map as follows:
------------
UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR
------------


Please you do the math, and finally, how many players will be out maze? 
rather than inside the circles.


Please submit the integer representing the number of players out of the maze, do not fill in any extra content.

#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;
}

Breadth-first search implementation - Reference

#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

Guess you like

Origin blog.csdn.net/weixin_38960774/article/details/79408339
Recommended