Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.6 kB
0
Indexable
Never
#include<iostream>
using namespace std;
int N;
char arr[4][4];
int ans;
bool safe(int r,int c){
	if(arr[r][c]=='X')return false;
	for(int i=c-1;i>=0;i--){
		if(arr[r][i]=='C')return false;
		else if(arr[r][i]=='X')break;
	}
	for(int i=r-1;i>=0;i--){
		if(arr[i][c]=='C')return false;
		else if(arr[i][c]=='X')break;
	}
	return true;
}
void reset(){
	
}
void backtrack(int k){
	if(k== N*N){
		int cnt=0;
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				if(arr[i][j]=='C'){
					cnt++;
				}
			}
		}
		if (cnt>ans)ans=cnt;
		return;
	}
	if(safe(k/N,k%N)){
		for(int i=0; i<2; i++){
			if(i==0){
				backtrack(k+1);
			}
			if(i==1){
				arr[k/N][k%N]='C';
				backtrack(k+1);
				arr[k/N][k%N]='.';
			}
		}
	}else backtrack(k+1);
}

int main()
{
	freopen("Text.txt","r",stdin);
	int t;
	cin >> t;
	for(int tc= 1; tc <=t; tc ++)
	{
		cin>>N;
		for(int i=0 ; i <N; i++)
		{
			for(int j=0 ; j <N; j++)
			{
				cin >> arr[i][j];
			}
		}
		ans =0;
		backtrack(0);
		cout <<"Case #"<<tc<<endl<< ans<< endl;
	}
	return 0;
}
///////
#include<iostream>
using namespace std;
int M,N;
int a[8][8];
int arr[8];
int ans;
bool safe(int r,int c){
	for(int i=0;i<r;i++){
		if(arr[i]==c)return false;
		if(i-arr[i]==r-c)return false;
		if(arr[i]+i==r+c)return false;
	}
	return true;
}
void backtrack(int k)
{
	if(k== 8){
		int sum=0;
		for(int i=0;i<8;i++){
			sum += a[i][arr[i]];
		}
		if(sum>ans)
			ans=sum;
		return;
	}
	for(int i=0; i<8; i++){
		if(safe(k,i)){
			arr[k]=i;
			backtrack(k+1);
		}
	}
}

int main()
{
	freopen("Text.txt","r",stdin);
	int t;
	cin >> t;
	for(int tc= 1; tc <=t; tc ++)
	{
		int n;
		cin>>n;
		cout << "Case #" << tc<<endl;
		for(int u=0;u<n;u++){
			for(int i=0 ; i <8; i++)
			{
				for(int j=0 ; j <8; j++)
				{
					cin >> a[i][j];
				}
			}
			ans =0;
			backtrack(0);
			cout << ans<< endl;
		}
	}
	return 0;
}
////////
#include<iostream>
#define INF 1000000
using namespace std;
int M,N;
int market[30];
int uudai[40][40];
int giauudai[30];
int arr[30];
int kim[30];
int price;
int kim_need;
int minPrice;

void reset(){
	for(int i = 0; i < 40; i++){
		for(int j = 0; j < 40; j++){
			uudai[i][j] = 0;
		}
	}
	for(int i = 0; i < 30; ++i){
		arr[i] = 0;
		kim[i] = 0;
		market[i] = 0;
		giauudai[i] = 0;
	}
}

void buy(int k){
	price +=giauudai[k];
	for(int i=1;i<= N;i++){
		arr[uudai[k][i]]++;
	}
}

void sell(int k){
	price -= giauudai[k];
	for(int i=1;i<=N;i++){
		arr[uudai[k][i]]--;
	}
}

void backtrack(int k)
{
	if(price>minPrice){return;}

	if(k== (M+1)){
		int sum=0;
		for(int i=1;i<=kim_need;i++){
			if(arr[kim[i]]==0){
				sum += market[kim[i]];
			}
		}
		int sumT = sum + price;
		if(sumT < minPrice){
			minPrice = sumT;
		}
		return;
	}
	for(int i=0; i<2; i++){
		if(i==0){
			backtrack(k+1);
		}
		if(i==1){
			buy(k);
            backtrack(k+1);
			sell(k);
		}
	}
}

int main()
{
	freopen("Text.txt","r",stdin);
	int T;
	cin >> T;
	for(int tc= 1; tc <=T; tc ++)
	{
		//input
		reset();
		cin >> N;
		market[0] = 0;
		for(int i=1 ; i <=N; i++)
		{
			cin >> market[i];
		}
		cin>>M;
		for(int i=1 ; i <=M; i++)
		{
			int soLinhKien = 0;
			cin >> giauudai[i] >> soLinhKien;
		
			for(int j=1 ; j <= soLinhKien; j++)
			{
				cin>>uudai[i][j];
			}
		}
		cin>>kim_need;

		for(int i=1;i<=kim_need;i++){
			cin>>kim[i];
		}
		minPrice = INF;
		price=0;
		backtrack(1);
		cout << "#" << tc << " " << minPrice << endl;
	}
	return 0;
}