1个回答

c语言走迷宫问题提问！

c语言迷宫问题怎么解？？

c语言走迷宫问题，输不出结果。。。
#include <stdio.h> void print (int map[][10]); //打印迷宫 void walk (int map[][10], int x, int y); //走迷宫 void push (int x, int *stack); //进栈 void delet (); //出栈 int stackx[100]; //用来存放每一步的x值 int stacky[100]; //每一步的y值 int size = 0; //一共已经走的步数 int walked[10][10]; //已经走过的位置，走过的话值为1，没走过为0 int main (void) { int i, j; int map[10][10]; for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { walked[i][j] = 0; map[i][j] = ' '; } } for (i = 0, j = 0; j < 10; j++) map[i][j] = 'o'; for (i = 9, j = 0; j < 10; j++) map[i][j] = 'o'; for (i = 0, j = 0; i < 10; i++) map[i][j] = 'o'; for (i = 0, j = 9; i < 10; i++) map[i][j] = 'o'; map[1][3] = map[2][3] = map[1][7] = map[2][7] = map[3][5] = map[3][6] = map[4][2] = map[4][3] = map[4][4] = map[5][4] = map[6][2] = map[6][6] = map[7][2] = map[7][3] = map[7][4] = map[7][6] = map[7][7] = map[8][1] = 'o'; print (map); walk (map, 1, 1); return 0; } void print (int map[][10]) { int i, j; for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) printf ("%c", map[i][j]); printf ("\n"); } } void walk (int map[][10], int x, int y) { int i; if (map[x][y] != 'o' && walked[x][y] != 1) //当前位置没有墙并且也没有走过 { size++; push (x, stackx); push (y, stacky); if (x == 8 && y == 8) //到达终点结束 { for (i = 0; i < size; i++) printf ("%d %d\n", stackx[i], stacky[i]); return; } } else //位置不可通，判断栈顶位置是否还有其他方向未探索 { if (size != 0 && walked[stackx[size-1]+1][stacky[size-1]] != 1 && map[stackx[size-1]+1][stacky[size-1]] != 'o' && stackx[size-1]+1 <= 9 && stackx[size-1]+1 >= 0) { x = stackx[size-1] + 1; y = stacky[size-1]; walked[x][y] = 1; walk (map, x, y); } else if (size != 0 && walked[stackx[size-1]][stacky[size-1] + 1] != 1 && map[stackx[size-1]][stacky[size-1] + 1] != 'o' && stacky[size-1] + 1 <= 9 && stacky[size-1] + 1 >= 0) { x = stackx[size-1]; y = stacky[size-1] + 1; walked[x][y] = 1; walk (map, x, y); } else if (size != 0 && walked[stackx[size-1]-1][stacky[size-1]] != 1 && map[stackx[size-1]-1][stacky[size-1]] != 'o' && stackx[size-1]-1 <= 9 && stackx[size-1]-1 >= 0) { x = stackx[size-1]-1; y = stacky[size-1]; walked[x][y] = 1; walk (map, x, y); } else if (size != 0 && walked[stackx[size-1]][stacky[size-1]-1] != 1 && map[stackx[size-1]][stacky[size-1]-1] != 'o' && stacky[size-1]-1 >= 0 && stacky[size-1]-1 <= 9) { x = stackx[size-1]; y = stacky[size-1]-1; walked[x][y] = 1; walk (map, x, y); } else //如果全探索了，就删去栈顶位置 { delet (); if (size == 0) return; walk(map, x, y); } } } void push (int x, int *stack) { stack[size-1] = x; } void delet () { size--; }

