- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 154 lines of Java from the credited upstream file ccc99s4.java.
- The implementation visibly relies on sequence storage.
- 9 loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 45678 910 111213 14import java.awt.*;15import hsa.*;16 17public class P4KnightDP18{19 static Console cc;20 static int maxr, maxc;21 static int m, ps, nps;22 static int [] [] b; 23 static Point [] p, np; 24 25 public static void main (String [] args)26 {27 cc = new Console ();28 TextInputFile fi = new TextInputFile ("knight.in");29 TextOutputFile fo = new TextOutputFile ("knight.out");30 int n, hpr, pc, kr, kc;31 boolean win, stalemate;32 33 n = fi.readInt ();34 for (int i = 1 ; i <= n ; i++)35 {36 maxr = fi.readInt ();37 maxc = fi.readInt ();38 hpr = fi.readInt () - 1;39 pc = fi.readInt () - 1;40 kr = fi.readInt () - 1;41 kc = fi.readInt () - 1;42 43 44 b = new int [maxr] [maxc];45 for (int r = 0 ; r < maxr ; r++)46 for (int c = 0 ; c < maxc ; c++)47 b [r] [c] = -1;48 b [kr] [kc] = 0;49 50 p = new Point [maxr * maxc];51 np = new Point [maxr * maxc];52 for (int r = 0 ; r < maxr * maxc ; r++)53 {54 p [r] = new Point ();55 np [r] = new Point ();56 }57 ps = 1;58 p [0].r = kr;59 p [0].c = kc;60 61 62 while (ps > 0)63 {64 nps = 0;65 for (int j = 0 ; j < ps ; j++)66 {67 newPoint (p [j].r + 1, p [j].c + 2, p [j].r, p [j].c);68 newPoint (p [j].r - 1, p [j].c + 2, p [j].r, p [j].c);69 newPoint (p [j].r - 2, p [j].c + 1, p [j].r, p [j].c);70 newPoint (p [j].r - 2, p [j].c - 1, p [j].r, p [j].c);71 newPoint (p [j].r - 1, p [j].c - 2, p [j].r, p [j].c);72 newPoint (p [j].r + 1, p [j].c - 2, p [j].r, p [j].c);73 newPoint (p [j].r + 2, p [j].c - 1, p [j].r, p [j].c);74 newPoint (p [j].r + 2, p [j].c + 1, p [j].r, p [j].c);75 }76 for (int j = 0 ; j < nps ; j++)77 {78 p [j].r = np [j].r;79 p [j].c = np [j].c;80 }81 ps = nps;82 }83 84 85 86 87 88 89 m = 1;90 win = false;91 92 for (int pr = hpr + 1 ; pr < maxr - 1 && !win ; pr++)93 {94 if (m >= b [pr] [pc] && b [pr] [pc] >= 0 && (m - b [pr] [pc]) % 2 == 0)95 {96 win = true;97 fo.println ("Win in " + m + " knight moves(s).");98 cc.println ("Win in " + m + " knight moves(s).");99 }100 m++;101 }102 103 if (!win)104 {105 106 107 108 109 110 111 112 m = 0;113 stalemate = false;114 for (int pr = hpr ; pr < maxr - 1 && !stalemate ; pr++)115 {116 if (m >= b [pr + 1] [pc] && b [pr + 1] [pc] >= 0 && (m - b [pr + 1] [pc]) % 2 == 0)117 {118 stalemate = true;119 fo.println ("Stalemate in " + m + " knight moves(s).");120 cc.println ("Stalemate in " + m + " knight moves(s).");121 }122 m++;123 }124 if (!stalemate)125 {126 fo.println ("Loss in " + (maxr - hpr - 2) + " knight moves(s).");127 cc.println ("Loss in " + (maxr - hpr - 2) + " knight moves(s).");128 }129 }130 }131 fo.close ();132 fi.close ();133 }134 135 136 public static void newPoint (int r, int c, int or, int oc)137 {138 if (r >= 0 && r < maxr && c >= 0 && c < maxc && b [r] [c] == -1)139 {140 b [r] [c] = b [or] [oc] + 1;141 np [nps].r = r;142 np [nps].c = c;143 nps++;144 }145 }146}147 148class Point149{150 public int r, c;151}152 153 154