Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #define ll long long
- #define ld double
- #define llu long long unsigned
- int grid[20][20];
- int r1 , c1;
- int m , n;
- vector < pair < int , int > > p;
- void dfs( )
- {
- for( int k = r1 - 1 ; k <= r1 + 1 ; k++ )
- {
- for( int l = c1 ; l < m ; l++ )
- {
- 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 )
- {
- grid[k][l] = -1;
- r1 = k;
- c1 = l;
- p.push_back( make_pair( k , l ) );
- return;
- }
- }
- for( int l = c1 ; l >= 0 ; l-- )
- {
- 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 )
- {
- grid[k][l] = -1;
- r1 = k;
- c1 = l;
- p.push_back( make_pair( k , l ) );
- return;
- }
- }
- }
- for( int k = c1 - 1 ; k <= c1 + 1 ; k++ )
- {
- for( int l = r1 ; l < n ; l++ )
- {
- 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 )
- {
- grid[l][k] = -1;
- r1 = l;
- c1 = k;
- p.push_back( make_pair( l , k ) );
- return;
- }
- }
- for( int l = r1 ; l >= 0 ; l-- )
- {
- 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 )
- {
- grid[l][k] = -1;
- r1 = l;
- c1 = k;
- p.push_back( make_pair( l , k ) );
- return;
- }
- }
- }
- }
- int main()
- {
- fast;
- int t;
- cin>>t;
- for( int c = 1 ; c <= t ; c++ )
- {
- cout<<"Case #"<<c<<": ";
- cin>>n>>m;
- memset( grid , 0 , sizeof( grid ) );
- p.clear();
- p.push_back( make_pair( 0 , 0 ) );
- r1 = 0 ;
- c1 = 0 ;
- grid[0][0] = -1;
- for( int tt = 1 ; tt <= m * n ; tt++ )
- dfs( );
- // cout<<p.size()<<endl;
- // for( int tt = 0 ; tt < p.size() ; tt++ )
- // cout<<p[tt].first+1<<" "<<p[tt].second+1<<endl;
- if( p.size() == m * n )
- {
- cout<<"POSSIBLE\n";
- for( int tt = 0 ; tt < m * n ; tt++ )
- cout<<p[tt].first+1<<" "<<p[tt].second+1<<endl;
- }
- else
- cout<<"IMPOSSIBLE\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement