1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <stack> #include <queue> #include <map>
#define ll long long int #define scf(a) scanf("%d", &a) #define mms(a) memset(a, 0, sizeof(a))
using namespace std;
int Map[20][20]; int dir[4][2] = {0,1, 1,0, 0,-1, -1,0}; int m, n;
bool isInMap(int i, int j) { return (i >= 0 && j >= 0 && i < m && j < n && !Map[i][j]) ? true : false; }
void init(int &nowi, int &nowj, int &now, int &num) { mms(Map); Map[0][0] = 1; nowi = 0; nowj = 0; now = 0; num = 2; }
int main() { int nowi, nowj, now, num; while (cin >> m >> n) { init(nowi, nowj, now, num);
while (num <= m * n) { if (isInMap(nowi + dir[now][0], nowj + dir[now][1])) { nowi += dir[now][0]; nowj += dir[now][1]; Map[nowi][nowj] = num++; } else now = ++now % 4; }
for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) printf("%2d%c", Map[i][j], ((j == n - 1) ? '\n' : ' ')); } } return 0; }
|