Challenging myself. See you then on 16th august.
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
Challenging myself. See you then on 16th august.
Which is the best place to practice among these two and what strategy would you use to practice if you are at my level of coding.
Please don't say both are equal. In your sight any one must be having slight edge over the other. Please do discuss the strategy to practice(which must be little fast(Though I don't want to be perfect coder in a night, because that is not possible)). Your guidance will be really appreciated.
I am stuck with this backtracking problem. I think there is mistake in my recursive code (loops or recursive calls) but I could not make out what it is.
I my pasting my code. Help from any one will be appreciated.
VERSION 1:
public class Obstacle {
boolean[][] visited;
int bestAnswer = 0;
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
//**************PROGRAM STARTS HERE**********************
public int getLongestPath(int[] a){
visited = new boolean[5][5];
for(int i = 0; i < a.length; i++) visited[a[i] / 5][a[i] % 5] = true;
doit(0, 0, 0, 0); //move down
doit(0, 0, 1, 0); //move right
return bestAnswer;
}
boolean isSafe(int x, int y){
if(x < 0 || x >= 5 || y < 0 || y >= 5 || visited[x][y]) return false;
return true;
}
private void doit(int x, int y, int direction, int counts ){
if(!isSafe(x, y)) return;
visited[x][y] = true;
counts = counts + 1;
bestAnswer = Math.max(bestAnswer, counts);
//if safe to move in same direction move on
if(isSafe(x + dx[direction], y + dy[direction])) doit(x + dx[direction], y + dy[direction], direction, counts);
else{
//if moving direction is vertical before getting blocked then try moving left & right
if(direction % 2 == 0){
doit(x + dx[1], y + dy[1], 1, counts);
doit(x + dx[3], y + dy[3], 3, counts);
}else{ //if moving direction is horizontal before getting blocked then try moving up & down
doit(x + dx[1], y + dy[1], 0, counts);
doit(x + dx[2], y + dy[2], 2, counts);
}
}
//backtrack
visited[x][y] = false;
}
}
please do explain why my backtracking approach is not working.
Name |
---|