RB
BAN USERBelow code reads a maze and and its walls from a text file. The text file contains entire maze in the format of 0's and 1's.
00000000000000000
01111111111101110
01000101000001010
01011101111111010
01010001000101000
01011101011101110
01000101010100000
01011101110101110
01010000010001000
01010111011101110
01010101000001000
01011101111111010
01110100010000010
01110111011111010
00010000000101010
01111111111101010
00000000000000000
While 0's represent wall/obstacles, 1's represent a path. One can define another class for MazeCellType where type can be a path and a wall
private void CreateDungeon() {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(Constants.dungeonFile));
String line;
int lineNumber = 0;
while ((line = reader.readLine()) != null && !line.isEmpty()
&& lineNumber < this.sizeOfMaze) {
int columnNumber = 0;
String[] paths = line.split("0");
for (int i = 0; i < paths.length; i++) {
String path = paths[i];
int j = 0;
while (j < path.length() && columnNumber < this.sizeOfMaze) {
this.dungeon[lineNumber][columnNumber].type = Path;
columnNumber++;
j++;
}
if (columnNumber < this.sizeOfMaze) {
this.dungeon[lineNumber][columnNumber].type = Wall;
columnNumber++;
}
}
lineNumber++;
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
//eat it
}
}
}
}
public void TargetSumMethod(int []numArray, int target)
{
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
int lookup;
for(int i=0; i<numArray.length; i++)
hm.put(numArray[i], i);
for(int i=0; i<numArray.length; i++)
{
lookup = target - numArray[i];
if((hm.get(numArray[lookup])) != null)
{
System.out.println(numArray[i] + "+" + lookup );
}
}
}
This should be in O(n)
- RB July 01, 2012
I have tried running this
- RB July 03, 2012It gives following output
0,5
1,4
2,3
3,2
4,1
5,0