CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
|
|
@ -0,0 +1,64 @@
|
|||
import java.util.*;
|
||||
|
||||
class DinesmanMultipleDwelling
|
||||
{
|
||||
private static void generatePermutations(String[] apartmentDwellers, Set<String> set, String curPermutation)
|
||||
{
|
||||
for (String s : apartmentDwellers)
|
||||
{
|
||||
if (!curPermutation.contains(s))
|
||||
{
|
||||
String nextPermutation = curPermutation + s;
|
||||
if (nextPermutation.length() == apartmentDwellers.length)
|
||||
set.add(nextPermutation);
|
||||
else
|
||||
generatePermutations(apartmentDwellers, set, nextPermutation);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
private static boolean topFloor(String permutation, String person)
|
||||
{ return permutation.endsWith(person); }
|
||||
|
||||
private static boolean bottomFloor(String permutation, String person)
|
||||
{ return permutation.startsWith(person); }
|
||||
|
||||
public static boolean livesAbove(String permutation, String upperPerson, String lowerPerson)
|
||||
{ return permutation.indexOf(upperPerson) > permutation.indexOf(lowerPerson); }
|
||||
|
||||
public static boolean adjacent(String permutation, String person1, String person2)
|
||||
{ return (Math.abs(permutation.indexOf(person1) - permutation.indexOf(person2)) == 1); }
|
||||
|
||||
private static boolean isPossible(String s)
|
||||
{
|
||||
// Conditions here
|
||||
if (topFloor(s, "B"))
|
||||
return false;
|
||||
if (bottomFloor(s, "C"))
|
||||
return false;
|
||||
if (topFloor(s, "F") || bottomFloor(s, "F"))
|
||||
return false;
|
||||
if (!livesAbove(s, "M", "C"))
|
||||
return false;
|
||||
if (adjacent(s, "S", "F"))
|
||||
return false;
|
||||
if (adjacent(s, "F", "C"))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Set<String> set = new HashSet<String>();
|
||||
generatePermutations(new String[] { "B", "C", "F", "M", "S" }, set, "");
|
||||
for (Iterator<String> iterator = set.iterator(); iterator.hasNext(); )
|
||||
{
|
||||
String permutation = iterator.next();
|
||||
if (!isPossible(permutation))
|
||||
iterator.remove();
|
||||
}
|
||||
for (String s : set)
|
||||
System.out.println("Possible arrangement: " + s);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue