POJ3984-迷宫问题

GerJCS岛 / 2024-09-05 / 原文

为啥刷这道题

POJ3984

poj依旧上不去,快半个月了,可用平台

 

 初次草稿:

  1 #include<iostream>
  2 #include<string.h>//memset用的
  3 using namespace std;//cout用的
  4 #include<queue>//queue<node>q;
  5 struct node
  6 {
  7     int x;
  8     int y;
  9     char path[26];//一开始定义的全局变量char path[5][5];感觉是5行5列的字符,而不是5行5列字符串  //https://qb.zuoyebang.com/xfe-question/question/234d44683fa655bd9af73f12e1f4e335.html
 10 };
 11 queue<node>q;
 12 int vismap[5][5];
 13 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右
 14 int map[5][5];
 15 int main()
 16 {
 17     memset(map,0,sizeof map);
 18     memset(vismap,0,sizeof vismap);
 19 //强迫症总想看懂别人的代码,q神慰藉不看
 20 //    输入多个数据不会了,只能想到第一个数字不是EOF,然后输入后面的,接下来循环输入4行
 21     for(int i=0;i<5;i++)
 22         for(int j=0;j<5;j++)
 23             cin>>map[i][j];//思考scanf的话,map数组用不用加&
 24 
 25 //    for(int i=0;i<5;i++){//验证输入数据是否正确
 26 //        for(int j=0;j<5;j++)
 27 //            printf("%d ",map[i][j]);
 28 //        printf("\n");
 29 //    }
 30 //    方法一广搜得到最少步数,再深搜等于最少步数就return一步步输出,但有点der
 31 //    方法二广搜记录每一步
 32 
 33 
 34 //    字符拼接:https://blog.csdn.net/MrWangHao/article/details/130302421#:~:text=%E6%96%87%E7%AB%A0%E4%BB%8B%E7%BB%8D%E4%BA%86C%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%94%A8
 35 //    char shr1[3]="32";     //shr1[2]="32"报错,理由长度定义少了:https://blog.csdn.net/weixin_45792414/article/details/125293580
 36 //    char str1[3]="32";
 37 //    char str2[3]="01";
 38 //    strcat(str1,str2);
 39 //    printf("%s\n",str1);
 40 
 41 //#include<stdio.h>
 42 //#include<string.h>
 43 //int main()
 44 //{
 45 //    char str1[3]="32";
 46 //    char str2[3]="01";
 47 //    strcat(str1,"4");
 48 //    printf("%s\n",str1);
 49 //}
 50 
 51 
 52     node Snode;
 53     Snode.x=0;
 54     Snode.y=0;
 55     vismap[0][0]=1;
 56     q.push(Snode);
 57     while(!q.empty())
 58     {
 59         node thisnode=q.front();
 60             node nextnode;
 61         for(int i=0;i<4;i++){
 62             nextnode.x=thisnode.x+dir[i][0];
 63             nextnode.y=thisnode.y+dir[i][1];
 64 
 65             if(nextnode.x==4&&nextnode.y==4)
 66             {
 67                 printf("!:%s\n",thisnode.path);
 68                 break;
 69             }
 70 
 71             if(0<=nextnode.x&&nextnode.x<5&&0<=nextnode.y&&nextnode.y<5&&!vismap[nextnode.x][nextnode.y]&&map[nextnode.x][nextnode.y]==0){
 72                 vismap[nextnode.x][nextnode.y]=1;
 73                 q.push(nextnode);
 74                 q.pop();
 75                 if(i==0)
 76                     strcat(nextnode.path,"0");//艹字符串一堆东西要学,如果是字符的话这里是‘’ 还是“”
 77                 if(i==1)
 78                     strcat(nextnode.path,"1");
 79                 if(i==2)
 80                     strcat(nextnode.path,"2");
 81                 if(i==3)
 82                     strcat(nextnode.path,"3");
 83             }
 84         }
 85     }
 86 
 87 
 88 }
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 //#include<iostream>
 97 //#include<string.h>//memset用的
 98 //using namespace std;//队列queue的pop push用的
 99 //#include<queue>//这两个一起给queue队列操作提供头文件,先搁置
