forked from StefanoChiodino/c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Labirinto.cpp
149 lines (133 loc) · 4.02 KB
/
Labirinto.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
1_2_3
4_O_6 tabella spostamenti
7_8_9
Lo spostamento Y+1 è inteso verso il basso
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define n 20
#define m 20
int labirinto[n][m];
int x=1,y=1;
struct nodo{
int x,y;
struct nodo *next;
}*testa=NULL,*tmp=NULL;
void push(){
tmp=(struct nodo*)malloc(sizeof(struct nodo));
if(testa==NULL)
tmp->next=NULL;
else
tmp->next=testa;
testa=tmp;
testa->x=x;
testa->y=y;
printf("E' stato trovato un nodo!\n");
tmp=testa;
while(tmp!=NULL){
printf("--> %d&%d ",tmp->x,tmp->y);
tmp=tmp->next;
}
}
void pop(){
if(testa==NULL){
printf("\n\nNon trovo soluzioni");
fflush(stdin);getchar();
}
else{
x=testa->x;
y=testa->y;
tmp=testa;
testa=testa->next;
free(tmp);
}
}
void mostra(){
system("CLS");
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++)
printf("%c",labirinto[i][j]);
printf("\n");
}
printf("\n\n\n");
}
void valuta(){
}
void genera(){
int i,j;
do{
printf("\n\n\nGenero....\n");
for(i=1;i<n-1;i++)
for(j=1;j<m-1;j++)
if(rand()%2)
labirinto[i][j]=219;
else
labirinto[i][j]=0;
}while(labirinto[1][1]!=0 || labirinto[n-2][m-2]!=0);
for(i=0;i<n;i++)
labirinto[0][i]=labirinto[n-1][i]=labirinto[i][0]=labirinto[i][m-1]=219;
mostra();
fflush(stdin);getchar();
}
void parti(){
labirinto[x][y]=176;
int eunnodo;
do{
eunnodo=0;
if(labirinto[x+1][y+1]==0)
eunnodo++;
if(labirinto[x][y+1]==0)
eunnodo++;
if(labirinto[x+1][y]==0)
eunnodo++;
if(labirinto[x-1][y+1]==0)
eunnodo++;
if(labirinto[x+1][y-1]==0)
eunnodo++;
if(labirinto[x-1][y]==0)
eunnodo++;
if(labirinto[x][y-1]==0)
eunnodo++;
if(labirinto[x-1][y-1]==0)
eunnodo++;
if(eunnodo>1)
push();
if(labirinto[x+1][y+1]==0){
x+=1;
y+=1;
}
else if(labirinto[x][y+1]==0)
y+=1;
else if(labirinto[x+1][y]==0)
x+=1;
else if(labirinto[x-1][y+1]==0){
x-=1;
y+=1;
}
else if(labirinto[x+1][y-1]==0){
x+=1;
y-=1;
}
else if(labirinto[x-1][y]==0)
x-=1;
else if(labirinto[x][y-1]==0)
y-=1;
else if(labirinto[x-1][y-1]==0){
x-=1;
y-=1;
}
else pop();
labirinto[x][y]=176;
mostra();
}while(x!=n-2 || y!=m-2);
printf("\n\n\nFATTO!!\n");
fflush(stdin);getchar();
}
int main(){
srand(time(NULL));
genera();
parti();
}