askarulytarlan

matrix from numbers

Nov 6th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cmath>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <cstdlib>
  8. #include <ctime>
  9. #include <string>
  10. #include <cstring>
  11. #include <string.h>
  12.  
  13.  
  14.  
  15. using namespace std;
  16.  
  17. const int N = 1005;
  18.  
  19. bool was[N][N]; //
  20. int a[N][N], n;
  21.  
  22. void build_board() {
  23. for (int i = 0; i <= n + 1; i++) {
  24. was[0][i] = true; // u
  25. was[i][n + 1] = true; // r
  26. was[i][0] = true; // l
  27. was[n + 1][i] = true; // d
  28. }
  29. }
  30.  
  31. bool est_hod() {
  32. if (was[x + 1][y] == true && was[x - 1][y] == true && was[x][y + 1] == true && was[x][y - 1] == true)
  33. return false;
  34. return true;
  35. }
  36.  
  37. int main() {
  38.  
  39. cin >> n;
  40.  
  41. build_board();
  42.  
  43. x = y = 1; // NOW
  44. cnt = 0;
  45.  
  46. while (est_hod()) {
  47. a[x][y] = ++cnt;
  48. was[x][y] = true;
  49. if (was[x][y + 1] == false)
  50. y++;
  51. else if (was[x + 1][y] == false)
  52. x++;
  53. else if (was[x][y - 1] == false)
  54. y--;
  55. else
  56. x--;
  57. }
  58.  
  59. for (int i = 1; i <= n; i++) {
  60. for (int j = 1; j <= n; j++)
  61. cout << a[i][j] << " ";
  62. cout << endl;
  63. }
  64.  
  65. }
Add Comment
Please, Sign In to add comment