-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumericalMaze.cpp
95 lines (81 loc) · 2.35 KB
/
numericalMaze.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
ifstream fin("numericalMaze.in");
ofstream fout("numericalMaze.out");
int exitX;
bool g(int y, int x, int n, int m)
{
return y >= 0 && y <= n - 1 && x >= 0 && x <= m - 1;
}
void solve(int nowY, int nowX, vector<vector<int>> a, int nowMax, int nowCount, int n, int m, vector<vector<bool>> path)
{
if (g(nowY, nowX, n, m) == true)
{
if (a[nowY][nowX] == nowCount && path[nowY][nowX] == false)
{
path[nowY][nowX] = true;
if (nowY == n - 1)
{
exitX = min(exitX, nowX);
return;
}
else if (nowCount < nowMax)
{
solve(nowY + 1, nowX, a, nowMax, nowCount + 1, n, m, path);
solve(nowY - 1, nowX, a, nowMax, nowCount + 1, n, m, path);
solve(nowY, nowX + 1, a, nowMax, nowCount + 1, n, m, path);
solve(nowY, nowX - 1, a, nowMax, nowCount + 1, n, m, path);
}
else
{
solve(nowY + 1, nowX, a, nowMax + 1, 1, n, m, path);
solve(nowY - 1, nowX, a, nowMax + 1, 1, n, m, path);
solve(nowY, nowX + 1, a, nowMax + 1, 1, n, m, path);
solve(nowY, nowX - 1, a, nowMax + 1, 1, n, m, path);
}
}
}
}
int main()
{
int testCase;
fin >> testCase;
for (int c = 1; c <= testCase; ++c)
{
if (c != 1)
{
fout << '\n';
}
exitX = 1000;
int n, m;
fin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
vector<vector<bool>> path(n, vector<bool>(m, false));
for (int y = 0; y <= n - 1; ++y)
{
for (int x = 0; x <= m - 1; ++x)
{
fin >> a[y][x];
}
}
for (int x = 0; x <= m - 1; ++x)
{
solve(0, x, a, 1, 1, n, m, path);
if (exitX != 1000)
{
fout << "1 " << x + 1 << '\n' << n << ' ' << exitX + 1 << '\n';
break;
}
}
}
return 0;
}