1.生成一个随机的正方形迷宫，输入大小n,n²个节点，全连通，（最少n-1条边，最多2n（n-1）条边），边只能存在于相邻节点之间，要求迷宫全连通，并且边数约为最少和最多边数的平均数 2.将迷宫呈现出来，节点用数字表示，位数等于最大边数的位数，位数不足的用0补足保证上下对齐，边用|和-表示，若不存在边则用空格 3.设计方法，Dijkstra算法，寻找任意两点之间的最短路径
C 的走迷宫问题 实在找不出问题所在了。。。
mice.txt文件内容 24 24 1 1 24 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 ------------------------------------------------------------------- （感觉是search的问题 但找不出问题） #include <stdio.h> #include<stdlib.h> int V=0; int xnum; int ynum; int a[100][100]; typedef struct { int x; int y; }NODE; typedef struct { NODE nownode; NODE prenode; int dir; }GRID; NODE start; NODE end; GRID grid; void Init() { FILE *fp; fp=fopen("mice.txt","r"); fscanf(fp,"%d%d%d%d%d%d",&xnum,&ynum,&start.x,&start.y,&end.x,&end.y);//24 24 1 1 24 1 int i,j; for(j=0;j<=ynum+1;j++) for(i=0;i<=xnum+1;i++) a[j][i]=1; for(j=1;j<=ynum;j++) for(i=1;i<=xnum;i++) fscanf(fp,"%d",&a[j][i]); //grid.nownode.x =start.x; //grid.nownode.y=start.y; grid.nownode=start; fclose(fp); } void search(int x,int y){ if((x==24)&&(y==1)) V=1; else{ a[x][y]=1; if((V==0)&&(a[x][y+1]==0)) //向右查找----------v==0 可避免在V==1时的不必要搜索 第一个if里的可略 search(x,y+1); if((V==0)&&(a[x+1][y]==0))//向下查找 search(x+1,y); if((V==0)&&(a[x][y-1]==0)) //向左查找 search(x,y-1); if((V==0)&&(a[x-1][y]==0)) //向上查找 search(x-1,y); } a[x][y]=0; if(V=1) a[x][y]=2; } int main() { int x,y; Init(); x=grid.nownode.x; y=grid.nownode.y; printf("%d %d\n",start.x,start.y); printf("%d %d %d \n",a[24][1],a[24][0],a[25][1]); printf("%d \n",V); search(x,y); for(int i=0;i<=xnum+1;i++) { for(int j=0;j<=ynum+1;j++) printf("%d",a[i][j]); printf("\n"); } scanf("%d",&x); return 0; }

Problem Description 　　给定一个m × n (m行, n列)的迷宫，迷宫中有两个位置，gloria想从迷宫的一个位置走到另外一个位置，当然迷宫中有些地方是空地，gloria可以穿越，有些地方是障碍，她必须绕行，从迷宫的一个位置，只能走到与它相邻的4个位置中,当然在行走过程中，gloria不能走到迷宫外面去。令人头痛的是，gloria是个没什么方向感的人，因此，她在行走过程中，不能转太多弯了，否则她会晕倒的。我们假定给定的两个位置都是空地，初始时，gloria所面向的方向未定，她可以选择4个方向的任何一个出发，而不算成一次转弯。gloria能从一个位置走到另外一个位置吗？ Input 　　第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数，接下来为t组测试数据，每组测试数据中， 　　第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数，接下来m行，每行包括n个字符，其中字符'.'表示该位置为空地，字符'*'表示该位置为障碍，输入数据中只有这两种字符，每组测试数据的最后一行为5个整数k, x1, y1, x2, y2 (1 ≤ k ≤ 10, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m),其中k表示gloria最多能转的弯数，(x1, y1), (x2, y2)表示两个位置，其中x1，x2对应列，y1, y2对应行。 Output 　　每组测试数据对应为一行，若gloria能从一个位置走到另外一个位置，输出“yes”，否则输出“no”。 Sample Input 2 5 5 ...** *.**. ..... ..... *.... 1 1 1 1 3 5 5 ...** *.**. ..... ..... *.... 2 1 1 1 3 Sample Output no yes

#include <stdio.h> #include <stdlib.h> int visit(int, int); //迷宫矩阵，2代表墙壁，0代表通道 int maze[7][7] = {{2, 2, 2, 2, 2, 2, 2}, {2, 0, 0, 0, 0, 0, 2}, {2, 0, 2, 0, 2, 0, 2}, {2, 0, 0, 2, 0, 2, 2}, {2, 2, 0, 2, 0, 2, 2}, {2, 0, 0, 0, 0, 0, 2}, {2, 2, 2, 2, 2, 2, 2}}; int startI = 1, startJ = 1; // 入口 int endI = 5, endJ = 5; // 出口 int success = 0; int main(void) { int i, j; printf("显示迷宫：\n"); for(i = 0; i < 7; i++) { for(j = 0; j < 7; j++) { if(maze[i][j] == 2) printf("█"); else printf(" "); } printf("\n"); } if(visit(startI, startJ) == 0) { printf("\n没有找到出口！\n"); } else { printf("\n显示路径：\n"); for(i = 0; i < 7; i++) { for(j = 0; j < 7; j++) { if(maze[i][j] == 2) printf("█"); else if(maze[i][j] == 1) printf("◇"); else printf(" "); } printf("\n"); } } return 0; } int visit(int i, int j) { //该点走过，标记为1 maze[i][j] = 1; //走到终点，成功 if(i == endI && j == endJ) success = 1; //向四个方向递归调用函数visit() if(success != 1 && maze[i][j+1] == 0) visit(i, j+1); if(success != 1 && maze[i+1][j] == 0) visit(i+1, j); if(success != 1 && maze[i][j-1] == 0) visit(i, j-1); if(success != 1 && maze[i-1][j] == 0) visit(i-1, j); //该点走过，但没成功，则该点重新置为0 if(success != 1) maze[i][j] = 0; return success; } 最后一句，该点走过，但没成功，则该点重新置为0，神魔意思啊，是回退一个格，还是回退多格，重新置零之后，又重新走了一边遍怎么办

Problem Description 小明又一次陷入了大魔王的迷宫，在无人机的帮忙下，小明获得了整个迷宫的草图。 不同于一般的迷宫，魔王在迷宫里安置了机关，一旦触碰，那么四个方向所在的格子，将翻转其可达性（原先可通过的格子不可通过，反之亦然，机关可以反复触发）。为了防止小明很容易地出逃，魔王在临走前把钥匙丢在了迷宫某处，只有拿到钥匙，小明才能开门在出口处离开迷宫。 万般无奈之下，小明想借助聪明的你，帮忙计算是否有机会离开这个迷宫，最少需要多少时间。（每一单位时间只能向四邻方向走一步） Input 第一行为 T，表示输入数据组数。 下面 T 组数据，对于每组数据： 第一行是两个数字 n, m（2 < n * m <= 64），表示迷宫的长与宽。 接下来 n 行，每行 m 个字符，‘.’表示空地可以通过，‘x’表示陷阱，‘*’表示机关，‘S’代表起点，‘E’代表出口，‘K’表示钥匙(保证存在且只有一个)。 Output 对第 i 组数据，输出 Case #i: 然后输出一行，仅包含一个整数，表示最少多少步能够拿到钥匙并走出迷魂阵，如果不能则打出-1。 Sample Input 5 5 7 ...*x.. ...x... xEx.... *x...K. .x*...S 5 7 K..*x.. ...x... xEx.... *x..... .x*...S 5 7 ..K*x.. ..*x*.. xEx.... *x..... .x*...S 5 7 ..K*x.. .*xx*.. *E*.... xx..... .x*...S 4 4 S*.. **.. ...E ...K Sample Output Case #1: 11 Case #2: 13 Case #3: 13 Case #4: 11 Case #5: -1

BFS走迷宫传送门超时怎么改进啊？

Line & Circle Maze 迷宫的问题
Problem Description A manufacturer keeps an ordered table of serial numbers by listing in each row of the table a range of serial numbers along with two corresponding pieces of information called the status code and the transfer code. A four-column table stores information about ranges of serial numbers in this order: starting serial number, ending serial number, status code, transfer code. Serial numbers as well as transfer codes are integers from 1 to 2147483647, and status codes are a single upper-case letter. The table is maintained in increasing order of serial numbers, serial number ranges are never allowed to overlap, and for any given serial number, the table must always accurately represent the most recent data (status code and transfer code) for that serial number. Let's say that 100,000 serial numbers are created with a status of "A" and a transfer code of "1". An entry for those serial numbers might look like this: 1 100000 A 1 This is obviously far more efficient than storing 100,000 individual rows all with the same status and transfer codes. The challenge arises when serial numbers within already defined ranges need to be given different status or transfer codes. For example, if serial number 12345 needs to change to status B, the above table would need to become three separate entries: 1 12344 A 1 12345 12345 B 1 12346 100000 A 1 Now let's change the transfer code of all serial numbers in the range 12000 to 12999 to 2. This gets us: 1 11999 A 1 12000 12344 A 2 12345 12345 B 2 12346 12999 A 2 13000 100000 A 1 Now change all existing serial numbers from 10000 to 100000 to status C and transfer code 2: 1 9999 A 1 10000 100000 C 2 Once created a serial number will never be deleted, but it is possible to have ranges of undefined serial numbers between ranges of defined ones. To demonstrate, let's now set all serial numbers from 1000000 to 1999999 to status Z and transfer code 99: 1 9999 A 1 10000 100000 C 2 1000000 1999999 Z 99 Finally, the table is always maintained with a minimal number of rows, meaning specifically that there will never be two adjacent rows in the table where one would suffice. For example, consider the following serial number table: 1 10 A 1 11 20 A 1 21 30 B 1 The first two rows could actually be represented by a single row, meaning that the table above does not have a minimal number of rows. The same data represented by a minimal number of rows would look like this: 1 20 A 1 21 30 B 1 The following table, however, because the first two rows have non-matching transfer codes, already contains the minimal number of rows: 1 10 A 1 11 20 A 2 21 30 B 1 Similarly, the following table cannot be reduced further because the first two rows do not represent a continuous series of serial numbers: 1 10 A 1 12 20 A 1 21 30 B 1 Input Each input case begins with a single line that is a character string naming the test case. This string contains at most 80 characters. The name "END" marks the end of the input. Following this will be 1 to 100 lines of the form "A B S T", where A, B, and T are integers in the range 1 to 231-1, S is an uppercase letter, and A<=B. These lines are, in the order they are to be applied, the serial number transactions to be recorded, where A is the start of the serial number range, B is the end of the serial number range, S is the status code, and T is the transfer code. The list of serial number transactions is terminated by a line containing only a 0 (zero) character. Output For each input case, echo the test case name to the output on a line by itself, followed by the resulting minimal-rows serial number table that results after all serial number transactions have been applied. Sample Input First Example 1 100000 A 1 12345 12345 B 1 0 And Another 1 100000 A 1 12345 12345 B 1 12000 12999 A 2 12345 12345 B 2 0 Test Case Three 1 100000 A 1 12345 12345 B 1 12000 12999 A 2 12345 12345 B 2 10000 100000 C 2 0 Example Four 1 100000 A 1 12345 12345 B 1 12000 12999 A 2 12345 12345 B 2 10000 100000 C 2 1000000 1999999 Z 99 0 Example 5 1 10 A 1 21 30 B 1 11 20 A 1 0 Example 6 21 30 B 1 1 10 A 1 11 20 A 2 0 Example 7 12 20 A 1 21 30 B 1 1 10 A 1 0 END Sample Output First Example 1 12344 A 1 12345 12345 B 1 12346 100000 A 1 And Another 1 11999 A 1 12000 12344 A 2 12345 12345 B 2 12346 12999 A 2 13000 100000 A 1 Test Case Three 1 9999 A 1 10000 100000 C 2 Example Four 1 9999 A 1 10000 100000 C 2 1000000 1999999 Z 99 Example 5 1 20 A 1 21 30 B 1 Example 6 1 10 A 1 11 20 A 2 21 30 B 1 Example 7 1 10 A 1 12 20 A 1 21 30 B 1

