- 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
- 113 lines of Java from the credited upstream file ccc02s3.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 456 78 91011 12131415 16import java.awt.*;17import hsa.*;18 19public class S3J5Blindfold20{21 static Console cc;22 static char [] [] grid;23 static int r, c, n;24 static char [] m;25 26 public static void main (String [] args)27 {28 cc = new Console ();29 30 String line;31 32 TextInputFile fi = new TextInputFile ("blind4.in");33 TextOutputFile fo = new TextOutputFile ("blind4.out");34 35 36 r = fi.readInt ();37 c = fi.readInt ();38 grid = new char [r] [c];39 for (int i = 0 ; i < r ; i++)40 {41 line = fi.readLine ();42 for (int j = 0 ; j < c ; j++)43 grid [i] [j] = line.charAt (j);44 }45 46 47 n = fi.readInt ();48 m = new char [n];49 for (int i = 0 ; i < n ; i++)50 m [i] = (fi.readLine ()).charAt (0);51 52 53 for (int i = 0 ; i < r ; i++)54 for (int j = 0 ; j < c ; j++)55 for (int d = 0 ; d < 360 ; d = d + 90)56 check (i, j, d);57 58 59 for (int i = 0 ; i < r ; i++)60 {61 for (int j = 0 ; j < c ; j++)62 fo.print (grid [i] [j]);63 fo.println ();64 }65 fi.close ();66 fo.close ();67 }68 69 70 71 72 73 74 public static void check (int i, int j, int dir)75 {76 int pi, pj, k;77 pi = i;78 pj = j;79 k = 0;80 while (pi >= 0 && pi < r && pj >= 0 && pj < c &&81 (grid [pi] [pj] == '.' || grid [pi] [pj] == '*') &&82 k < n)83 {84 if (m [k] == 'R')85 {86 dir = dir - 90;87 if (dir < 0)88 dir = 270;89 }90 else if (m [k] == 'L')91 dir = (dir + 90) % 360;92 else if (m [k] == 'F')93 {94 if (dir == 0)95 pj = pj + 1;96 else if (dir == 180)97 pj = pj - 1;98 else if (dir == 90)99 pi = pi - 1;100 else101 pi = pi + 1;102 }103 k++;104 }105 106 if (k >= n && pi >= 0 && pi < r && pj >= 0 && pj < c &&107 grid [pi] [pj] == '.')108 grid [pi] [pj] = '*';109 110 }111}112 113