-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewHanoiHARDWAY.cpp
80 lines (67 loc) · 1.42 KB
/
newHanoiHARDWAY.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
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
ifstream fin("newHanoi.in");
ofstream fout("newHanoi.out");
bool isSquare(int in)
{
int i = sqrt(in);
return i * i == in;
}
int solve(vector<int> a, int now)
{
int size = a.size(), maxCount = 0;
// for (int i = 0; i <= size - 1; ++i)
// {
// fout << a[i] << ' ';
// }
// fout << '\n';
bool sth = false;
int pos1 = -1, pos2 = -1;
for (int i = 0; i <= size - 1; ++i)
{
if (isSquare(a[i] + now) == true && pos2 == -1 && a[i] != -1)
{
pos2 = i;
}
if (a[i] == -1 && pos1 == -1)
{
pos1 = i;
}
}
if (pos1 == -1 && pos2 == -1)
{
return now - 1;
}
if (pos1 != -1)
{
int temp1 = a[pos1];
a[pos1] = now;
maxCount = max(maxCount, solve(a, now + 1));
a[pos1] = temp1;
}
if (pos2 != -1)
{
int temp2 = a[pos2];
a[pos2] = now;
maxCount = max(maxCount, solve(a, now + 1));
a[pos2] = temp2;
}
return maxCount;
}
int main()
{
int count;
fin >> count;
for (int i = 1; i <= count; ++i)
{
int size;
fin >> size;
vector<int> a(size, -1);
fout << solve(a, 1) << '\n';
}
return 0;
}