Guest User

pylons

a guest
Apr 13th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. /**< https://codingcompetitions.withgoogle.com/codejam/round/0000000000051635/0000000000104e03 */
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. # define ll long long
  5. int arr[21][21];
  6. vector<pair<int,int>> st;
  7. int r,c,q,cnt=0;
  8. bool isvalid(int x,int y,int lastx,int lasty)
  9. {
  10. if(lastx==lasty&&lastx==-1)
  11. return true;
  12. if(arr[x][y]==1||(x-y==lastx-lasty)||(x+y==lastx+lasty)||y==lasty||x==lastx)
  13. return false;
  14. return true;
  15. }
  16. bool solve(int x,int lastr,int lastc)
  17. {
  18. if(cnt==q)
  19. return true;
  20. bool b=false;
  21. for(int i=1;i<=c;i++)
  22. {
  23. if(isvalid(x,i,lastr,lastc))
  24. {
  25. arr[x][i]=1;
  26. cnt++;
  27. b=solve((x+1)>r?1:x+1,x,i);
  28.  
  29. if(b)
  30. {
  31. st.push_back(make_pair(x,i));
  32. break;
  33. }
  34. arr[x][i]=0;
  35. cnt--;
  36. }
  37. }
  38. return b;
  39. }
  40. int main() {
  41. ios_base::sync_with_stdio(false);
  42. cin.tie(NULL);
  43. #ifndef ONLINE_JUDGE
  44. freopen("input.txt","r",stdin);
  45. freopen("output.txt","w",stdout);
  46. #endif // ONLINE
  47. int test,c1=0;
  48. cin>>test;
  49. while(c1<test)
  50. {
  51. c1++;
  52. cout<<"Case #"<<c1<<": ";
  53. cin>>r>>c;
  54. q=r*c;
  55. bool b=solve(1,-1,-1);
  56. if(!b)
  57. cout<<"IMPOSSIBLE\n";
  58. else{
  59. cout<<"POSSIBLE\n";
  60. for(int i=st.size()-1;i>=0;i--)
  61. cout<<st[i].first<<" "<<st[i].second<<"\n";
  62.  
  63. }
  64. st.clear();
  65. cnt=0;
  66. memset(arr,0,sizeof(arr));
  67. }
  68. return 0;
  69. }
Add Comment
Please, Sign In to add comment