找回密码
 中文实名注册
查看: 92|回复: 0

迷宫

[复制链接]

694

主题

1082

帖子

2万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
22801
发表于 2024-4-14 19:30:12 | 显示全部楼层 |阅读模式
[C++] 纯文本查看 复制代码
#include<iostream>
using namespace std;

char f[100][100];
bool vis[100][100];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n, m;

// 在 (x,y) 这个位置,走到重点的方案数
int dfs(int x, int y) {
    if (f[x][y] == 'e') {
        return 1;
    }

    int cnt = 0;
    for(int i = 0; i < 4; i++) {
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tx][ty] && f[tx][ty] != '#') {
            vis[tx][ty] = 1;
            cnt += dfs(tx, ty);
            vis[tx][ty] = 0;
        }
    }
    return cnt;
}
int main() {
    int x, y;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> f[i];
        for (int j = 0; j < m; j++) {
            if (f[i][j] == 's') {
                x = i;
                y = j;
            }
        }
    }
    vis[x][y] = 1;
    cout << dfs(x, y) << endl;
    return 0;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

小黑屋|东台市机器人学会 ( 苏ICP备2021035350号-1;苏ICP备2021035350号-2;苏ICP备2021035350号-3 )

GMT+8, 2024-4-30 03:58 , Processed in 0.041098 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表