fork download
  1. #include <iostream>
  2. using namespace std;
  3. #define N 20
  4. int hc[N][N];
  5.  
  6. void printArray()
  7. {
  8. for(int i = 0; i < N; i++)
  9. {
  10. for(int j = 0; j < N; j++)
  11. if(hc[i][j])
  12. printf("%3d ", hc[i][j]);
  13. else
  14. printf(" ");
  15. cout << endl;
  16. }
  17. }
  18.  
  19. void fillArray( int &i, int &j, string directions[], int n, int round, int &num)
  20. {
  21. int flag = 1;
  22. for (int k = 0; k < n; k++)
  23. {
  24. string dir = directions[k];
  25.  
  26. for(int m = 0 ; (m < round) && flag; m++)
  27. {
  28. //cout << dir << endl;
  29. if (dir=="S") i += 2, m++;
  30. else if (dir=="N") i -= 2;
  31. else if (dir=="W") j -= 2;
  32. else if (dir=="E") j += 2;
  33. else if (dir=="NW")
  34. j -=1, i -=1;
  35. else if (dir=="NE")
  36. j +=1, i -=1;
  37. else if (dir=="SE")
  38. j +=1, i +=1;
  39. else if (dir=="SW")
  40. j -=1, i +=1, m++;
  41.  
  42. hc[i][j] = num;
  43. num += 1;
  44. //flag = 0;
  45. }
  46. }
  47. }
  48.  
  49. int main()
  50. {
  51. int i = N/2;
  52. int j = N/2;
  53. int num = 1;
  54. hc[i][j] = num;
  55. num += 1;
  56. int round = 1;
  57.  
  58. string directions1[] = {"S", "NW", "N", "NE", "SE", "S" };
  59. int n = sizeof(directions1)/sizeof(directions1[0]);
  60. fillArray(i, j, directions1, n, round, num);
  61.  
  62. string directions2[] = {"S", "SW", "NW", "N", "NE", "SE", "S"};
  63. n = sizeof(directions2)/sizeof(directions2[0]);
  64.  
  65. while(num < 36)
  66. {
  67. round++;
  68. fillArray(i, j, directions2, n, round, num);
  69. }
  70.  
  71. printArray();
  72. // your code goes here
  73. return 0;
  74. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                   31                                               
                                              30        32                                          
                                         29        15        33                                     
                                    28        14        16        34                                
                                         13         5        17                                     
                                    27         4         6        35                                
                                         12         1        18                                     
                                    26         3         7        36                                
                                         11         2        19                                     
                                    25        10         8                                          
                                         24         9        20                                     
                                              23        21                                          
                                                   22