鱼C论坛

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

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

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

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

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

x

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

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

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

  17. int main() {
  18.         int times;
  19.         scanf("%d", &times);
  20.         for(int t = 0; t < times; t++) {
  21.                
  22.         int start[2]{}, end[2]{};
  23.         scanf("%d", &num);
  24.         in_maze(num);
  25.         scanf("%d%d%d%d", &start[0], &start[1], &end[0], &end[1]);
  26.        
  27.         map[start[0]][start[1]] = 1;
  28.         search(start[0], start[1]);
  29.        
  30.         if(find_map(end[0], end[1], num))
  31.                 printf("YES\n");
  32.         else
  33.                 printf("NO\n");
  34. //        our_map(num);
  35.         init(num);
  36.        
  37.         }
  38.         return 0;
  39. }
  40. //---------------------------------
  41. bool find_map(int x, int y, int n) {
  42.         return map[x][y];
  43. }
  44. //---------------------------------
  45. void in_maze(int n) {
  46.         for(int i = 0; i < n; ++i) {
  47.                 for(int j = 0; j < n; ++j) {
  48.                         scanf(" %c", &maze[i][j]);
  49.                 }
  50.         }
  51. }
  52. //---------------------------------
  53. int validate(int x, int y, int N){
  54.     if(x >= 0 && x <= N - 1 && y >= 0 && y <= N - 1){
  55.         return 1;
  56.     }
  57.     return 0;
  58. }
  59. //---------------------------------
  60. void our_map(int num) {
  61.         cout << endl << "map:" << endl;
  62.         for(int i = 0; i < num; ++i) {
  63.                 for(int j = 0; j < num; ++j) {
  64.                         cout << map[i][j] << " ";
  65.                 }
  66.                 cout << endl;
  67.         }
  68. }
  69. //---------------------------------
  70. void init(int n) {
  71.         for(int i = 0; i < n; ++i) {
  72.                 for(int j = 0; j < n; ++j) {
  73.                         maze[i][j] = 0;
  74.                         map[i][j] = 0;
  75.                         visit[i][j] = 0;
  76.                 }
  77.         }
  78. }
  79. //---------------------------------
  80. void search(int x,int y){
  81.         if(visit[x][y] == 1)
  82.                 return;
  83.         visit[x][y] = 1;
  84.         for(int k = 0; k < 4; k++) {
  85.                 int x1 = x + xstep[k];
  86.                int y1 = y + ystep[k];
  87.         if(validate(x1, y1, num) && maze[x][y] == '.') {
  88.                 if(find_map(x1, y1, num)) {
  89.                         map[x][y] = 1;
  90.                         }
  91.                 }
  92.         }
  93.     for (int i=0;i<4;i++){
  94.         int x1=x+xstep[i];
  95.         int y1=y+ystep[i];
  96.         if(validate(x1,y1,num) && maze[x1][y1] == '.'){
  97.             search(x1, y1);
  98.         }
  99.     }
  100. }
复制代码

题目网站就一直10分只给我5分,怎么办呀,这是为什么
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

  5. int foo(vector <vector <char>> maze, pair <int, int> A, pair <int, int> B, int n) {
  6.         auto &[r1, c1] = A;
  7.         auto &[r2, c2] = B;
  8.         if (r1 == r2 and c1 == c2) {
  9.                 return 1;
  10.         }
  11.         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) {
  12.                 return 0;
  13.         }
  14.         else if (maze[r1][c1] != '.' or maze[r2][c2] != '.') return 0;
  15.         vector <vector <char>> res = maze;
  16.         res[r1][c1] = '#';
  17.         return
  18.                 foo(res, make_pair(r1 + 1, c1), B, n) +
  19.                 foo(res, make_pair(r1 - 1, c1), B, n) +
  20.                 foo(res, make_pair(r1, c1 + 1), B, n) +
  21.                 foo(res, make_pair(r1, c1 - 1), B, n);
  22. }

  23. int main(void) {
  24.         int n, k;
  25.         vector <string> answer;
  26.         cin >> k;
  27.         for (int i = 0; i < k; ++i) {
  28.                 cin >> n;
  29.                 vector <vector <char>> maze (n, vector <char>(n, '#'));
  30.                 for (int r = 0; r < n; ++r) {
  31.                         for (int c = 0; c < n; ++c) {
  32.                                 cin >> maze[r][c];
  33.                         }
  34.                 }
  35.                 int x, y;
  36.                 pair <int, int> A, B;
  37.                 cin >> x >> y;
  38.                 A = make_pair(x, y);
  39.                 cin >> x >> y;
  40.                 B = make_pair(x, y);
  41.        
  42.                 if (foo(maze, A, B, n)) {
  43.                         answer.push_back("YES");
  44.                 }
  45.                 else {
  46.                         answer.push_back("NO");
  47.                 }
  48.         }
  49.        
  50.         for (const string& str: answer) {
  51.                 cout << str << endl;
  52.         }
  53.         return 0;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 01:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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