Posts

(SPOJ) f91 - Solution 2

Two years ago, I solved this problem using the language C (the only solution using C in this blog). You can check the previous solution here . Although the solution could be as simple as: If n <= 100, print 91 If n >= 101, print n-10 This solution calculates the answer through the given recursion, which is: If n <= 100, do f91(n) = f91(f91(n+11) If n >= 101, do f91(n) = n-10 import java.io.*; class Main  {     public static int f91(int n) {         if (n >= 101) {             return n-10;         }                 return f91(f91(n+11));     }         public static int reader(BufferedReader br) throws NumberFormatException, IOException {           ...

(UVA) Il Gioco dell'X - Solution

For this solution I used Depth-First Search. import java.io.*; import java.util.*; class Main  {     public static char[][] matrix;     public static boolean[][] visited;     public static int[] vi = {-1,-1,0,0,1,1};     public static int[] vj = {-1,0,-1,1,0,1};     public static int length;     public static boolean blackWon;         public static void dfs(int currI, int currJ) {         if (currI < 0 || currJ < 0 || currI >= length || currJ >= length || matrix[currI][currJ] == 'w' || visited[currI][currJ] == true) {             return;         }                 if (currI == length-1) {             blac...

(UVA) Popes - Solution 2

If you want to see another solution for this problem, click here . For this solution I used Binary Search. The complexity of this solution is O(N log N), which is better than the previous solution. import java.io.*; import java.util.*; class Main  {     public static int[] electionYears;     public static int lastYearPeriod;         public static int binSearch(int lo, int hi) {         if (lo > hi) {             return hi;         }                 int mi = (lo+hi)/2;         if (electionYears[mi] > lastYearPeriod) {             return binSearch(lo, mi-1);         }      ...

(UVA) Popes - Solution 1

For this problem we had to use Binary Search to find the answer. Firstly, I tried to solve it without using it; however, the complexity of such solution was O(N^2). import java.io.*; import java.util.*; class Main  {     public static void process() throws NumberFormatException, IOException {           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 String line = br.readLine();         while (line != null) {             if (line.equals("")) {                 line = br.readLine();             }                  ...

(UVA) Subway - Solution

I used Dijkstra's algorithm to solve this problem. First, I read the entries, and associate every type of coordinate to a type, for example: Coordinate of home: type 0 Coordinate of school: type 1 Coordinate of the first subway: type 2 Coordinate of the second subway: type 3 And so on... These types will help me to see if I am walking (10km/h) or taking a subway (40km/h). Then, during the Dijkstra method, I need to check the types of every two different coordinates to evaluate the situation. import java.io.*; import java.util.*; import java.lang.*; class Main  {     public static HashMap<Integer, Coord> map;     public static ArrayList<Type> listCoords;         public static double time(Coord c1, Coord c2, int v) {         return (Math.sqrt((c2.x-c1.x)*(c2.x-c1.x)+(c2.y-c1.y)*(c2.y-c1.y))*60)/v;     }         public st...

(UVA) Deciding Victory in Go - Solution

I used Depth-First Search to solve this problem. import java.io.*; import java.util.*; class Main  {     public static char[][] matrix;     public static boolean[][] visited;     public static int[] vi = {-1,1,0,0};     public static int[] vj = {0,0,-1,1};     public static final int LENGTH = 9;     public static int numX;     public static int numO;     public static int countPoint;         public static void dfs(int currI, int currJ) {         if (currI < 0 || currJ < 0 || currI >= LENGTH || currJ >= LENGTH || visited[currI][currJ] == true || matrix[currI][currJ] != '.') {             if (currI >= 0 && currJ >= 0 && currI < LENGTH && currJ < LENGTH) {        ...

(UVA) Pick Up Sticks - Solution

I used Topological Sorting to solve this problem. import java.io.*; import java.util.*; class Main  {     public static HashMap<Integer, ArrayList<Integer>> adjListOut;     public static int[] degrees;         public static ArrayList<Integer> topoSort(int numSticks) {         Queue<Integer> zeroDegree = new ArrayDeque<Integer>();         for (int i = 1; i <= numSticks; i++) {             if (degrees[i] == 0) {                 zeroDegree.add(i);             }         }                 ArrayList<Integer> seq = new ArrayList<Int...