The ‘I-Game’: Answer

Hey guys!
Hope you can remember my previous post ” The ‘I-Game’ “ . In this post I like to share an answer for that problem. There can be many solutions for the problem but I found this neat piece of code while surfing the internet. Anyone whose interested in other problems & answers they can visit http://www.migids.com/ .  I have tested the code for the sample input and worked fine.

Enjoj ! 🙂


import java.util.Scanner;

public class ProblemS {

    static char[][] grid = new char[10][10];

    public static void main(String[] args) {

        // "turn off lights"
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                grid[i][j] = '.';
            }
        }

        String line = "";
        Scanner console = new Scanner(System.in);

        try {
            while (console.hasNextLine()) {
                line = console.nextLine();
                if (line == null || line.isEmpty()) {
                    break;
                }
                int[] coords = new int[2];
                coords[0] = Integer.parseInt(line.split(" ")[0]);
                coords[1] = Integer.parseInt(line.split(" ")[1]);

                // invert the coords as in document
                push(coords[1], coords[0]);
            }
        } catch (Exception e) {
        }

        // output the grid
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j <= 8; j++) {
                System.out.print(grid[i][j] + " ");
            }
            System.out.println(grid[i][9]);
        }
    }
    /**
     * Pushes the screwdriver on the target coordinates
     *
     * @param x
     * @param y
     */

    private static void push(int x, int y) {

        sw(x, y);
        sw(x - 1, y - 1);
        sw(x - 1, y);
        sw(x - 1, y + 1);
        sw(x + 1, y - 1);
        sw(x + 1, y);
        sw(x + 1, y + 1);
    }
    /**
     * Switches the light
     */
    private static void sw(int x, int y) {

        // check bound
        if (x > 10 || x < 0 || y > 10 || y < 0) {
            return;
        }
        if (grid[x][y] == '.') {
            grid[x][y] = 'X';
        } else {
            grid[x][y] = '.';
        }
    }
}

Advertisements

The ‘I-Game’

You all know that IEEEXtreme 4.0 finished recently. I thought of publishing some of the problems given at the competition. Give it a try if you have a little time. Most of the questions are really cool. Try to develop algorithms to solve the questions. I’ll try to publish all the questions whenever possible.

Here’s The problem called ‘I-Game’ straight from Xtreme 4.0 (Hope I’m not violating any copyright laws) Have Fun Coding….

Continue reading “The ‘I-Game’”

IEEEXtreme 4.0 @ MORA

ieeextreme 4.0

IEEEXtreme 4.0 organized by the IEEE concluded successfully today (2010-08-24) morning at 0530h local time. IEEEXtreme is a global challenge in which teams of IEEE student members compete in a 24-hour time span against each other to solve a set of programming problems.

The competition started on 2010-08-24 at 0530h local time (00:00 GMT). This year 37 teams registered for the competition from University of moratuwa. The team “beta” (Members: Varuna Parinda Jayasiri, Chethiya Abesinghe, Lalintha Gayan Polpitiya) has managed to secure the 11th place of the competition this year. Also the teams “Selachimorpha” and “SharpSharks” managed to reach top 50 in the competition.

Hats off for all the teams for the great effort they have given. Coding algorithms for 24 hours straight is not an easy task as it seems. The IEEE Student branch of University of Moratuwa was there with all the participants in the university giving them an immense support providing necessary facilities. So a BIG “THANK YOU” goes to them.

It was a great opportunity for me to take part in the organizing process of an event with that magnitude. And hopefully I’ll be and Xtremer for IEEEXtreme 5.0 !!! 😀