-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.cpp
66 lines (58 loc) · 1.2 KB
/
maze.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
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
struct pace{
int x;
int y;
}pace[100];
int a[5][5], v[5][5] = {0}, cnt = 0;
int nx[] = {1, 0, 0, -1};
int ny[] = {0, 1, -1, 0};
void maze(int x, int y);
void print();
bool isafe(int x, int y);
int main()
{
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
scanf("%d", &a[i][j]);
}
}
v[0][0] = 1;
pace[cnt].x = 0;
pace[cnt++].y = 0;
maze(0, 0);
return 0;
}
bool isafe(int x, int y){
if(x < 0 || x > 4 || y < 0 || y > 4 || a[y][x] == 1){
return false;
}
return true;
}
void maze(int x, int y){
if(x == 4 && y == 4){
v[4][4] = 1;
print();
exit(0);
}else{
for(int i = 0; i < 4; i++){
int mx = x + nx[i];
int my = y + ny[i];
if(!v[my][mx] && isafe(mx, my)){
v[my][mx] = 1;
pace[cnt].x = mx;
pace[cnt++].y = my;
maze(mx, my);
v[my][mx] = 0;
cnt--;
}
}
}
}
void print(){
for(int i = 0; i < cnt; i++){
printf("(%d, %d)\n", pace[i].y, pace[i].x);
}
}