Skip to content

Instantly share code, notes, and snippets.

@evandrix
Created January 25, 2012 03:00
Show Gist options
  • Save evandrix/1674363 to your computer and use it in GitHub Desktop.
Save evandrix/1674363 to your computer and use it in GitHub Desktop.
UVa Online Judge Practice
import static java.lang.Math.*;
import static java.util.Arrays.*;
import java.io.*;
import java.util.*;
/**
* 291 - The House Of Santa Claus
*/
class Main
{
private int total = 0;
private void solve(int[][] adj, int[] path, int index)
{
if (index == 8) {
for (int i = 1; i <= 8; i++) {
System.out.print(path[i]);
}
System.out.println("2");
total++;
return;
}
for (int i = 1; i < adj.length; i++) {
if (adj[path[index]][i] == 1) {
adj[path[index]][i] = 0;
path[index + 1] = i;
solve(adj, path, index + 1);
adj[path[index]][i] = 1;
} else if (adj[i][path[index]] == 1) {
adj[i][path[index]] = 0;
path[index + 1] = i;
solve(adj, path, index + 1);
adj[i][path[index]] = 1;
}
}
}
private void run()
{
int[][] adj = new int[6][6];
adj[1][2] = 1;
adj[1][3] = 1;
adj[1][5] = 1;
adj[2][3] = 1;
adj[2][5] = 1;
adj[3][4] = 1;
adj[3][5] = 1;
adj[4][5] = 1;
int[] path = new int[9];
path[1] = 1;
solve(adj, path, 1);
// System.err.println("total: " + total);
}
public static void main(String[] args)
{
new Main().run();
}
}
import static java.lang.Math.*;
import static java.util.Arrays.*;
import java.io.*;
import java.util.*;
/**
* 291 - The House Of Santa Claus
*/
class Main
{
public static void main(String[] args)
{
String[] solution = new String[] {"123153452",
"123154352",
"123451352",
"123453152",
"123513452",
"123543152",
"125134532",
"125135432",
"125315432",
"125345132",
"125431532",
"125435132",
"132153452",
"132154352",
"132534512",
"132543512",
"134512352",
"134512532",
"134521532",
"134523512",
"134532152",
"134532512",
"135123452",
"135125432",
"135215432",
"135234512",
"135432152",
"135432512",
"152134532",
"152135432",
"152345312",
"152354312",
"153123452",
"153125432",
"153213452",
"153254312",
"153452132",
"153452312",
"154312352",
"154312532",
"154321352",
"154325312",
"154352132",
"154352312"
};
for (int i=0; i<solution.length; i++)
{
System.out.println(solution[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment