fork download
  1. // Author: Ivan Kazmenko (gassa@mail.ru)
  2. module solution;
  3. import std.algorithm;
  4. import std.conv;
  5. import std.range;
  6. import std.stdio;
  7.  
  8. char [] [] initBoard (int r, int c)
  9. {
  10. auto res = new char [] [] (r, c);
  11. foreach (ref line; res)
  12. {
  13. line[] = '.';
  14. }
  15. return res;
  16. }
  17.  
  18. void placeCross (ref char [] [] board, int i, int j)
  19. {
  20. board[i + 0][j + 0] = '+';
  21. board[i - 1][j + 0] = '|';
  22. board[i + 1][j + 0] = '|';
  23. board[i + 0][j - 1] = '-';
  24. board[i + 0][j + 1] = '-';
  25. }
  26.  
  27. char [] [] solveNotDivisible (int r, int c)
  28. {
  29. auto res = initBoard (r, c);
  30. int start = r % 3 - 1;
  31. int cur = 0;
  32. int type = 1;
  33. while (cur + 3 <= c)
  34. {
  35. for (int pos = start; pos + 3 <= r; pos += 3)
  36. {
  37. int row = pos + type;
  38. if (pos == 1 && type == 1)
  39. {
  40. row = 0;
  41. }
  42. placeCross (res, row + 1, cur + 1);
  43. }
  44. if (c % 2 == 0 && cur == 2)
  45. {
  46. cur += 3;
  47. }
  48. else
  49. {
  50. cur += 2;
  51. type ^= 1;
  52. }
  53. }
  54. return res;
  55. }
  56.  
  57. char [] [] solveDivisible (int r, int c)
  58. {
  59. auto res = initBoard (r, c);
  60. for (int i = 1; i < r; i += 3)
  61. {
  62. for (int j = 1; j < c; j += 3)
  63. {
  64. placeCross (res, i, j);
  65. }
  66. }
  67. return res;
  68. }
  69.  
  70. char [] [] transposeBoard (char [] [] board, int r, int c)
  71. {
  72. auto res = new char [] [] (r, c);
  73. foreach (i; 0..r)
  74. {
  75. foreach (j; 0..c)
  76. {
  77. res[i][j] = board[j][i];
  78. if ("|-".canFind (res[i][j]))
  79. {
  80. res[i][j] = to !(char) ('|' + '-' - res[i][j]);
  81. }
  82. }
  83. }
  84. return res;
  85. }
  86.  
  87. char [] [] solve (int r, int c)
  88. {
  89. if (r % 3 == 0 && c % 3 == 0)
  90. {
  91. return solveDivisible (r, c);
  92. }
  93. if (r % 3 == 0)
  94. {
  95. return transposeBoard (solve (c, r), r, c);
  96. }
  97. return solveNotDivisible (r, c);
  98. }
  99.  
  100. void main ()
  101. {
  102. int r;
  103. int c;
  104. while (readf (" %s %s", &r, &c) > 0)
  105. {
  106. writefln ("%-(%s\n%)", solve (r, c));
  107. }
  108. }
  109.  
Success #stdin #stdout 0s 14992KB
stdin
14 28
stdout
.|......|...|...|...|...|...
-+-|..|-+-|-+-|-+-|-+-|-+-|.
.|-+--+-|-+-|-+-|-+-|-+-|-+-
...|..|...|...|...|...|...|.
...|..|...|...|...|...|...|.
.|-+--+-|-+-|-+-|-+-|-+-|-+-
-+-|..|-+-|-+-|-+-|-+-|-+-|.
.|.|..|.|.|.|.|.|.|.|.|.|.|.
.|-+--+-|-+-|-+-|-+-|-+-|-+-
-+-|..|-+-|-+-|-+-|-+-|-+-|.
.|.|..|.|.|.|.|.|.|.|.|.|.|.
.|-+--+-|-+-|-+-|-+-|-+-|-+-
-+-|..|-+-|-+-|-+-|-+-|-+-|.
.|......|...|...|...|...|...