やるだけ
- class SpiralWalking {
- public:
- int totalPoints(vector <string> levelMap)
- {
- const int di[] = {0, 1, 0, -1};
- const int dj[] = {1, 0, -1, 0};
- int i = 0;
- int j = 0;
- int d = 0;
- int sum = 0;
- const int h = levelMap.size();
- const int w = levelMap[0].size();
- for (int loop = h * w; loop--; ) {
- sum += (levelMap[i][j] - '0');
- int tmp = d;
- if (loop == 0) break;
- while (true) {
- int ni = i + di[d];
- int nj = j + dj[d];
- if (ni < 0 || nj < 0) d = (d + 1) % 4;
- else if (h <= ni || w <= nj) d = (d + 1) % 4;
- else if (levelMap[ni][nj] == '@') d = (d + 1) % 4;
- break;
- }
- if (tmp != d) sum -= (levelMap[i][j] - '0');
- levelMap[i][j] = '@';
- i = i + di[d];
- j = j + dj[d];
- }
- return sum;
- }
- };