鱼C论坛

 找回密码
 立即注册
查看: 1393|回复: 1

判断迷宫中两点是否连通,我老是过不了,大佬救命

[复制链接]
发表于 2022-6-19 18:32:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

题目网址  迷宫
我的代码
#include<cstdio>
#define  MAX 100
#include<iostream>
using namespace std;

int map[MAX][MAX]{};                        //迷宫与起点连通部分 
char maze[MAX][MAX]{};                        //存放迷宫 
bool visit[MAX][MAX]{};                        //记录已经访问的结点 
int xstep[4] = {0,1,0,-1};                //从一个点的四个方向 
int ystep[4] = {1,0,-1,0};
int num;                                                //迷宫边长 

void init(int n);                                                //初始化 
bool find_map(int x, int y, int num);        //返回map[x][y] 
void in_maze(int n);                                        //输入迷宫,从键盘 
int validate(int x, int y, int num);        //(x,y)没越界则返回true 
void our_map(int num);                                        //输出
void search(int x,int y);                                //递归深度优先搜索 

int main() {
        int times;
        scanf("%d", ×);
        for(int t = 0; t < times; t++) {
                
        int start[2]{}, end[2]{};
        scanf("%d", &num);
        in_maze(num);
        scanf("%d%d%d%d", &start[0], &start[1], &end[0], &end[1]);
        
        map[start[0]][start[1]] = 1;
        search(start[0], start[1]);
        
        if(find_map(end[0], end[1], num)) 
                printf("YES\n");
        else 
                printf("NO\n");
//        our_map(num);
        init(num);
        
        }
        return 0;
}
//---------------------------------
bool find_map(int x, int y, int n) {
        return map[x][y];
}
//---------------------------------
void in_maze(int n) {
        for(int i = 0; i < n; ++i) {
                for(int j = 0; j < n; ++j) {
                        scanf(" %c", &maze[i][j]);
                }
        }
}
//---------------------------------
int validate(int x, int y, int N){
    if(x >= 0 && x <= N - 1 && y >= 0 && y <= N - 1){
        return 1;
    }
    return 0;
}
//---------------------------------
void our_map(int num) {
        cout << endl << "map:" << endl;
        for(int i = 0; i < num; ++i) {
                for(int j = 0; j < num; ++j) {
                        cout << map[i][j] << " ";
                }
                cout << endl;
        }
}
//---------------------------------
void init(int n) {
        for(int i = 0; i < n; ++i) {
                for(int j = 0; j < n; ++j) {
                        maze[i][j] = 0;
                        map[i][j] = 0;
                        visit[i][j] = 0;
                }
        }
}
//---------------------------------
void search(int x,int y){
        if(visit[x][y] == 1)
                return;
        visit[x][y] = 1;
        for(int k = 0; k < 4; k++) {
                int x1 = x + xstep[k];
               int y1 = y + ystep[k];
        if(validate(x1, y1, num) && maze[x][y] == '.') {
                if(find_map(x1, y1, num)) {
                        map[x][y] = 1;
                        }
                }
        }
    for (int i=0;i<4;i++){
        int x1=x+xstep[i];
        int y1=y+ystep[i];
        if(validate(x1,y1,num) && maze[x1][y1] == '.'){
            search(x1, y1);
        }
    }
}
题目网站就一直10分只给我5分,怎么办呀,这是为什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-6-20 11:12:08 From FishC Mobile | 显示全部楼层
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int foo(vector <vector <char>> maze, pair <int, int> A, pair <int, int> B, int n) {
        auto &[r1, c1] = A;
        auto &[r2, c2] = B;
        if (r1 == r2 and c1 == c2) {
                return 1;
        }
        else if (r1 < 0 or r2 < 0 or c1 < 0 or c2 < 0 or r1 >= n or r2 >= n or c1 >= n or c2 >= n) {
                return 0;
        }
        else if (maze[r1][c1] != '.' or maze[r2][c2] != '.') return 0;
        vector <vector <char>> res = maze;
        res[r1][c1] = '#';
        return
                foo(res, make_pair(r1 + 1, c1), B, n) +
                foo(res, make_pair(r1 - 1, c1), B, n) +
                foo(res, make_pair(r1, c1 + 1), B, n) +
                foo(res, make_pair(r1, c1 - 1), B, n);
}

int main(void) {
        int n, k;
        vector <string> answer;
        cin >> k;
        for (int i = 0; i < k; ++i) {
                cin >> n;
                vector <vector <char>> maze (n, vector <char>(n, '#'));
                for (int r = 0; r < n; ++r) {
                        for (int c = 0; c < n; ++c) {
                                cin >> maze[r][c];
                        }
                }
                int x, y;
                pair <int, int> A, B;
                cin >> x >> y;
                A = make_pair(x, y);
                cin >> x >> y;
                B = make_pair(x, y);
        
                if (foo(maze, A, B, n)) {
                        answer.push_back("YES");
                }
                else {
                        answer.push_back("NO");
                }
        }
        
        for (const string& str: answer) {
                cout << str << endl;
        }
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 02:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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