Advertisement
Shiam7777777

Untitled

Apr 12th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  4. #define ll long long
  5. #define ld double
  6. #define llu long long unsigned
  7.  
  8. int grid[20][20];
  9. int r1 , c1;
  10. int m , n;
  11. vector < pair < int , int > > p;
  12.  
  13. void dfs(  )
  14. {
  15.     for( int k = r1 - 1 ; k <= r1 + 1 ; k++ )
  16.     {
  17.         for( int l = c1 ; l < m ; l++ )
  18.         {
  19.             if( !( k == r1 or l == c1 or k - l == r1 - c1 or k + l == r1 + c1 ) and grid[k][l] == 0 and k >= 0 and k < n and l >= 0 and k < m )
  20.             {
  21.                 grid[k][l] = -1;
  22.                 r1 = k;
  23.                 c1 = l;
  24.                 p.push_back( make_pair( k , l ) );
  25.                 return;
  26.             }
  27.         }
  28.         for( int l = c1 ; l >= 0 ; l-- )
  29.         {
  30.             if( !( k == r1 or l == c1 or k - l == r1 - c1 or k + l == r1 + c1 ) and grid[k][l] == 0 and k >= 0 and k < n and l >= 0 and k < m )
  31.             {
  32.                 grid[k][l] = -1;
  33.                 r1 = k;
  34.                 c1 = l;
  35.                 p.push_back( make_pair( k , l ) );
  36.                 return;
  37.             }
  38.         }
  39.     }
  40.     for( int k = c1 - 1 ; k <= c1 + 1 ; k++ )
  41.     {
  42.         for( int l = r1 ; l < n ; l++ )
  43.         {
  44.             if( !( l == r1 or k == c1 or l - k == r1 - c1 or k + l == r1 + c1 ) and grid[k][l] == 0 and k >= 0 and k < n and l >= 0 and k < m )
  45.             {
  46.                 grid[l][k] = -1;
  47.                 r1 = l;
  48.                 c1 = k;
  49.                 p.push_back( make_pair( l , k ) );
  50.                 return;
  51.             }
  52.         }
  53.         for( int l = r1 ; l >= 0 ; l-- )
  54.         {
  55.             if( !( l == r1 or k == c1 or l - k == r1 - c1 or k + l == r1 + c1 ) and grid[k][l] == 0 and k >= 0 and k < n and l >= 0 and k < m )
  56.             {
  57.                 grid[l][k] = -1;
  58.                 r1 = l;
  59.                 c1 = k;
  60.                 p.push_back( make_pair( l , k ) );
  61.                 return;
  62.             }
  63.         }
  64.     }
  65. }
  66.  
  67. int main()
  68. {
  69.     fast;
  70.     int t;
  71.     cin>>t;
  72.     for( int c = 1 ; c <= t ; c++ )
  73.     {
  74.         cout<<"Case #"<<c<<": ";
  75.         cin>>n>>m;
  76.         memset( grid , 0 , sizeof( grid ) );
  77.         p.clear();
  78.         p.push_back( make_pair( 0 , 0 ) );
  79.         r1 = 0 ;
  80.         c1 = 0 ;
  81.         grid[0][0] = -1;
  82.         for( int tt = 1 ; tt <= m * n ; tt++ )
  83.             dfs(  );
  84. //        cout<<p.size()<<endl;
  85. //        for( int tt = 0 ; tt < p.size() ; tt++ )
  86. //                cout<<p[tt].first+1<<" "<<p[tt].second+1<<endl;
  87.         if( p.size() == m * n )
  88.         {
  89.             cout<<"POSSIBLE\n";
  90.             for( int tt = 0 ; tt < m * n ; tt++ )
  91.                 cout<<p[tt].first+1<<" "<<p[tt].second+1<<endl;
  92.         }
  93.         else
  94.             cout<<"IMPOSSIBLE\n";
  95.     }
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement