-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOMCB.cpp
More file actions
70 lines (68 loc) · 1.71 KB
/
COMCB.cpp
File metadata and controls
70 lines (68 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 17 August 2015 (Monday) 04:58
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define mod 1000000009
int x[8] = {-1,1,-2,2,-2,2,-1,1};
int y[8] = {-2,-2,-1,-1,1,1,2,2};
int n , m;
bool valid(int row , int col){
if(row>=0 && row< n && col >=0 && col < m)
return true;
return false;
}
bool dfs(vector<string> path ,int row ,int col , int count, int check[][30]){
string temp = "";
char c = (char)(col + 65);
temp.pb(c);
temp.append(to_string(row+1));
path.pb(temp);
if(count == m*n){
for(int i = 0 ; i < path.size() ; i++)
cout<<path[i];
cout<<endl;
return true;
}
int p , q;
for(int i = 0 ; i < 8 ; i++){
p = row + x[i] , q = col + y[i];
if(valid(p , q) && !check[p][q]){
check[p][q] = 1;
if(dfs( path , p , q , count+1 , check)) return true;
check[p][q] = 0;
}
}
return false;
}
bool path(){
for(int i = 0 ; i < n; i ++){
for(int j = 0 ; j < m ; j++){
int check[30][30];
memset(check , 0 , sizeof check);
check[i][j] = 1;
vector<string> path;
if(dfs(path , i , j ,1 , check )) return true;
}
}
return false;
}
int main(){
int t;
cin>>t;
while(t--){
cin>>n>>m;
if(!path()) cout<<-1<<endl;
}
return 0;
}