halisi7

一个专注技术的组织

0%

水管工游戏-c语言

水管工游戏

问题描述

image-20220420151631766

image-20220420151651747

image-20220420151826646

分析

image-20220420151840116

image-20220420151920723

image-20220420152129666

image-20220420152151463

image-20220420152854022

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
int n, m, front,top,flag;
int a[51][51];
int book[51][51];
struct note {
int x;
int y;
};
struct note s[101];
void dfs(int x,int y,int front) {
int i;
if (x == n && y == m + 1) {
flag = 1;
for (i = 0; i < top; i++) {

printf("(%d,%d)", s[i].x, s[i].y);
}
printf("\n---------------------\n");
return;
}
if (x<1 || x>n || y<1 || y>m)
return;
if (a[x][y] == 0)
return;
if (book[x][y] == 1)
return;
s[top].x = x;
s[top].y = y;
top++;
book[x][y] = 1;
//直管
if (a[x][y] >= 5 ) {

if (front == 1) {
dfs(x , y+1, 1);
}
if (front == 2) {
dfs(x+1, y , 2);
}
if (front == 3) {
dfs(x, y - 1, 3);
}
if (front == 4) {
dfs(x - 1, y, 4);
}
}
if (a[x][y] <= 4 && a[x][y] >= 1 ) {

if (front == 1) {
dfs(x+1, y , 2);
dfs(x - 1, y, 4);
}
if (front == 2) {
dfs(x , y+1, 1);
dfs(x , y-1, 3);
}
if (front == 3) {
dfs(x-1, y , 4);
dfs(x + 1, y, 2);
}
if (front == 4) {
dfs(x , y+1, 1);
dfs(x , y-1, 3);
}



}
book[x][y] = 0;
top--;
return;
}
int main() {
int i, j;
scanf_s("%d %d", &n, &m);
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
scanf_s("%d", &a[i][j]);

dfs(1, 1, 1);
if (flag == 0) {
printf("找不到路径。");
}
getchar(); getchar();
return 0;
}

输入

1
2
3
4
5
6
5 4
5 3 5 3
1 5 3 0
2 3 5 1
6 1 1 5
1 5 5 4

输出(直接输出路径)

1
(1,1)(1,2)(2,2)(3,2)(3,3)(3,4)(4,4)(5,4)
打赏一下作者~ ฅ( ̳• ◡ • ̳)ฅ