Java学习的正确打开方式

linux系列之常用运维命令整理笔录

python学习方法总结(内附python全套学习资料)

python 简易微信实现（注册登录+数据库存储+聊天+GUI+文件传输）
socket+tkinter详解+简易微信实现 历经多天的努力，查阅了许多大佬的博客后终于实现了一个简易的微信O(∩_∩)O~~ 简易数据库的实现 使用pands+CSV实现数据库框架搭建 import socket import threading from pandas import * import pymysql import csv # 创建DataFrame对象 # 存储用户数据的表（...

Python十大装B语法
Python 是一种代表简单思想的语言，其语法相对简单，很容易上手。不过，如果就此小视 Python 语法的精妙和深邃，那就大错特错了。本文精心筛选了最能展现 Python 语法之精妙的十个知识点，并附上详细的实例代码。如能在实战中融会贯通、灵活使用，必将使代码更为精炼、高效，同时也会极大提升代码B格，使之看上去更老练，读起来更优雅。

2019年11月中国大陆编程语言排行榜
2019年11月2日，我统计了某招聘网站，获得有效程序员招聘数据9万条。针对招聘信息，提取编程语言关键字，并统计如下： 编程语言比例 rank pl_ percentage 1 java 33.62% 2 cpp 16.42% 3 c_sharp 12.82% 4 javascript 12.31% 5 python 7.93% 6 go 7.25% 7 p...

《奇巧淫技》系列-python！！每天早上八点自动发送天气预报邮件到QQ邮箱

Python实例大全（基于Python3.7.4）

SQL-小白最佳入门sql查询一

JavaScript 为什么能活到现在？

Nginx 原理和架构
Nginx 是一个免费的，开源的，高性能的 HTTP 服务器和反向代理，以及 IMAP / POP3 代理服务器。Nginx 以其高性能，稳定性，丰富的功能，简单的配置和低资源消耗而闻名。 Nginx 的整体架构 Nginx 里有一个 master 进程和多个 worker 进程。master 进程并不处理网络请求，主要负责调度工作进程：加载配置、启动工作进程及非停升级。worker 进程负责处...

Python 编程开发 实用经验和技巧
Python是一门很灵活的语言，也有很多实用的方法，有时候实现一个功能可以用多种方法实现，我这里总结了一些常用的方法和技巧，包括小数保留指定位小数、判断变量的数据类型、类方法@classmethod、制表符中文对齐、遍历字典、datetime.timedelta的使用等，会持续更新......

“狗屁不通文章生成器”登顶GitHub热榜，分分钟写出万字形式主义大作