Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge (Out-of-Date)
New Posts  All Forums:Forum Nav:

Programming Challenge (Out-of-Date) - Page 24  

Poll Results: Are you interested in participating in and/or helping organise and post these programming challenges?

 
  • 100% (2)
    I want to participate.
  • 0% (0)
    I want to contribute by helping posting and organise these challenges.
  • 0% (0)
    I'll only take part if other people are willing to participate.
  • 0% (0)
    I can help and participate - I love programming!
  • 0% (0)
    I do not wish to participate or help.
2 Total Votes  
post #231 of 306
For everyday applications on windows (i.e. those that don't require highly optimized C/C++) C# is awesome.
    
CPUMotherboardOSMonitor
2500k P8P67 Windows 7 Ultimate x64 22" phillips 
KeyboardPowerMouseMouse Pad
Cheap Logitech Antec Earthwatts 650W Razer Deathadder Razer eXactMat 
  hide details  
    
CPUMotherboardOSMonitor
2500k P8P67 Windows 7 Ultimate x64 22" phillips 
KeyboardPowerMouseMouse Pad
Cheap Logitech Antec Earthwatts 650W Razer Deathadder Razer eXactMat 
  hide details  
post #232 of 306
I've recently had to do some ASP.net and it was pretty rough. I did some stuff in mono a long time ago. Their default time/date stuff blows java's out of the water iirc.

Maybe it's all the capital letters.. damn self important properties.. treating themselves like proper names.
post #233 of 306
Here is the new one the problem writeup is long but it should not be too difficult.

Quote:
Problem B -- SameGame Simulation -- Filename SAMEGAME

The one-player game of SameGame is played on an M row by N column rectangular grid. In each cell of the grid is placed a positive integer in the range 0 through 9. The goal of the game is to remove all the integers from the grid. The player attempts this by repeatedly selecting a cell for removal. Each time a cell is selected for removal, all the cells in the connected region (defined below) containing the same integer found in selected cell are removed, and all cells above those that were removed 'drop down' (toward the bottom of the grid). When all the cells in a column have been removed, then columns to the right of the removed column slide to the left. The game is over when all cells are removed (a win), or when no more cells can be removed. A region can only be removed if it contains at least two cells.
A connected region consists of all cells that can be reached by moving horizontally (left or right) and/or vertically (up or down) from any cell in the region, subject to the restriction that all cells in the connected region must contain the same value.
Cells will be numbered starting with the lower left corner of the grid; this is cell (1,1). The cell above it is cell (2,1), and the cell to its right is cell (1,2).
Consider this small (3 by 5) example:
1 3 5 2 2
2 2 3 5 1
1 2 3 5 5

The cells at (1,1), (2,5), (3,1) (3,2) and (3,3) may not be successfully selected for removal, since they aren't parts of connected regions (i.e., regions that contain at least two connected cells with the same value). The cell at (2,1) is part of the connected region also containing the cells at (2,2) and (1,2). Likewise, the connected region containing the cell at (1,5) also contains the cells at (1,4) and (2,4), but not the cell at (3,3). Starting with the original grid shown above, the following selections will result in a win:
Code:
Step 1. Select cell (3,5):

1 3 5     
2 2 3 5 1 
1 2 3 5 5 

Step 2. Select cell (2,2):

    5     
1   3 5 1 
1 3 3 5 5 


Step 3. Select cell (1,2):

          
1   5 1   
1 5 5 5   

Step 4. Select cell (1,2):

          
1         
1 1
Finally, in step 5, selecting cell (1,1), (1,2) or (2,1) will remove the remaining integers from the grid.
In the input for this problem your program will be presented with a sequence of grids, each having no more than 10 rows and 40 columns. For each grid there will also be given a sequence of cell removal selections. Apply these selections, in order, to each grid, ignoring those that are not permitted (e.g. they select non-existant cells, or they select regions with fewer than two cells). Then display the resulting grid or, if appropriate, the message "Game Won".
Input

The input will consist entirely of non-negative integers without regard to line structure. Each grid and sequence of removal selections will begin with values for M and N. If either of these values is zero, then the input is terminated.
Following M and N will appear the MxN integers for the grid, in row major order. That is, the values are given in order for cells (1,1), (1,2), ..., (1,N), (2,1), ..., (M,N). Following the grid data will appear pairs of integers, each pair indicating the row and column of a grid cell selected for removal. The end of this sequence will be marked by a pair of zeroes. If a game is won, your program must skip any remaining pairs of integers (if any) through and including the pair of zeroes to reach the data for the next grid in the input.
Output

As noted above, the output for each grid in the input data should be either the grid that remains after considering all selections, or the message 'Game Won'. Precede the output for each grid by its sequence in the input; the first grid is numbered 1.
Sample solution

Example Input


3 5
1 2 3 5 5
2 2 3 5 1
1 3 5 2 2
3 5
2 2
1 2
1 2
1 1
0 0

3 5
1 2 3 5 5
2 2 3 5 1
1 3 5 2 2
2 2 1 2 1 4 1 2
99 99 0 0

4 3
1 4 4
4 4 2
1 2 3
3 1 3
1 2 1 1 1 3 1 1 0 0
0 0



Expected Output
Code:
Grid 1.
    Game Won

Grid 2.
              
    1   2     
    1 2 1     

Grid 3.
    Game Won

Edited by Midpipps - 2/4/11 at 2:05pm
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
post #234 of 306
Code:
import java.io.*;
import java.util.*;

public class samegame2{
    public static class Mask extends board{
    
        public Mask(int i, int j){
            super(i,j);
            }
        public void set(int i, int j){
            Vector v = (Vector)rows.get(i);
            v.set(j, new Integer("1"));
            }
        public int size(){
            int size = 0;
            for (int x=rows.size() -1; x >= 0; x--){
                Vector v = (Vector)rows.get(x);
                for (int y=v.size() -1; y>= 0; y--)
                    if ((Integer)v.get(y) == 1)
                        size++;
                }
            return size;
            }
        }
    public static class board{
        Vector rows;
        int x,y;
        
        public board(){
            this(0,0);
            }
        public board(int i, int j){
            rows = new Vector(i,1);
            for (int x=0; x<i; x++){
                Vector v = new Vector(j,1);
                rows.add(v);
                for (int y=0; y<j; y++)
                    v.add(new Integer(0));
                }
            x=i;
            y=j;
            }

        public void set(int i, int j, int val){
            Vector v = (Vector)rows.get(i);
            v.set(j, new Integer(val));
            }

        public int get(int i, int j){
            Vector v = (Vector)rows.get(i);
            if (v.size() <= j)
                return 0;
            Integer I = (Integer)v.get(j);
            return I.intValue();
            }

        public void remove1(int i, int j){
            Vector v = (Vector)rows.get(i);
            v.removeElementAt(j);
            }

        public void rremove(int i, int j, Mask mask){
            int I = get(i,j);
            mask.set(i,j);

            int top = j;
            int bottom = j;
            Vector v = (Vector)rows.get(i);
            while (top < v.size() -1)
                if (I == get(i,top + 1))
                    top +=1;
                else
                    break;
            while (bottom > 0)
                if (I == get(i,bottom -1))
                    bottom -= 1;
                else
                    break;
            for (int m=top; m >=bottom; m--){
                if (mask.get(i,m) == 0)
                    rremove(i,m,mask);
                }
            
            int left = i;
            int right = i;
            while (left > 0)
                if (I == get(left -1,j))
                    left -= 1;
                else
                    break;
            while (right < rows.size() -1)
                if (I == get(right +1,j))
                    right += 1;
                else
                    break;
            for (int m=left;  m<=right; m++){
                if (mask.get(m,j) == 0)
                    rremove(m,j, mask);
                }
            }
            
        public int remove(int i, int j){
            Mask mask = new Mask(x,y);
            rremove(i,j, mask);

            int _size = 0;
            for (int x=rows.size() -1; x >= 0; x--){
                Vector v = (Vector)rows.get(x);
                _size += v.size();
                for (int y=v.size() -1; y>= 0; y--)
                    if (mask.get(x,y) == 1){
                        if (mask.size() > 1){
                            remove1(x,y);
                            _size -= 1;
                            }
                        }
                if (v.size() == 0)
                    rows.remove(x);
                }
            return _size;
            }

        public String toString(){
            StringBuffer sb =new StringBuffer();
            int max=0;
            for (int i=0; i< rows.size(); i++){
                Vector v = (Vector)rows.get(i);
                if (v.size() > max)
                    max = v.size();
                }
            while (--max >= 0){
                for (int i=0; i< rows.size(); i++){
                    Vector v = (Vector)rows.get(i);
                    if (v.size() > max)
                        sb.append(get(i,max) + " ");
                    else
                        sb.append("  ");
                    }
                sb.append("\
");
                }
            return sb.toString();
            }
        }
    public static void main(String[] args){
        try {
            char c;
            int C;
            LinkedList ll = new LinkedList();
            int lli = 0;
            int grid = 1;
            StringBuffer sb = new StringBuffer();
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            while ((C = in.read()) != -1){
                c = (char)C;
                if (c >= '0' && c <= '9')
                        sb.append(c);
                else {
                    if (sb.length() >0)
                        ll.add(new Integer(sb.toString()));
                        sb.setLength(0);
                    }
                }

            while (lli < ll.size() -3){
                int x = (Integer)ll.get(lli++);
                int y = (Integer)ll.get(lli++);
                if (x == 0 || y == 0)
                    continue;

                System.out.println("Grid " + grid++ + ".");    
                board b = new board(y,x);
                for (int i=x; i>=1; i--)
                    for (int j=1; j<= y; j++)
                        b.set(j-1, i-1, (Integer)ll.get(lli++));

                System.out.println(b);
                System.out.println();
                int x2, y2;
                int ret = 0;
                while (true){
                    x2 = (Integer)ll.get(lli++);
                    y2 = (Integer)ll.get(lli++);
                    if (x2 == 0 || y2 == 0)
                        break;
                    if (x2 >x || y2 > y)
                        continue;

                    ret = b.remove(y2-1,x2-1);
                    System.out.println(b);
                    System.out.println();
                    if (ret == 0){
                        System.out.println("Game Won");
                        break;
                        }
                    }
                if (ret > 0)
                    System.out.println(b);
                    
                while (x2 !=0 || y2 != 0){
                    x2 = (Integer)ll.get(lli++);
                    y2 = (Integer)ll.get(lli++);
                    }
                }
            }
        catch (Exception e){
            e.printStackTrace();
            }
        }
    }
Code:
Grid 1.
1 2 3 5 5
2 2 3 5 1
1 3 5 2 2


1 2 3
2 2 3 5 5
1 3 5 5 1


    3
1   3 5 5
1 3 5 5 1


1   3
1 3 3 1


1
1 1




Game Won

Edited by wimcle - 2/2/11 at 10:00pm
water damage
(7 items)
 
  
CPUMotherboardGraphicsRAM
2600k 4.5Ghz Asus p8p67 deluxe evga gtx980ti classified SLI 16G ddr3 1600 
Hard DriveOSMonitor
Samsung 850pro x2 Windows 7 Seiki sm40unp 
  hide details  
water damage
(7 items)
 
  
CPUMotherboardGraphicsRAM
2600k 4.5Ghz Asus p8p67 deluxe evga gtx980ti classified SLI 16G ddr3 1600 
Hard DriveOSMonitor
Samsung 850pro x2 Windows 7 Seiki sm40unp 
  hide details  
post #235 of 306
Quote:
Originally Posted by wimcle View Post
I think your example input needs some work... for example grid 3 has one '2' in it, it can never win.

Grid 3 has 2 twos in it.
1 4 4
4 4 2
1 2 3
3 1 3

But I do see the problem in the example the tabs were not kept should have used code instead of quote the 5 in the example problem should be moved over one spot fixing now
Edited by Midpipps - 2/2/11 at 9:43pm
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
post #236 of 306
Quote:
Originally Posted by Midpipps View Post
Grid 3 has 2 twos in it.
1 4 4
4 4 2
1 2 3
3 1 3

But I do see the problem in the example the tabs were not kept should have used code instead of quote the 5 in the example problem should be moved over one spot fixing now
been typeing on this two long, going blind
water damage
(7 items)
 
  
CPUMotherboardGraphicsRAM
2600k 4.5Ghz Asus p8p67 deluxe evga gtx980ti classified SLI 16G ddr3 1600 
Hard DriveOSMonitor
Samsung 850pro x2 Windows 7 Seiki sm40unp 
  hide details  
water damage
(7 items)
 
  
CPUMotherboardGraphicsRAM
2600k 4.5Ghz Asus p8p67 deluxe evga gtx980ti classified SLI 16G ddr3 1600 
Hard DriveOSMonitor
Samsung 850pro x2 Windows 7 Seiki sm40unp 
  hide details  
post #237 of 306
Quote:
Originally Posted by wimcle View Post
A kinda verbose java one
I think they are all going to be verbose.
post #238 of 306
A test case for regions:
all the 2s should drop out
Code:
4 5
2 2 2 1 1
2 1 2 2 1
1 2 1 2 2
2 2 2 2 1
2 4
1 1
0 0
water damage
(7 items)
 
  
CPUMotherboardGraphicsRAM
2600k 4.5Ghz Asus p8p67 deluxe evga gtx980ti classified SLI 16G ddr3 1600 
Hard DriveOSMonitor
Samsung 850pro x2 Windows 7 Seiki sm40unp 
  hide details  
water damage
(7 items)
 
  
CPUMotherboardGraphicsRAM
2600k 4.5Ghz Asus p8p67 deluxe evga gtx980ti classified SLI 16G ddr3 1600 
Hard DriveOSMonitor
Samsung 850pro x2 Windows 7 Seiki sm40unp 
  hide details  
post #239 of 306
Quote:
Originally Posted by wimcle View Post
A test case for regions:
all the 2s should drop out
Code:
4 5
2 2 2 1 1
2 1 2 2 1
1 2 1 2 2
2 2 2 2 1
2 4
1 1
0 0
nice that is actually a good idea.
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
post #240 of 306
It is a little convoluted but hear it is. It is very breakable but it works well I think the revursive call was the funnest part everything else was just fluff

Code:
import java.util.Scanner;
import java.util.Vector;

public class SameGame {
private static Vector <Vector<Integer>> mColumnV; //made global just so that I did not have to pass it all the time
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int rows = in.nextInt();
int columns = in.nextInt();
//get the vectors ready 0's are just place holders till they are filled this is so we can put them in reverse order.
mColumnV = new Vector<Vector<Integer>>(columns);
for (int i = 0; i < columns; i++)
mColumnV.add(i, new Vector<Integer>(rows));

//things will automagically fall into place :) just need to flip it for output
for (int r = 0; r < rows; r++)
for (int c = 0; c < mColumnV.size(); c++){
int tempNumberRead = in.nextInt();
mColumnV.get(c).add(r, tempNumberRead);
}

int r = -1;
int c = -1;
while ( (r = in.nextInt()) != 0 && (c = in.nextInt()) != 0){
r = r - 1;
c = c - 1;

if(c < mColumnV.size()  && c >= 0){
removeThisPlusNeighbors(r, c, -1, new Vector<String>(), 0);//uses -1 as a key to know the first entrance to the function the zero is going to be used as a counter
//look for 0's if there are some remove them :) the map will collapse
for (int i = 0; i < mColumnV.size(); i ++)
while (mColumnV.get(i).contains(0))
mColumnV.get(i).remove(mColumnV.get(i).indexOf(0));
//look for any empty vectors to collapse the map again
for (int i = mColumnV.size() - 1; i >= 0; i--)
if (mColumnV.get(i).size() == 0)
mColumnV.remove(i);
}
}
//end game
if (mColumnV.size() > 0){
System.out.println("You loose");
printGame(rows);
}
else
System.out.println("Game Won");
}

public static void printGame(int rows){
String output = "";
for (int a = rows - 1; a >= 0; a--){
for (int i = 0; i < mColumnV.size() ; i++){
if( a < mColumnV.get(i).size())
output += mColumnV.get(i).get(a) + " ";
else
output += "  ";
}
output += "\
";
}
System.out.println(output);
}

public static int removeThisPlusNeighbors(int r, int c, int numberBeingChecked, Vector<String> previouslyChecked, int toBeZeroed){
previouslyChecked.add("" + r + "," + c + ""); //the vector is to keep track so that we do not remove something we have already been to. 
if (numberBeingChecked == -1)
numberBeingChecked = mColumnV.get(c).get(r);

if (numberBeingChecked == mColumnV.get(c).get(r))
{
toBeZeroed++;
//check right
if (c - 1 >= 0   && c < mColumnV.size() && !previouslyChecked.contains("" + (r) + "," + (c - 1) + ""))
if (r < mColumnV.get(c - 1).size())
toBeZeroed = removeThisPlusNeighbors(r, c-1, numberBeingChecked, previouslyChecked, toBeZeroed);
//check left
if (c + 1 < mColumnV.size() && c >= 0 && !previouslyChecked.contains("" + (r) + "," + (c + 1) + ""))
if (r < mColumnV.get(c + 1).size())
toBeZeroed = removeThisPlusNeighbors(r, c+1, numberBeingChecked, previouslyChecked, toBeZeroed);
//check down
if (r - 1 >= 0 && !previouslyChecked.contains("" + (r - 1) + "," + (c) + ""))
toBeZeroed = removeThisPlusNeighbors(r - 1, c, numberBeingChecked, previouslyChecked, toBeZeroed);
//check up
if (r + 1 < mColumnV.get(c).size() && !previouslyChecked.contains("" + (r + 1) + "," + (c) + ""))
toBeZeroed = removeThisPlusNeighbors(r + 1, c, numberBeingChecked, previouslyChecked, toBeZeroed);
// 0 is a locator to show us where there are items to be removed only zero things out if we have more then 2 to be zeroed
if (toBeZeroed > 1)
mColumnV.get(c).set(r, 0);
}
return toBeZeroed;
}
}
Code:
run 1:  game won
run 2: You loose
      
1   2 
1 2 1 

run 3:
game won

Test Data from wimcle:
game won


Decided to do spoiler tags that way people won't have to see code if they want a clean look at the problem. Without being tainted by others code.
Edited by Midpipps - 2/7/11 at 5:52pm
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
This thread is locked  
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge (Out-of-Date)