Dwite round 2 problem 3
Author |
Message |
MagicIdiot
|
Posted: Thu Nov 25, 2010 12:36 am Post subject: Dwite round 2 problem 3 |
|
|
Aww I got 4/5 on it.
For the last test case,
input:
......6.
.6.....3
...#4.75
7......2
..9.....
.....53.
.8.6....
4..5.9..
--------
My output: 33 (correct answer is 41)
In Java:
code: |
import java.awt.*;
import java.io.*;
public class EscapeAndLoot
{
public static void main (String[] args) throws IOException
{
BufferedReader input = new BufferedReader (new FileReader ("DATA3.txt"));
PrintWriter output = new PrintWriter (new FileWriter ("OUT3.txt"));
String line;
char[] [] grid = new char [8] [8];
int r = 8;
int c = 0;
int MAXCOL = 8;
for (int a = 0 ; a < 5 ; a++)
{
for (int i = 0 ; i < 8 ; i++)
{
line = input.readLine ();
for (int j = 0 ; j < 8 ; j++)
grid [i] [j] = line.charAt (j);
}
output.println (recurse (grid, r - 1, c));
line = input.readLine ();
}
input.close ();
output.close ();
}
public static int recurse (char grid[] [], int row, int col)
{
if (row == 0 && col == 7)
{
if (grid [row] [col] != '#' && grid [row] [col] != '.')
return Integer.valueOf (grid [row] [col] + "").intValue (); // To int
else
return 0;
}
if (row - 1 < 0 || col + 1 >= 8)
return 0;
int num;
if (grid [row] [col] != '#' && grid [row] [col] != '.')
num = Integer.valueOf (grid [row] [col] + "").intValue (); // To int
else
num = 0;
if (grid [row - 1] [col] == '#' && grid [row] [col + 1] == '#')
return num;
else if (grid [row - 1] [col] == '#')
return num + recurse (grid, row, col + 1);
else if (grid [row] [col + 1] == '#')
return num + recurse (grid, row - 1, col);
else
{
int a, b;
a = num + recurse (grid, row - 1, col);
b = num + recurse (grid, row, col + 1);
if (a > b)
return a;
else
return b;
}
}
}
|
(How do I make it highlight syntax?) |
|
|
|
|
|
Sponsor Sponsor
|
|
|
A.J
|
Posted: Thu Nov 25, 2010 12:44 am Post subject: Re: Dwite round 2 problem 3 |
|
|
Well, since you didn't ask any question as such, I can only assume you are wondering why the answer is 41. The following path yields 41 (one of two paths, I believe):
......6#
.6.....#
...#4.##
7.....#2
..9...#.
.....##.
.#####..
##.5.9..
4 + 8 + 6 + 5 + 3 + 7 + 5 + 3 = 41 |
|
|
|
|
|
MagicIdiot
|
Posted: Thu Nov 25, 2010 7:43 am Post subject: Re: Dwite round 2 problem 3 |
|
|
Whoops, I was in a rush and forgot to ask, what am I missing or doing wrong in my code? |
|
|
|
|
|
A.J
|
Posted: Thu Nov 25, 2010 4:18 pm Post subject: RE:Dwite round 2 problem 3 |
|
|
When it comes to debugging code, the person who coded up the program has a the best chance of catching the mistake, as only they know everything that is going on in their code. Having said that, the error would have occurred anywhere. Try debugging it yourself (since you already know what testcase it is failing, try looking at all the intermediate steps). I can`t run it for you at home and figure out what it is doing as I don`t have a Java compiler, nor do I have the time (sorry). |
|
|
|
|
|
|
|