Last active
December 1, 2016 02:30
-
-
Save nareshdb/9c231c1cd9bd536d3d85141d863c3bc3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.google.challenges; | |
import java.util.*; | |
public class Answer { | |
public static int answer(int src, int dest) { | |
class cell{ | |
public int x,y,steps; | |
public boolean mask; | |
cell(int x, int y){ | |
this.x = x; | |
this.y = y; | |
this.steps = 0; | |
mask = true; | |
} | |
} | |
cell srcc = new cell(src/8, src%8); | |
cell destt = new cell(dest/8, dest%8); | |
if(destt.x == srcc.x && destt.y == srcc.y){ | |
return 0; | |
} | |
cell[][] cells = new cell[8][8]; | |
for(int i=0 ; i < 8 ; i++){ | |
for( int j=0;j<8 ; j++){ | |
cells[i][j] = new cell(i,j); | |
} | |
} | |
ArrayList<cell> sources = new ArrayList<cell>(); | |
int i ; | |
int j ; | |
sources.add(srcc); | |
for(int p=1;p<=6;p++){ | |
ArrayList<cell> temp = new ArrayList<cell>(); | |
for(int q=0;q<sources.size();q++){ | |
i = (sources.get(q)).x; | |
j = (sources.get(q)).y; | |
// right | |
if(i+2 <= 7){ | |
// top | |
if(j+1 <= 7){ | |
if((cells[i+2][j+1].mask) == true) | |
{(cells[i+2][j+1].steps) = p; | |
cells[i+2][j+1].mask = false;} | |
temp.add(cells[i+2][j+1]); | |
if(destt.x == i+2 && destt.y == j+1){ | |
return cells[i+2][j+1].steps; | |
} | |
} | |
// bottom | |
if(j-1 >=0){ | |
if((cells[i+2][j-1].mask) == true) | |
{(cells[i+2][j-1].steps) = p; | |
cells[i+2][j-1].mask = false;} | |
temp.add(cells[i+2][j-1]); | |
if(destt.x == i+2 && destt.y == j-1){ | |
return cells[i+2][j-1].steps; | |
} | |
} | |
} | |
// top | |
if(j+2 <= 7){ | |
// right | |
if(i+1 <= 7){ | |
if((cells[i+1][j+2].mask) == true) | |
{(cells[i+1][j+2].steps) = p; | |
cells[i+1][j+2].mask = false;} | |
temp.add(cells[i+1][j+2]); | |
if(destt.x == i+1 && destt.y == j+2){ | |
return cells[i+1][j+2].steps; | |
} | |
} | |
//left | |
if(i-1 >= 0){ | |
if((cells[i-1][j+2].mask) == true) | |
{(cells[i-1][j+2].steps) = p; | |
cells[i-1][j+2].mask = false;} | |
temp.add(cells[i-1][j+2]); | |
if(destt.x == i-1 && destt.y == j+2){ | |
return cells[i-1][j+2].steps; | |
} | |
} | |
} | |
// left | |
if(i-2 >= 0){ | |
// top | |
if(j+1 <= 7){ | |
if((cells[i-2][j+1].mask) == true) | |
{ | |
(cells[i-2][j+1].steps) = p; | |
cells[i-2][j+1].mask = false; | |
} | |
temp.add(cells[i-2][j+1]); | |
if(destt.x == i-2 && destt.y == j+1){ | |
return cells[i-2][j+1].steps; | |
} | |
} | |
// bottom | |
if(j-1 >=0){ | |
if((cells[i-2][j-1].mask) == true) | |
{(cells[i-2][j-1].steps) = p; | |
cells[i-2][j-1].mask = false;} | |
temp.add(cells[i-2][j-1]); | |
if(destt.x == i-2 && destt.y == j-1){ | |
return cells[i-2][j-1].steps; | |
} | |
} | |
} | |
// bottom | |
if(j-2 >= 0){ | |
// right | |
if(i+1 <= 7){ | |
if((cells[i+1][j-2].mask) == true) | |
{ | |
(cells[i+1][j-2].steps) = p; | |
cells[i+1][j-2].mask = false; | |
} | |
temp.add(cells[i+1][j-2]); | |
if(destt.x == i+1 && destt.y == j-2){ | |
return cells[i+1][j-2].steps; | |
} | |
} | |
// left | |
if(i-1 >= 0){ | |
if((cells[i-1][j-2].mask) == true) | |
{ | |
(cells[i-1][j-2].steps) = p; | |
cells[i-1][j-2].mask = false; | |
} | |
temp.add(cells[i-1][j-2]); | |
if(destt.x == i-1 && destt.y == j-2){ | |
return cells[i-1][j-2].steps; | |
} | |
} | |
} | |
} | |
// | |
sources.clear(); | |
sources = temp; | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment