fork download
  1. /*
  2.   プログラミングのお題スレ Part13
  3.   https://m...content-available-to-author-only...h.net/test/read.cgi/tech/1549160513/
  4.  
  5.   746デフォルトの名無しさん2019/03/21(木) 13:37:55.15ID:p+hkSRzH
  6.   お題
  7.   長方形の盤で左上から上下左右に一マスずつ移動して全てのマスを
  8.   辿る。
  9.   移動の順番を表した数字を向きと終点を表す文字(↑↓←→★、
  10.   上下左右終、UDLRG、^V<>Gなど)に変換する。
  11.  
  12.   入力
  13.   1 4 5 6
  14.   2 3 8 7
  15.   15 14 9 10
  16.   16 13 12 11
  17.  
  18.   出力
  19.   ↓→→↓
  20.   →↑↓←
  21.   ↓←→↓
  22.   ★↑←←
  23.  
  24.  
  25. 直線部に矢印
  26. ↓┌→┐
  27. └┘┌┘
  28. ┌┐└┐
  29. ★└←┘
  30. */
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34.  
  35. enum{
  36. NONE,
  37. UP, // ↑ (UP)
  38. DW, // ↓ (DOWN)
  39. RR, // → (Right)
  40. LL, // ← (Left)
  41. LT, // ┌ (Left Top)
  42. LB, // └ (Left Bottom)
  43. RT, // ┐ (Right Top)
  44. RB, // ┘ (Right Bottom)
  45. GL, // ★ (Goal)
  46. IDT // \n (Indention)
  47. };
  48.  
  49. void code_print(int n){
  50. switch(n){
  51. case NONE: printf("■"); break;
  52. case DW: printf("↓"); break;
  53. case UP: printf("↑"); break;
  54. case RR: printf("→"); break;
  55. case LL: printf("←"); break;
  56. case LT: printf("┌"); break;
  57. case LB: printf("└"); break;
  58. case RT: printf("┐"); break;
  59. case RB: printf("┘"); break;
  60. case GL: printf("★"); break;
  61. case IDT: puts(""); break;
  62. default: break;
  63. }
  64. }
  65.  
  66. int *num_step(int *ret, int *src, int row, int col){
  67. int i;
  68. int idtcol = col+1;
  69. int goal_num;
  70. for(i = 1, goal_num = src[0]; i < row * col; i++){
  71. if(goal_num < src[i]){
  72. goal_num = src[i];
  73. }
  74. }
  75. for(i = 1; i <= row; i++){
  76. ret[idtcol * i - 1] = IDT;
  77. }
  78. int prev, cur;
  79. int cur_x = 0;
  80. int cur_y = 0;
  81. int prev_x = 0;
  82. int prev_y = 0;
  83. int next_num;
  84. for(i = 0; i < row * col; i++){
  85. if(src[cur_y * col + cur_x] == goal_num){
  86. ret[cur_y * idtcol + cur_x] = GL;
  87. break;
  88. }
  89. prev_y = cur_y;
  90. prev_x = cur_x;
  91. next_num = src[cur_y * col + cur_x] + 1;
  92. if( (cur_y != row - 1) && (src[(cur_y + 1) * col + cur_x] == next_num) ){
  93. cur = DW;
  94. cur_y++;
  95. }
  96. else if( (cur_y != 0) && (src[(cur_y - 1) * col + cur_x] == next_num) ){
  97. cur = UP;
  98. cur_y--;
  99. }
  100. else if( (cur_x != col - 1) && (src[cur_y * col + cur_x + 1] == next_num) ){
  101. cur = RR;
  102. cur_x++;
  103. }
  104. else if( (cur_x != 0) && (src[cur_y * col + cur_x - 1] == next_num) ){
  105. cur = LL;
  106. cur_x--;
  107. }
  108. else{
  109. printf("Error!\n");
  110. break;
  111. }
  112. if(i != 0){
  113. int temp = -1;
  114. if( (prev == UP && cur == RR) || (prev == LL && cur == DW) ){ //LT
  115. temp = LT;
  116. }
  117. else if( (prev == DW && cur == RR) || (prev == LL && cur == UP) ){ //LB
  118. temp = LB;
  119. }
  120. else if( (prev == UP && cur == LL) || (prev == RR && cur == DW) ){ //RT
  121. temp = RT;
  122. }
  123. else if( (prev == DW && cur == LL) || (prev == RR && cur == UP) ){ //RB
  124. temp = RB;
  125. }
  126. if(temp != -1){
  127. ret[prev_y * idtcol + prev_x] = temp;
  128. }
  129. else{
  130. ret[prev_y * idtcol + prev_x] = cur;
  131. }
  132. }
  133. else{
  134. ret[prev_y * idtcol + prev_x] = cur;
  135. }
  136. prev = cur;
  137. }
  138. return(ret);
  139. }
  140.  
  141. void run(int *src, int row, int col){
  142. int i, j;
  143. int len = row * (col + 1);
  144. int *ret = (int *)calloc(len, sizeof(int));
  145. num_step(ret, src, row, col);
  146. puts("Input: ");
  147. for(i = 0; i < row; i++){
  148. for(j = 0; j < col; j++){
  149. printf("%d ", src[i * col + j]);
  150. }
  151. puts("");
  152. }
  153. puts("\nOutput:");
  154. for(i = 0; i < len; i++){
  155. code_print(ret[i]);
  156. }
  157. free(ret);
  158. puts("");
  159. }
  160.  
  161. int main(void){
  162. int input0[]={1,4,5,6,
  163. 2,3,8,7,
  164. 15,14,9,10,
  165. 16,13,12,11};
  166.  
  167. int input1[] = {1,0,0,0,0,0,0,0,93,94,0,0,0,0,0,0,0,0,0,0,173,174,175,176,177,178,179,180,181,182,0,0,0,0,0,272,273,0,0,0,0,0,0,0,0,0,0,374,375,376,377,
  168. 2,3,0,0,0,0,0,0,92,95,0,0,0,0,0,0,0,0,0,0,172,171,170,169,168,167,166,185,184,183,0,0,0,0,0,271,274,0,0,0,0,0,0,0,0,0,0,373,0,0,378,
  169. 0,4,5,6,0,0,0,0,91,96,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,164,165,186,0,0,0,0,0,0,0,270,275,0,0,364,365,366,367,368,369,370,371,372,0,0,379,
  170. 0,0,8,7,0,0,0,0,90,97,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,163,188,187,0,0,0,0,0,0,0,269,276,0,0,363,362,361,360,359,358,357,384,383,382,381,380,
  171. 0,0,9,10,0,0,0,0,89,98,0,0,0,0,0,0,0,0,0,0,0,0,0,0,161,162,189,0,0,0,0,0,0,0,0,268,277,0,0,0,0,0,0,0,0,356,385,0,0,0,0,
  172. 0,0,12,11,0,0,0,0,88,99,100,0,0,0,0,0,0,0,0,0,0,0,0,0,160,191,190,0,0,0,0,0,0,0,0,267,278,0,0,0,0,0,0,0,0,355,386,0,0,0,0,
  173. 0,0,13,14,0,69,69,70,87,86,101,104,105,0,0,0,0,0,0,0,0,0,155,156,159,192,195,196,199,200,203,204,0,0,0,266,279,0,0,348,349,350,351,352,353,354,387,388,389,390,391,
  174. 0,0,16,15,0,67,68,71,72,85,102,103,106,0,0,0,0,0,0,0,0,0,154,157,158,193,194,197,198,201,202,205,206,263,264,265,280,0,0,347,346,345,344,343,342,341,396,395,394,393,392,
  175. 0,0,17,0,65,66,0,0,73,84,0,0,107,108,0,0,0,0,0,0,151,152,153,0,0,0,0,0,0,0,0,0,207,262,0,282,281,0,0,0,0,0,0,0,0,340,397,0,0,0,0,
  176. 0,0,18,63,64,0,0,0,74,83,0,0,0,109,110,0,0,0,0,0,150,0,0,0,0,0,0,0,0,0,0,0,208,261,0,283,284,0,0,0,0,0,0,0,0,339,398,0,0,0,0,
  177. 0,0,19,62,0,0,0,0,75,82,0,0,0,0,111,114,115,0,147,148,149,0,0,0,0,0,0,0,0,0,0,0,209,260,0,286,285,0,0,0,0,0,0,0,0,338,399,0,0,0,0,
  178. 0,21,20,61,0,0,0,0,76,81,0,0,0,0,112,113,116,0,146,0,0,0,0,0,0,0,0,0,0,0,0,0,210,259,0,287,288,0,0,0,0,0,0,0,0,337,400,0,0,0,0,
  179. 23,22,0,60,59,0,0,0,77,80,0,0,0,0,0,0,117,144,145,0,0,0,0,0,0,0,0,0,0,0,0,0,211,258,0,290,289,0,0,0,0,0,0,0,0,336,401,0,0,0,0,
  180. 24,25,0,0,58,57,0,0,78,79,0,0,0,0,0,0,118,143,0,0,0,0,0,0,0,0,0,0,0,0,0,0,212,257,0,291,292,0,0,0,0,0,0,0,0,335,402,0,0,0,0,
  181. 27,26,0,0,55,56,51,50,0,0,0,0,0,0,123,122,119,142,0,0,0,0,229,228,225,224,0,0,0,0,0,0,213,256,0,294,293,0,0,0,0,330,331,332,333,334,403,0,0,0,0,
  182. 28,29,0,0,54,53,52,49,0,0,0,0,0,125,124,121,120,141,0,0,0,231,230,227,226,223,222,0,0,0,0,215,214,255,0,295,296,0,0,0,0,329,328,327,326,325,404,0,0,0,0,
  183. 31,30,0,0,0,0,47,48,0,0,0,0,127,126,0,0,139,140,0,0,233,232,0,0,0,0,221,220,219,218,217,216,253,254,0,298,297,0,0,310,311,0,0,0,0,324,405,408,409,0,0,
  184. 32,33,0,0,0,0,46,45,0,0,0,0,128,129,0,0,138,137,0,0,234,235,0,0,0,0,246,247,248,249,250,251,252,0,0,299,300,0,308,309,312,0,0,0,0,323,406,407,410,411,0,
  185. 0,34,35,38,39,42,43,44,0,0,0,0,0,130,131,134,135,136,0,0,0,236,237,240,241,244,245,0,0,0,0,0,0,0,0,302,301,306,307,0,313,314,317,318,321,322,0,0,0,412,413,
  186. 0,0,36,37,40,41,0,0,0,0,0,0,0,0,132,133,0,0,0,0,0,0,238,239,242,243,0,0,0,0,0,0,0,0,0,303,304,305,0,0,0,315,316,319,320,0,0,0,0,415,414};
  187.  
  188. run(input0, 4, 4);
  189. run(input1, 20, 51);
  190. return 0;
  191. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
Input: 
1 4 5 6 
2 3 8 7 
15 14 9 10 
16 13 12 11 

Output:
↓┌→┐
└┘┌┘
┌┐└┐
★└←┘

Input: 
1 0 0 0 0 0 0 0 93 94 0 0 0 0 0 0 0 0 0 0 173 174 175 176 177 178 179 180 181 182 0 0 0 0 0 272 273 0 0 0 0 0 0 0 0 0 0 374 375 376 377 
2 3 0 0 0 0 0 0 92 95 0 0 0 0 0 0 0 0 0 0 172 171 170 169 168 167 166 185 184 183 0 0 0 0 0 271 274 0 0 0 0 0 0 0 0 0 0 373 0 0 378 
0 4 5 6 0 0 0 0 91 96 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 164 165 186 0 0 0 0 0 0 0 270 275 0 0 364 365 366 367 368 369 370 371 372 0 0 379 
0 0 8 7 0 0 0 0 90 97 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 163 188 187 0 0 0 0 0 0 0 269 276 0 0 363 362 361 360 359 358 357 384 383 382 381 380 
0 0 9 10 0 0 0 0 89 98 0 0 0 0 0 0 0 0 0 0 0 0 0 0 161 162 189 0 0 0 0 0 0 0 0 268 277 0 0 0 0 0 0 0 0 356 385 0 0 0 0 
0 0 12 11 0 0 0 0 88 99 100 0 0 0 0 0 0 0 0 0 0 0 0 0 160 191 190 0 0 0 0 0 0 0 0 267 278 0 0 0 0 0 0 0 0 355 386 0 0 0 0 
0 0 13 14 0 69 69 70 87 86 101 104 105 0 0 0 0 0 0 0 0 0 155 156 159 192 195 196 199 200 203 204 0 0 0 266 279 0 0 348 349 350 351 352 353 354 387 388 389 390 391 
0 0 16 15 0 67 68 71 72 85 102 103 106 0 0 0 0 0 0 0 0 0 154 157 158 193 194 197 198 201 202 205 206 263 264 265 280 0 0 347 346 345 344 343 342 341 396 395 394 393 392 
0 0 17 0 65 66 0 0 73 84 0 0 107 108 0 0 0 0 0 0 151 152 153 0 0 0 0 0 0 0 0 0 207 262 0 282 281 0 0 0 0 0 0 0 0 340 397 0 0 0 0 
0 0 18 63 64 0 0 0 74 83 0 0 0 109 110 0 0 0 0 0 150 0 0 0 0 0 0 0 0 0 0 0 208 261 0 283 284 0 0 0 0 0 0 0 0 339 398 0 0 0 0 
0 0 19 62 0 0 0 0 75 82 0 0 0 0 111 114 115 0 147 148 149 0 0 0 0 0 0 0 0 0 0 0 209 260 0 286 285 0 0 0 0 0 0 0 0 338 399 0 0 0 0 
0 21 20 61 0 0 0 0 76 81 0 0 0 0 112 113 116 0 146 0 0 0 0 0 0 0 0 0 0 0 0 0 210 259 0 287 288 0 0 0 0 0 0 0 0 337 400 0 0 0 0 
23 22 0 60 59 0 0 0 77 80 0 0 0 0 0 0 117 144 145 0 0 0 0 0 0 0 0 0 0 0 0 0 211 258 0 290 289 0 0 0 0 0 0 0 0 336 401 0 0 0 0 
24 25 0 0 58 57 0 0 78 79 0 0 0 0 0 0 118 143 0 0 0 0 0 0 0 0 0 0 0 0 0 0 212 257 0 291 292 0 0 0 0 0 0 0 0 335 402 0 0 0 0 
27 26 0 0 55 56 51 50 0 0 0 0 0 0 123 122 119 142 0 0 0 0 229 228 225 224 0 0 0 0 0 0 213 256 0 294 293 0 0 0 0 330 331 332 333 334 403 0 0 0 0 
28 29 0 0 54 53 52 49 0 0 0 0 0 125 124 121 120 141 0 0 0 231 230 227 226 223 222 0 0 0 0 215 214 255 0 295 296 0 0 0 0 329 328 327 326 325 404 0 0 0 0 
31 30 0 0 0 0 47 48 0 0 0 0 127 126 0 0 139 140 0 0 233 232 0 0 0 0 221 220 219 218 217 216 253 254 0 298 297 0 0 310 311 0 0 0 0 324 405 408 409 0 0 
32 33 0 0 0 0 46 45 0 0 0 0 128 129 0 0 138 137 0 0 234 235 0 0 0 0 246 247 248 249 250 251 252 0 0 299 300 0 308 309 312 0 0 0 0 323 406 407 410 411 0 
0 34 35 38 39 42 43 44 0 0 0 0 0 130 131 134 135 136 0 0 0 236 237 240 241 244 245 0 0 0 0 0 0 0 0 302 301 306 307 0 313 314 317 318 321 322 0 0 0 412 413 
0 0 36 37 40 41 0 0 0 0 0 0 0 0 132 133 0 0 0 0 0 0 238 239 242 243 0 0 0 0 0 0 0 0 0 303 304 305 0 0 0 315 316 319 320 0 0 0 0 415 414 

Output:
↓■■■■■■■┌┐■■■■■■■■■■┌→→→→→→→→┐■■■■■┌┐■■■■■■■■■■┌→→┐
└┐■■■■■■↑↓■■■■■■■■■■└←←←←←┐┌←┘■■■■■↑↓■■■■■■■■■■↑■■↓
■└→┐■■■■↑↓■■■■■■■■■■■■■■■┌┘↓■■■■■■■↑↓■■┌→→→→→→→┘■■↓
■■┌┘■■■■↑↓■■■■■■■■■■■■■■■↑┌┘■■■■■■■↑↓■■└←←←←←┐┌←←←┘
■■└┐■■■■↑↓■■■■■■■■■■■■■■┌┘↓■■■■■■■■↑↓■■■■■■■■↑↓■■■■
■■┌┘■■■■↑└┐■■■■■■■■■■■■■↑┌┘■■■■■■■■↑↓■■■■■■■■↑↓■■■■
■■└┐■■┌┐└┐↓┌┐■■■■■■■■■┌┐↑↓┌┐┌┐┌┐■■■↑↓■■┌→→→→→┘└→→→┐
■■┌┘■┌┘└┐↑└┘↓■■■■■■■■■↑└┘└┘└┘└┘└┐┌→┘↓■■└←←←←←┐┌←←←┘
■■↓■┌┘■■↓↑■■└┐■■■■■■┌→┘■■■■■■■■■↓↑■┌┘■■■■■■■■↑↓■■■■
■■↓┌┘■■■↓↑■■■└┐■■■■■↑■■■■■■■■■■■↓↑■└┐■■■■■■■■↑↓■■■■
■■↓↑■■■■↓↑■■■■↓┌┐■┌→┘■■■■■■■■■■■↓↑■┌┘■■■■■■■■↑↓■■■■
■┌┘↑■■■■↓↑■■■■└┘↓■↑■■■■■■■■■■■■■↓↑■└┐■■■■■■■■↑↓■■■■
┌┘■└┐■■■↓↑■■■■■■↓┌┘■■■■■■■■■■■■■↓↑■┌┘■■■■■■■■↑↓■■■■
└┐■■└┐■■└┘■■■■■■↓↑■■■■■■■■■■■■■■↓↑■└┐■■■■■■■■↑↓■■■■
┌┘■■┌┘┌┐■■■■■■┌┐↓↑■■■■┌┐┌┐■■■■■■↓↑■┌┘■■■■┌→→→┘↓■■■■
└┐■■└←┘↑■■■■■┌┘└┘↑■■■┌┘└┘└┐■■■■┌┘↑■└┐■■■■└←←←┐↓■■■■
┌┘■■■■┌┘■■■■┌┘■■┌┘■■┌┘■■■■└←←←←┘┌┘■┌┘■■┌┐■■■■↑↓┌┐■■
└┐■■■■└┐■■■■└┐■■└┐■■└┐■■■■┌→→→→→┘■■└┐■┌┘↓■■■■↑└┘└┐■
■└┐┌┐┌→┘■■■■■└┐┌→┘■■■└┐┌┐┌┘■■■■■■■■┌┘┌┘■└┐┌┐┌┘■■■└┐
■■└┘└┘■■■■■■■■└┘■■■■■■└┘└┘■■■■■■■■■└→┘■■■└┘└┘■■■■★┘