Last active
January 24, 2019 21:26
-
-
Save Romern/a659a1c0f8756381e9e9dd2183cbc603 to your computer and use it in GitHub Desktop.
https://www.youtube.com/watch?v=RGQe8waGJ4w Comment asked what would happen if you marked 2084 as visited
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
Normally: | |
Current Position: (510,477)=2084 | |
2084 visited at the beginning: | |
Current Position: (474,489)=2720 | |
Mark all blocking as visited: | |
Blocking numbers: | |
2084,2720,3325,3753,7776,5632,7411,8562,14076,8469,9231,22702,14661,21710,21078,25809,27112,24708,19844,26943,26737,32449,31366,45036,37853,37188,43318,62095,67401,68736,70848,62789,63223,69245,85385,52467,71072,68435,76611,84206,81869,70277,81475,83776,70767,84763,99029,82609,103815,86102,93729,100614,108039,82111,99935,85283,109993,119856,119518,116066,109686,92741,124770,92378,104657,125102,107267,107246,117089,117766,99295,121575,98930,117390,123583,112565,122080,111612,111597,97349,105002,130602,133509,153410,127138,143952,153326,157774,122534,136542,163038,134778,140186,162865,171044,159637,171041,174368,184225,152988,176535,171506,147883,172360,156132,179411,179238,175850,168569,195533,191278,168006,204000,171467,144410,211291,188859,213596,225332,212859,201021,240304,233933,183242,237743,219573,162013,230993,272187,255213,254682,208965,252154,241644,245595,265411,206168,217227,263507,275293,268475,264339,233353,303280,267891,221884,226639,255175,198031,260706,297759,320601,297687,288717,239650,216254,224715,281047,315480,249064,242129,308384,329687,285288,254093,324526,330268,260691,264794,268929,273096,277295,325082,284244,270494,305377,288524,285786,357239,352551,243562,307058,360853,242079,354836,262199,372368,333134,353636,371649,372949,363210,343563,256041,312571,385237,344369,374068,419535,381448,393911,282003,268330,393969,349971,364974,350607,424145,298733,427228,288377,300326,292687,292171,448411,324947,463354,283571,287850,438460,464628,385823,305817,467422,495930,332415,345209,352902,353524,337607,345791,502996,483451,494488,509364,374614,424571,429802,507215,516509,537602,370924,558347,471453,552362,573389,563589,562883,446364,398237,421306,378260,381944,382608,387575,579461,567976,453058,400752,574949,443675,425181,375788,513558,402664,409018,423245,454392,590121,467984,375181,605614,422553,567963,453038,605590,571789,422533,360606,541125,463873,634736,525072,448974,427749,528720,449664,643522,583942,461729,459044,554467,472743,485924,653258,459079,654825,555231,489398,677697,423813,451620,521407,671820,517577,697598,606277,446953,558569,694257,637842,463803,612514,593121,517781,624340,653915,575499,581586,715200,555910,735686,549948,669390,736507,538034,557402,593055,684965,677620,669371,697533,597681,591505,590756,801426,784455,711722,525679,586033,730432,575456,796030,515514,670965,712602,728684,785320,680829,833977,598409,603858,631407,753718,606202,642547,710871,850506,850479,829375,525646,519856,525643,578459,590623,645750,863450,860620,874637,852300,887772,867148,751917,621030,835720,767643,832063,817524,750199,884908,882106,832057,816560,913368,712510,602201,779932,697399,796235,771129,880188,726081,620981,576116,582207,643273,727800,695677,816875,949086,848585,711560,884857,795909,608405,813867,637609,806654,679075,685686,665056,641676,648103,767577,822891,942246,670862,678176,933516,600655,841136,682350,709961,739738,976550,736293,971526,628893,934452,713323,899933,798533,738023,777142,704783,859574,1028631,739454,696429,846637,854016,675718,1005398,757031,768429,821041,949013,1031644,919000,955795,1028594,1052072,830140,1081025,813778,793169,737075,842925,1022520,822847,800875,854933,880066,736221,1069581,952890,1078893,1087220,945070,797585,948960,811957,1108193,899903,1049983,934388,757736,864083,807182,1025479,736199,743082,972482,760431,1007291,912259,1152838,823611,1010321,1134671,856636,1166879,882770,890305,1126279,1022444,770926,852076,1199517,907478,854565,980366,1066376 |
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
import java.util.*; | |
public class TrappedKnight { | |
public enum Direction { | |
LEFT,RIGHT,UP,DOWN | |
} | |
public HashSet<Integer> visited; | |
int[][] matrix; | |
int size; //Size of matrix | |
public int xpos, ypos; | |
public TrappedKnight (int size) { | |
this.size = size; | |
xpos = size/2; | |
ypos = size/2; | |
visited = new HashSet<>(); | |
} | |
public static void main(String[] args) { | |
System.out.println("Normally:"); | |
TrappedKnight tk = new TrappedKnight(1000); | |
tk.preCompute(); | |
tk.makeStepUntilFail(); | |
System.out.println("Current Position: ("+tk.xpos+","+tk.ypos+")="+tk.matrix[tk.xpos][tk.ypos]); | |
System.out.println("2084 visited at the beginning:"); | |
TrappedKnight tk2 = new TrappedKnight(1000); | |
tk2.preCompute(); | |
tk2.visited.add(Integer.valueOf(2084)); | |
tk2.makeStepUntilFail(); | |
System.out.println("Current Position: ("+tk2.xpos+","+tk2.ypos+")="+tk2.matrix[tk2.xpos][tk2.ypos]); | |
System.out.println("Mark all blocking as visited:"); | |
TrappedKnight tk3 = new TrappedKnight(10000); | |
tk3.preCompute(); | |
HashSet<Integer> visitedUpdated = new HashSet<>(); | |
System.out.println("Blocking numbers: "); | |
while(true) { | |
tk3.xpos = tk3.size/2; | |
tk3.ypos = tk3.size/2; | |
int ret = tk3.makeStepUntilFail(); | |
visitedUpdated.add(Integer.valueOf(ret)); | |
tk3.visited = new HashSet<>(visitedUpdated); | |
System.out.print(ret+","); | |
//try { System.in.read(); } catch(Exception e) { } | |
} | |
} | |
public int makeStepUntilFail() { | |
int i = 1; | |
visited.add(Integer.valueOf(1)); | |
boolean step = true; | |
while (step) { | |
//drawOutput(10); | |
step = makeStep(); | |
//try { System.in.read(); } catch(Exception e) { } | |
} | |
return matrix[xpos][ypos]; | |
} | |
public int getMinIndex(int[] arr) { | |
int min = Integer.MAX_VALUE; | |
int ret = -1; | |
for (int i = 0; i< arr.length; i++) { | |
if (arr[i]<min) { | |
min = arr[i]; | |
ret = i; | |
} | |
} | |
return ret; | |
} | |
public int[] getStep() { | |
int[] choices = new int[8]; | |
choices[0] = matrix[xpos-1][ypos+2]; | |
choices[1] = matrix[xpos+1][ypos+2]; | |
choices[2] = matrix[xpos+2][ypos-1]; | |
choices[3] = matrix[xpos+2][ypos+1]; | |
choices[4] = matrix[xpos-1][ypos-2]; | |
choices[5] = matrix[xpos+1][ypos-2]; | |
choices[6] = matrix[xpos-2][ypos-1]; | |
choices[7] = matrix[xpos-2][ypos+1]; | |
for (int i = 0; i< 8; i++) { | |
if (visited.contains(Integer.valueOf(choices[i]))) { | |
choices[i] = Integer.MAX_VALUE; | |
} | |
} | |
int minIndex = getMinIndex(choices); | |
if (minIndex==-1) { | |
return new int[]{-1,-1}; | |
} | |
switch (minIndex) { | |
case 0: | |
return new int[]{xpos -1,ypos +2}; | |
case 1: | |
return new int[]{xpos +1,ypos +2}; | |
case 2: | |
return new int[]{xpos +2,ypos -1}; | |
case 3: | |
return new int[]{xpos +2,ypos +1}; | |
case 4: | |
return new int[]{xpos -1,ypos -2}; | |
case 5: | |
return new int[]{xpos +1,ypos -2}; | |
case 6: | |
return new int[]{xpos -2,ypos -1}; | |
case 7: | |
return new int[]{xpos -2,ypos +1}; | |
} | |
System.out.println(minIndex+"WHAT WHY"); | |
return new int[]{}; | |
} | |
public boolean makeStep() { | |
int[] step = getStep(); | |
if (step[0]==-1) { | |
return false; | |
} | |
xpos = step[0]; | |
ypos = step[1]; | |
visited.add(Integer.valueOf(matrix[xpos][ypos])); | |
return true; | |
} | |
public void drawOutput(int window) { | |
for (int y = ypos-window; y<ypos+window; y++) { | |
for (int x = xpos-window; x<xpos+window; x++) { | |
if (xpos == x && ypos == y) { | |
System.out.print(">"+matrix[x][y]+"<\t"); | |
} else { | |
System.out.print(matrix[x][y]+"\t"); | |
} | |
} | |
System.out.println(); | |
} | |
} | |
public void preCompute() { | |
int x = size/2; | |
int y = size/2; | |
matrix = new int[size][size]; | |
Direction cur = Direction.RIGHT; | |
int goForXSteps = 1; | |
boolean first = true; | |
int remainingSteps = 1; | |
for (int i = 1; x<size && y < size; i++) { | |
matrix[x][y] = i; | |
if (remainingSteps==0) { | |
if (first) { | |
first = false; | |
} else { | |
first = true; | |
goForXSteps +=1; | |
} | |
remainingSteps = goForXSteps; | |
switch (cur) { | |
case RIGHT: | |
cur = Direction.UP; | |
break; | |
case DOWN: | |
cur = Direction.RIGHT; | |
break; | |
case LEFT: | |
cur = Direction.DOWN; | |
break; | |
case UP: | |
cur = Direction.LEFT; | |
break; | |
} | |
} | |
switch (cur) { | |
case RIGHT: | |
remainingSteps--; | |
x++; | |
break; | |
case DOWN: | |
remainingSteps--; | |
y++; | |
break; | |
case LEFT: | |
remainingSteps--; | |
x--; | |
break; | |
case UP: | |
remainingSteps--; | |
y--; | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment