fork(2) download
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. using namespace std;
  5. int n, p, q;
  6. int used[26][26];
  7. int pathX[26];
  8. int pathY[26];
  9. int dir[8][2] = { { -2, -1 }, { -2, 1 }, { -1, -2 }, { -1, 2 }, { 1, -2 }, { 1,
  10. 2 }, { 2, -1 }, { 2, 1 } };
  11. int ok;
  12. void dfs(int x, int y, int step) {
  13. pathX[step - 1] = x;
  14. pathY[step - 1] = y;
  15. used[x][y] = 1;
  16. if (step == p * q) {
  17. ok = 1;
  18. return;
  19. }
  20. int i;
  21. for (i = 0; i < 8; i++) {
  22. int _x = x + dir[i][0];
  23. int _y = y + dir[i][1];
  24. if (_x >= 0 && _y >= 0 && _x < p && _y < q && !used[_x][_y]) {
  25. dfs(_x, _y, step + 1);
  26. }
  27. }
  28. used[x][y] = 0;
  29. }
  30. int main() {
  31. int sum = 0;
  32. cin >> n;
  33. while (n--) {
  34. cin >> p >> q;
  35. ok = 0;
  36. memset(used, 0, sizeof(used));
  37. memset(pathX, 0, sizeof(pathX));
  38. memset(pathY, 0, sizeof(pathY));
  39. printf("Scenario #%d:\n", ++sum);
  40. dfs(0, 0, 1);
  41. if (ok != 1) {
  42. printf("impossible\n");
  43. } else {
  44. for (int i = 0; i < p * q; i++) {
  45. printf("%c%d", pathY[i] + 'A', pathX[i] + 1);
  46. }
  47. printf("\n");
  48. }
  49. printf("\n");
  50. }
  51. return 0;
  52. }
Success #stdin #stdout 0.01s 2728KB
stdin
3 
1 1 
2 3 
4 3
stdout
Scenario #1:
A1

Scenario #2:
impossible

Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4