Anonymous user
Dinesman's multiple-dwelling problem: Difference between revisions
Dinesman's multiple-dwelling problem (view source)
Revision as of 13:24, 7 August 2020
, 3 years ago→{{header|C++}}: C++ entry
(→{{header|Common Lisp}}: Implemented Common Lisp version) |
(→{{header|C++}}: C++ entry) |
||
Line 425:
For this program, to change conditions, one needs to edit content between BEGIN and END of problem specific setup.
Those could even be setup in an external file and gets <code>#include</code>d if need be.
=={{header|C++}}==
{{Works with|C++11}}
<lang cpp>#include <algorithm>
#include <array>
#include <cmath>
#include <functional>
#include <string>
#include <iostream>
#include <list>
int main() {
constexpr auto floors = 5;
constexpr auto top = floors - 1;
constexpr auto bottom = 0;
using namespace std;
array<string, floors> tenants = { "Baker", "Cooper", "Fletcher", "Miller", "Smith" };
const auto index_of = [&tenants](string t) {
for (int i = bottom; i <= top; i++)
if (tenants[i] == t)
return i;
throw "invalid tenant";
};
const list<function<bool()>> constraints = {
[&tenants]() { return tenants[top] != "Baker"; },
[&tenants]() { return tenants[bottom] != "Cooper"; },
[&tenants]() { return tenants[top] != "Fletcher"; },
[&tenants]() { return tenants[bottom] != "Fletcher"; },
[&index_of]() { return index_of("Miller") > index_of("Cooper"); },
[&index_of]() { return abs(index_of("Fletcher") - index_of("Smith")) != 1; },
[&index_of]() { return abs(index_of("Fletcher") - index_of("Cooper")) != 1; }
};
sort(tenants.begin(), tenants.end());
do {
bool ok = true;
for (const auto &c : constraints)
if (!c()) {
ok = false;
break;
}
if (ok) {
for (const auto &t : tenants)
cout << t << ' ';
cout << endl;
}
} while (next_permutation(tenants.begin(), tenants.end()));
return EXIT_SUCCESS;
}</lang>
=={{header|C sharp}}==
|