[help]what' wrong with this?
Author |
Message |
chelseafc
|
Posted: Thu Jan 15, 2009 6:52 pm Post subject: [help]what' wrong with this? |
|
|
I attended the dwite contest today and when i was doing the 4th problem, there were some test cases that didn't work.
Which means there are some problems in my program. But I don't know where is wrong.
Can anyone help me? What's wrong with my program??
The question is here:
http://dwite.org/questions/shortest_path_around_v2.html
the test cases are here(the 2nd one doesn't work in my program):
http://dwite.org/home/testcase/81
My solution is here:
Java: |
import java.io.*;
import java.util.*;
public class D4 {
static int startX = - 1 ,startY = - 1, result = 999999;
public static void main (String[] args ) throws IOException {
Scanner fIn = new Scanner (new FileReader("DATA4.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("OUT4.txt"));
for(int i = 0 ; i < 1 ; i ++ ) {
result = 999999;
int[][] map = new int[8][8];
for(int j = 0 ; j < 8 ; j ++ ) {
String s = fIn. nextLine();
for(int m = 0 ; m < 8 ; m ++ ) {
char current = s. charAt(m );
if (current == '#') {
map [j ][m ] = - 999;
}
else if (current == 'A') {
startX = j;
startY = m;
map [j ][m ] = 0;
}
else if (current == 'B') {
map [j ][m ] = 1688;
}
else {
map [j ][m ] = 9999;
}
}
}
find (8 , map , startX,startY, 0);
pw. println(result );
}
pw. close();
}
private static void find (int dim, int[][] map, int x, int y, int count ) {
if (map [x ][y ] == 1688) {
if (count < result ) {
result = count;
}
return;
}
else {
map [x ][y ] = count;
}
if (x < dim - 1 && map [x+ 1][y ] > map [x ][y ]) {
find (dim, map, x+ 1 ,y , count+ 1);
}
if (x < dim - 1 && y <dim - 1 && map [x+ 1][y+ 1] > map [x ][y ]) {
find (dim, map, x+ 1 ,y+ 1 , count+ 1);
}
if (x > 0 && map [x- 1][y ] > map [x ][y ]) {
find (dim, map, x- 1 ,y ,count+ 1);
}
if (x > 0 && y > 0 && map [x- 1][y- 1] > map [x ][y ]) {
find (dim, map, x- 1 ,y- 1 ,count+ 1);
}
if (y < dim - 1 && map [x ][y+ 1] > map [x ][y ]) {
find (dim, map, x ,y+ 1 ,count+ 1);
}
if (x> 0 && y < dim - 1 && map [x- 1][y+ 1] > map [x ][y ]) {
find (dim, map, x- 1 ,y+ 1 ,count+ 1);
}
if (y > 0 && map [x ][y- 1] > map [x ][y ]) {
find (dim, map, x ,y- 1 ,count+ 1);
}
if (x< dim - 1 && y > 0 && map [x+ 1][y- 1] > map [x ][y ]) {
find (dim, map, x ,y- 1 ,count+ 1);
}
}
}
|
Although my solution is not an fast and dynamic way, let's not talk about that first. The only thing I want to know is why my program can't get the correct answer. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
DemonWasp
|
Posted: Fri Jan 16, 2009 10:10 am Post subject: RE:[help]what\' wrong with this? |
|
|
If nothing else, the last if in find() seems to be incorrect. The coordinates it tests are (x+1, y-1), but those it passes on are (x, y-1). It looks like just a copy-paste error.
I'm also curious as to what the first for-loop in main() is there for, given as it executes exactly once. |
|
|
|
|
 |
chelseafc
|
Posted: Fri Jan 16, 2009 4:12 pm Post subject: RE:[help]what\' wrong with this? |
|
|
oh,damn it ,that's the mistake...
the last if is wrong!!!!What a silly mistake I had made!!!!
the for loop is 5 when I submit the solution...I changed it to 1 then to see where is wrong... |
|
|
|
|
 |
saltpro15

|
Posted: Fri Jan 16, 2009 7:15 pm Post subject: RE:[help]what\' wrong with this? |
|
|
I think DemonWasp nailed it, because the code is otherwise correct. |
|
|
|
|
 |
|
|