- 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
- 127 lines of Java from the credited upstream file ccc08s5.java.
- The implementation visibly relies on sequence storage.
- 10 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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 60import java.awt.*;61import hsa.*;62 63public class CCC2008S5NukitDP64{65 static Console cc;66 67 static boolean[] [] [] [] winningPosition;68 static int[] [] moves = {{2, 1, 0, 2}, {1, 1, 1, 1}, {0, 0, 2, 1}, {0, 3, 0, 0}, {1, 0, 0, 1}};69 70 71 72 73 74 75 static boolean loosingPosition (int a, int b, int c, int d)76 {77 if (a < 0 || b < 0 || c < 0 || d < 0)78 return false;79 else80 return !winningPosition [a] [b] [c] [d];81 }82 83 84 public static void main (String[] args)85 {86 int n, a, b, c, d;87 cc = new Console ();88 TextInputFile f = new TextInputFile ("s5.4.in");89 90 winningPosition = new boolean [31] [31] [31] [31];91 92 93 for (int i = 0 ; i < 31 ; i++)94 for (int j = 0 ; j < 31 ; j++)95 for (int k = 0 ; k < 31 ; k++)96 for (int l = 0 ; l < 31 ; l++)97 winningPosition [i] [j] [k] [l] = false;98 99 100 101 102 for (int i = 0 ; i < 31 ; i++)103 for (int j = 0 ; j < 31 ; j++)104 for (int k = 0 ; k < 31 ; k++)105 for (int l = 0 ; l < 31 ; l++)106 for (int m = 0 ; m < 5 ; m++)107 if (loosingPosition (i - moves [m] [0], j - moves [m] [1], k - moves [m] [2], l - moves [m] [3]))108 winningPosition [i] [j] [k] [l] = true;109 110 n = f.readInt ();111 for (int i = 0 ; i < n ; i++)112 {113 a = f.readInt ();114 b = f.readInt ();115 c = f.readInt ();116 d = f.readInt ();117 if (winningPosition [a] [b] [c] [d])118 cc.println ("Patrick");119 else120 cc.println ("Roland");121 }122 }123}124 125 126 127