100 //
101 //int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右
102 //int map[5][5];
103 //int main()
104 //{
105 ////    for(int i=0;i<1;i++)
106 ////        int a=1;
107 ////    printf("%d\n",a);
108 //
109 //}
110 
111 //#include <stdio.h>
112 //
113 //int main(void)
114 //{
115 //    int i=0;
116 //    int sum=0;
117 //    for(int sum=10,i=1;i<2;++i)
118 //        sum+=i;
119 //    printf("%d %d\n",i,sum);
120 //}
121 
122 //
123 //#include <stdio.h>
124 //int main(void)
125 //{
126 //    for(int i=0;i<3;++i)
127 //        int sum=3;
128 //    printf("%d\n",sum);
129 //}https://bbs.csdn.net/topics/391956385#:~:text=%E5%A6%82%E6%9E%9C%E4%BD%A0%E6%98%AFC%E8%AF%AD%E8%A8%80%E7%BC%96%E8%AF%91,
130 //一群傻逼瞎说
131 
132 //扯了这么多看似没啥意义但其实我也真不知道有啥用处的坑
133 //学strcat的时候发现定义了str老说我没定义,因为我是把这题代码写一半,
134 //用到拼接再测试写的,注释了一半最后发现for的问题,
135 //以为C语言用for定义2遍变量会报错没定义
136 //结果是for里的定义是无效定义
137 //所以注释一半写其他的还是可以学到一些东西的
138 
139 
140 
141 //https://blog.csdn.net/weixin_43719752/article/details/120599794
142 //    虽然我啥也不会,但学了这么多年还是知道啥游泳啥咩有的,比如这个,就有预感,他没啥用
143 //    学性价比不搞
144 //搁置
145 //
146 //第一次觉得之前写的所有代码都有问题,是回顾了形参后
147 //第二次就是这次for里定义int
148 
149 //C语言for里面不允许定义变量
150 
151 //我念大学15年那时候编程也没这么多屁事啊
152 //#:~:text=%E4%B8%BA%E4%BB%80%E4%B9%88for%20(in
153 //一堆老头来闲的回改名字,跟USB一样,张宇讲过数学界的事
154 
155 //第一次觉得后怕是知道阿辉讲的切弯技巧
156 
157 //岛娘一样胡言乱语
View Code

 简化版:

 1 #include<iostream>
 2 #include<string.h>//memset用的
 3 using namespace std;//cout用的
 4 #include<queue>//queue<node>q;
 5 struct node
 6 {
 7     int x;
 8     int y;
 9     char path[26];
10 };
11 queue<node>q;
12 int vismap[5][5];
13 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右
14 int map[5][5];
15 int main()
16 {
17     memset(map,0,sizeof map);
18     memset(vismap,0,sizeof vismap);
19     for(int i=0;i<5;i++)
20         for(int j=0;j<5;j++)
21             cin>>map[i][j];
22 
23 
24 
25     node Snode;
26     Snode.x=0;
27     Snode.y=0;
28     vismap[0][0]=1;
29     q.push(Snode);
30     while(!q.empty())
31     {
32         node thisnode=q.front();
33             node nextnode;
34         for(int i=0;i<4;i++){
35             nextnode.x=thisnode.x+dir[i][0];
36             nextnode.y=thisnode.y+dir[i][1];
37 
38             if(nextnode.x==4&&nextnode.y==4)
39             {
40                 printf("!:%s\n",thisnode.path);
41                 break;
42             }
43 
44             if(0<=nextnode.x&&nextnode.x<5&&0<=nextnode.y&&nextnode.y<5&&!vismap[nextnode.x][nextnode.y]&&map[nextnode.x][nextnode.y]==0){
45                 vismap[nextnode.x][nextnode.y]=1;
46                 q.push(nextnode);
47                 q.pop();
48                 if(i==0)
49                     strcat(nextnode.path,"0");//艹字符串一堆东西要学,如果是字符的话这里是‘’ 还是“”
50                 if(i==1)
51                     strcat(nextnode.path,"1");
52                 if(i==2)
53                     strcat(nextnode.path,"2");
54                 if(i==3)
55                     strcat(nextnode.path,"3");
56             }
57         }
58     }
59 
60 
61 }
View Code

发现输出乱码,逐步检查发现第一个strcat赋值完就是乱码

好像是没初始化的事,单独拿出来测试,相当离谱诡异

#include<iostream>
#include<string.h>
using namespace std;
#include<queue>
struct node
{
    int x;
    int y;
    char path[26];
};
int main()
{
    node a;
    printf("%s\n",a.path);
    strcat(a.path,"13");
    printf("%s\n",a.path);
    printf("-----------------\n");

    node b;
    printf("%s\n",b.path);
    printf("-----------------\n");
    
    node c;
    strcat(c.path,"13");
    printf("%s\n",c.path);
    printf("-----------------\n");
}


//这他么离奇,分别单独运行node a/b/c段代码结果居然不一样,整体运行结果又不一样,绝了!!!
View Code

记得之前有听说过局部要初始化,全局不用的事,但一直也没局部特意初始化过也没错过啊

继续测试惊呆了

 1 #include<iostream>
 2 #include<string.h>
 3 using namespace std;
 4 #include<queue>
 5 struct node
 6 {
 7     int x;
 8     int y;
 9     char path[26];
10 };
11 int main()
12 {
13     int a;
14     printf("%d\n",a);
15     a++;
16     printf("%d\n-------\n",a);
17 
18     int b;
19     printf("%d\n----\n",b);
20 
21     int c;
22     c++;
23     printf("%s\n----\n",c);
24 }
25 //这他么还有点死循环的意思呢???
View Code