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