fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int width, height;
  6. int states[][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
  7. cin >> width >> height;
  8. cout << '\n';
  9. int peaks[] = {0, 0, height, width};
  10. int pos[] = {0, width-1};
  11. int state = 0;
  12. while (true) {
  13. if (peaks[0] == peaks[2] || peaks[1] == peaks[3]) {
  14. break;
  15. }
  16. cout << pos[1] << ' ';
  17. int tpos[] = {pos[0] + states[state][0], pos[1] + states[state][1]};
  18. if (tpos[0] < peaks[0] || tpos[0] == peaks[2] || tpos[1] < peaks[1] || tpos[1] == peaks[3]) {
  19. peaks[state] += states[state][0] - states[state][1];
  20. state = (state + 1) % 4;
  21. cout << '\n';
  22. continue;
  23. }
  24. pos[0] = tpos[0];
  25. pos[1] = tpos[1];
  26. }
  27. cout << '\n';
  28. }
  29.  
Success #stdin #stdout 0s 3460KB
stdin
10 10
stdout
9 8 7 6 5 4 3 2 1 0 
0 0 0 0 0 0 0 0 0 0 
0 1 2 3 4 5 6 7 8 9 
9 9 9 9 9 9 9 9 9 
9 8 7 6 5 4 3 2 1 
1 1 1 1 1 1 1 1 
1 2 3 4 5 6 7 8 
8 8 8 8 8 8 8 
8 7 6 5 4 3 2 
2 2 2 2 2 2 
2 3 4 5 6 7 
7 7 7 7 7 
7 6 5 4 3 
3 3 3 3 
3 4 5 6 
6 6 6 
6 5 4 
4 4 
4 5