• Source
    1. #include <bits/stdc++.h>
    2. #include<limits.h>
    3. #include<stdio.h>
    4. #include<cstring>
    5. #include<string>
    6. using namespace std;
    7. typedef long long ll;
    8. typedef long double ld;
    9. #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    10. #define pb push_back
    11. #define mp make_pair
    12. #define sz(a) a.size()
    13. #define fi first
    14. #define se second
    15. #define rsz(a,n) a.resize(n)
    16. #define test(t) ll t;cin>>t;while(t--)
    17. #define forn(i,e) for(int i = 0; i < e; i++)
    18. #define forsn(i,s,e) for(int i = s; i < e; i++)
    19. #define rforn(i,s) for(int i = s; i >= 0; i--)
    20. #define rforsn(i,s,e) for(int i = s; i >= e; i--)
    21. #define fillv(a,k) memset(a,k,sizeof(a))
    22. #define filla(a,k,n) memset(a,k,sizeof(a[0])*n)
    23. #define leadzero(a) __builtin_clz(a) //count leading zeros
    24. #define trailzero(a) __builtin_ctz(a) //count trailing zeros
    25. #define bitcount(a) __builtin_popcount(a) // count set bits (add ll)
    26. #define ln cout<<"\n"
    27. #define sp cout<<" "
    28. #define spaceprint(a,i,s,n) {forsn(i,s,n) { cout<<a[i]; sp;}}
    29. #define lineprint(a,i,s,n) {forsn(i,s,n) {cout<<a[i]; ln;}}
    30. #define maxe(a) *max_element(a.begin(),a.end())
    31. #define maxi(a) max_element(a.begin(),a.end())-a.begin()
    32. #define mine(a) *min_element(a.begin(),a.end())
    33. #define mini(a) min_element(a.begin(),a.end())-a.begin()
    34. #define all(c) c.begin(),c.end()
    35. #define trav(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
    36. #define present(container, element) (container.find(element) != container.end())
    37. #define cpresent(container, element) (find(all(container),element) != container.end())// check the presence of element
    38. //copy(from_begin, from_end, to_begin); copy function
    39. typedef unsigned long long int ull;
    40. typedef long double ld;
    41. typedef pair<ll,ll> p64;
    42. typedef pair<int,int> p32;
    43. typedef pair<int,p32> p96;
    44. typedef vector<ll> v64;
    45. typedef vector<string> vs;
    46. typedef vector<int> v32;
    47. typedef vector<v32> vv32;
    48. typedef vector<v64> vv64;
    49. typedef vector<p32> vp32;
    50. typedef vector<p64> vp64;
    51. typedef vector<vp32> vvp32;
    52. typedef map<int,int> m32;
    53. const int LIM = 1e5+5, MOD = 1e9+7;
    54. #define sumv(v) accumulate(all(v),ll(0))
    55. #define productv(v) accumulate(all(v), ll(1), multiplies< ll >())
    56. int main()
    57. {
    58. ll t;
    59. cin>>t;
    60. ll h=1;
    61. while(h<=t)
    62. {
    63. v64 vx,vy;
    64. ll flag=0,k=1,r=0,i,j;
    65. ll n,m;
    66. cin>>n>>m;
    67. if(n>m)
    68. {
    69. swap(n,m);
    70. k=0;
    71. }
    72. if(n==1 or m==1)
    73. {
    74. flag=1;
    75. }
    76. else if(n<=3 and m<=3)
    77. {
    78. flag=1;
    79. }
    80. else if(m==4 and n==2)
    81. {
    82. flag=1;
    83. }
    84. else if(m==4 and n==4)
    85. {
    86. cout<<"Case #"<<h;
    87. cout<<": POSSIBLE";
    88. ln;
    89. for(i=1;i<=4;i++)
    90. {
    91. cout<<1<<" "<<i;
    92. ln;
    93. cout<<3<<" "<<(i%4)+1;
    94. ln;
    95. }
    96. for(i=4;i>1;i--)
    97. {
    98. cout<<2<<" "<<i;
    99. ln;
    100. cout<<4<<" "<<i-1;
    101. ln;
    102. }
    103. cout<<2<<" "<<1;
    104. ln;
    105. cout<<4<<" "<<4;
    106. ln;
    107. h++;
    108. continue;
    109. }
    110. if(n%2 and flag==0)
    111. {
    112. ll s=((m+1)/2);
    113. if(m==4)
    114. {
    115. vx.pb(2);vy.pb(2);
    116. vx.pb(4);vy.pb(1);
    117. vx.pb(1);vy.pb(3);
    118. vx.pb(3);vy.pb(2);
    119. vx.pb(1);vy.pb(1);
    120. vx.pb(4);vy.pb(3);
    121. vx.pb(2);vy.pb(1);
    122. vx.pb(4);vy.pb(2);
    123. vx.pb(2);vy.pb(3);
    124. vx.pb(3);vy.pb(1);
    125. vx.pb(1);vy.pb(2);
    126. vx.pb(3);vy.pb(3);
    127. }
    128. else
    129. {
    130. for(i=1;i<=(m+1)/2;i++)
    131. {
    132. vx.pb(i);
    133. vy.pb(1);
    134. if(i+s<=m)
    135. {
    136. vx.pb(i+s);
    137. vy.pb(3);
    138. r=1;
    139. }
    140. }
    141. for(j=1;i<=m;i++,j++)
    142. {
    143. if(m%2==0)
    144. {
    145. vx.pb(i);
    146. vy.pb(1);
    147. vx.pb(j);
    148. vy.pb(2);
    149. }
    150. else
    151. {
    152. vx.pb(j);
    153. vy.pb(2);
    154. vx.pb(i);
    155. vy.pb(1);
    156. }
    157. }
    158. for(i=1;j<=m;j++,i++)
    159. {
    160. if(m%2==1)
    161. {
    162. vx.pb(j);
    163. vy.pb(2);
    164. vx.pb(i);
    165. vy.pb(3);
    166. }
    167. else
    168. {
    169. vx.pb(i);
    170. vy.pb(3);
    171. vx.pb(j);
    172. vy.pb(2);
    173. }
    174. }
    175. }
    176. }
    177. if(flag==0)
    178. {
    179. ll i,j;
    180. i=1;
    181. if(n%2)
    182. {
    183. i=i+3;
    184. }
    185. ll s=((m+1)/2);
    186. for(;i+1<=n;i=i+2)
    187. {
    188. for(j=1;j<=m;j++)
    189. {
    190. vx.pb(j);
    191. vy.pb(i);
    192. vx.pb(((j+s-1)%m)+1);
    193. vy.pb(i+1);
    194. }
    195. }
    196. cout<<"Case #"<<h;
    197. cout<<": POSSIBLE";
    198. ln;
    199. if(k==1)
    200. {
    201. for(i=0;i<vx.size();i++)
    202. {
    203. cout<<vy[i];
    204. sp;
    205. cout<<vx[i];
    206. ln;
    207. }
    208. }
    209. else
    210. {
    211. for(i=0;i<vx.size();i++)
    212. {
    213. cout<<vx[i];
    214. sp;
    215. cout<<vy[i];
    216. ln;
    217. }
    218. }
    219. }
    220. else if(flag==1)
    221. {
    222. cout<<"Case #"<<h;
    223. cout<<": IMPOSSIBLE";
    224. ln;
    225. }
    226. h++;
    227.  
    228. }
    229.  
    230.  
    231. }
    232.