Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
1.4 kB
1
Indexable
Never
#include<iostream>
using namespace std;
int visited[5][5];
int arr[5][5];
int n, result;
int countt;
int maxx;
bool isSafe(int row, int col, int a[5][5]){
	for(int i=row; i<n; i++){
		if(arr[i][col]==1) break;
		else{
			if(arr[i][col]==2) return false;
		}
	}
	for(int i=row; i>=0; i--){
		if(arr[i][col]==1) break;
		else{
			if(arr[i][col]==2) return false;
		}
	}
	for(int i=col; i<n; i++){
		if(arr[row][i]==1) break;
		else{
			if(arr[row][i]==2) return false;
		}
	}
	for(int i=col; i>=0; i--){
		if(arr[row][i]==1) break;
		else{
			if(arr[row][i]==2) return false;
		}
	}

	if(arr[row][col]==2 || arr[row][col]==1) return false;
	if(arr[row][col]==3) return false;
	return true;
}
void rook(int row, int col, int a[5][5]){
	//int check=false;
	for(int i=col; i<n; i++){
		if(isSafe(row, i, a)){
			arr[row][i]=2;
			countt++;
			rook(row, i+1, a);
			if(countt>maxx) maxx=countt;
			countt--;
			arr[row][i]=0;
		}
	}
	if(row==n-1) {
		return;
	}
	rook(row+1, 0, a);
	
}
int main(){
	int t;
	cin>>t;
	for(int tc=1; tc<=t; tc++){
		cin>>n;
		char x;
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				cin>>x;
				if(x=='.') arr[i][j]=0;
				else arr[i][j]=1;
				visited[i][j]=0;
			}
		}
		countt=0; maxx=0;
		int m=0;
		int dem=0;

		rook(0,0, visited);
	
		cout<<"Case #"<<tc<<endl;
		//cout<<m<<endl;
		cout<<maxx<<endl;
	}
	return 0;
}