Baby Ming and Matrix games(BC)

本文介绍了一个基于深度优先搜索(DFS)的算法实现,该算法应用于一个特定的问题场景中,通过递归方式遍历所有可能的路径组合来寻找符合特定条件的解。文章详细展示了如何使用C++实现DFS,并通过具体的代码示例解释了其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

dfs,比较简单

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
const int INF = ~0U >> 1;
const int maxn = 50;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
char str[maxn][maxn];
int T, n, m, sum;
int vis[maxn][maxn];

bool ok(int nx, int ny) {
    if(nx >= 1 && nx <= n && ny >= 1 && ny <= m) return true;
    return false;
}

bool dfs(int x, int y, double value) {
    if(value == sum) {
        return true;
    }
    for(int i = 0; i < 4; ++i) {
        int nx = dx[i] + x, ny = dy[i] + y;
        if(ok(nx, ny)) {
            if(str[nx][ny] == '+') {
                for(int j = 0; j < 4; ++j) {
                    int X = dx[i] + nx, Y = dy[i] + ny;
                    if(ok(X, Y) && str[X][Y] >= '0' && str[X][y] <= '9' && vis[X][Y] == 0) {
                        vis[X][Y] = 1;
                        if(dfs(X, Y, value + (str[X][Y] - '0'))) return true;
                        vis[X][Y] = 0;
                    }
                }
            }

            if(str[nx][ny] == '-') {
                for(int j = 0; j < 4; ++j) {
                    int X = dx[i] + nx, Y = dy[i] + ny;
                    if(ok(X, Y) && str[X][Y] >= '0' && str[X][y] <= '9' && vis[X][Y] == 0) {
                        vis[X][Y] = 1;
                        if(dfs(X, Y, value - (str[X][Y] - '0'))) return true;
                        vis[X][Y] = 0;
                    }
                }
            }

            if(str[nx][ny] == '*') {
                for(int j = 0; j < 4; ++j) {
                    int X = dx[i] + nx, Y = dy[i] + ny;
                    if(ok(X, Y) && str[X][Y] >= '0' && str[X][y] <= '9' && vis[X][Y] == 0) {
                        vis[X][Y] = 1;
                        if(dfs(X, Y, value * (str[X][Y] - '0'))) return true;
                        vis[X][Y] = 0;
                    }
                }
            }

            if(str[nx][ny] == '/') {
                for(int j = 0; j < 4; ++j) {
                    int X = dx[i] + nx, Y = dy[i] + ny;
                    if(ok(X, Y) && str[X][Y] >= '0' && str[X][y] <= '9' && vis[X][Y] == 0) {
                        vis[X][Y] = 1;
                        if(dfs(X, Y, value / (str[X][Y] - '0'))) return true;
                        vis[X][Y] = 0;
                    }
                }
            }
        }
    }
    return false;
}

int main() {
    scanf("%d", &T);
    while(T--) {
        memset(vis, 0, sizeof(vis));
        scanf("%d%d%d", &n, &m, &sum);
        for(int i = 1; i <= n; ++i)
            scanf("%s", str[i] + 1);
        bool is = false;
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= m; ++j) {
                if(str[i][j] >= '0' && str[i][j] <= '9') {
                    vis[i][j] = 1;
                    if(dfs(i, j, str[i][j] - '0')) {
                        is = true;
                        goto leap;
                    }
                    vis[i][j] = 0;
                }
            }
        }
        leap:
        if(is) printf("Possible\n");
        else printf("Impossible\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值