fork download
  1. int* buildSnailReverse(int* a, int w, int h)
  2.  
  3. {
  4.  
  5.  
  6.  
  7. int x;
  8.  
  9. int y;
  10.  
  11. int turn = 1; //x축 이동중 : +1 y축 이동중 : -1
  12.  
  13. int direction = 1; //→,↓ 방향 : +1 ↑,→ 방향 : -1
  14.  
  15. int move = 0;
  16.  
  17. int maxMoveX = 1;
  18.  
  19. int maxMoveY = 1;
  20.  
  21. int min;
  22.  
  23.  
  24.  
  25.  
  26.  
  27. //1. 시작위치(x,y), 초기 축(turn), 축마다 초기이동거리(maxMoveX, maxMoveY)를 초기화해준다.
  28.  
  29. {
  30.  
  31. //1.1 행렬에 맞게 초기화해준다.
  32.  
  33. //1.1.1 너비,높이의 차이가 1인 행렬 or 정사각행렬
  34.  
  35. if( abs(w-h) == 1 || w-h==0)
  36.  
  37. {
  38.  
  39. x=(w-1)/2;
  40.  
  41. y=(h-1)/2;
  42.  
  43. turn=1;
  44.  
  45. }
  46.  
  47.  
  48.  
  49. //1.1.2 너비 또는 높이가 1인 행렬
  50.  
  51. else if ( w==1 || h==1 )
  52.  
  53. {
  54.  
  55. if(w==1)
  56.  
  57. {
  58.  
  59. maxMoveY=h-1;
  60.  
  61. turn=-1;
  62.  
  63. }
  64.  
  65. if(h==1)
  66.  
  67. {
  68.  
  69. maxMoveX=w-1;
  70.  
  71. turn=1;
  72.  
  73. }
  74.  
  75.  
  76.  
  77. x=0;
  78.  
  79. y=0;
  80.  
  81. }
  82.  
  83.  
  84.  
  85. //1.1.3 홀수x홀수 행렬
  86.  
  87. else if( h%2==1 && w%2==1)
  88.  
  89. {
  90.  
  91. if( h < w)
  92.  
  93. {
  94.  
  95. min=h;
  96.  
  97. maxMoveX=w - h;
  98.  
  99. maxMoveY=1;
  100.  
  101. turn=1;
  102.  
  103. }
  104.  
  105. else
  106.  
  107. {
  108.  
  109. min=w;
  110.  
  111. maxMoveX=1;
  112.  
  113. maxMoveY=h-w;
  114.  
  115. turn=-1;
  116.  
  117. }
  118.  
  119. x=min/2;
  120.  
  121. y=min/2;
  122.  
  123. }
  124.  
  125.  
  126.  
  127. //1.1.4 짝수x짝수 행렬
  128.  
  129. else if( h%2==0 && w%2==0)
  130.  
  131. {
  132.  
  133. if( h < w )
  134.  
  135. {
  136.  
  137. min = h;
  138.  
  139. maxMoveX=w-2*(h/2-1)-1;
  140.  
  141. maxMoveY=1;
  142.  
  143. turn=1;
  144.  
  145. }
  146.  
  147. else
  148.  
  149. {
  150.  
  151. min = w;
  152.  
  153. maxMoveX=1;
  154.  
  155. maxMoveY=h-2*(w/2-1)-1;
  156.  
  157. turn=-1;
  158.  
  159. }
  160.  
  161. x = min/2-1;
  162.  
  163. y = min/2-1;
  164.  
  165. }
  166.  
  167.  
  168.  
  169. //1.2 시작지점을 1로 초기화한다.
  170.  
  171. a[y*w + x ] = 1;
  172.  
  173.  
  174.  
  175. }
  176.  
  177.  
  178.  
  179. //2. 나머지 수들을 채워넣는다.
  180.  
  181. /* turn : 1이면 x축이동중
  182.  
  183. -1이면 y축이동중
  184.  
  185. direction : 1이면 →,↓
  186.  
  187. -1이면 ↑,→
  188.  
  189. */
  190.  
  191. for(int i=2 ; i<=w*h ; i++)
  192.  
  193. {
  194.  
  195. //x축
  196.  
  197. if(turn==1)
  198.  
  199. {
  200.  
  201. move++;
  202.  
  203. x+=direction;
  204.  
  205.  
  206.  
  207. //현재 최대이동거리에 도달했을 때
  208.  
  209. if( move == maxMoveX )
  210.  
  211. {
  212.  
  213. move=0;
  214.  
  215. maxMoveX++;
  216.  
  217. turn*=-1;
  218.  
  219. if( h >= w )
  220.  
  221. {
  222.  
  223. direction*=-1;
  224.  
  225. }
  226.  
  227. }
  228.  
  229. }
  230.  
  231. // y축
  232.  
  233. else
  234.  
  235. {
  236.  
  237. move++;
  238.  
  239. y+=direction;
  240.  
  241.  
  242.  
  243. if( move == maxMoveY )
  244.  
  245. {
  246.  
  247. move=0;
  248.  
  249. maxMoveY++;
  250.  
  251. turn*=-1;
  252.  
  253.  
  254.  
  255. if( h < w)
  256.  
  257. {
  258.  
  259. direction*=-1;
  260.  
  261. }
  262.  
  263. }
  264.  
  265. }
  266.  
  267.  
  268.  
  269. a[y*w + x ] = i;
  270.  
  271. }
  272.  
  273.  
  274.  
  275.  
  276.  
  277. return a;
  278.  
  279. }
  280.  
  281.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int* buildSnailReverse(int*, int, int)’:
prog.cpp:35:15: error: ‘abs’ was not declared in this scope
stdout
Standard output is empty