ybtoj:深度優先搜尋

cathy_zro發表於2024-11-22

A:

點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;

int t,ss=0,ans=0x7fffffff;


int n;
int w[26];

void dfs(int a,int b,int c){//遍歷人數,選擇人數,總體重
 
	if(b==n/2){
		ans=min(ans,abs(c*2-ss));
		
	} 
	if(a>n)
	{
		return;
	}
	dfs(a+1,b+1,c+w[a]);
	dfs(a+1,b,c);
	
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		ans=0x7fffffff;
		ss=0;
		for(int i=1;i<=n;i++)
		{
			cin>>w[i];
			ss+=w[i];
			
		}
		dfs(1,0,0);
		cout<<ans<<endl;				
	}
	return (0^0);
}

B:

點選檢視程式碼
#include<bits/stdc++.h>
#define FQR ;
#define yeyou26 int
#define zyssss char
#define lj 10
#define lmt for
#define xhc777 {
#define zwh }
#define ljr cout
#define fdsama return
#define hnb using namespace std

hnb  FQR  
const yeyou26 N=10086  FQR  
inline yeyou26 f(yeyou26 x,yeyou26 y) xhc777 
	fdsama x/3*3+y/3  FQR  
 zwh 
yeyou26 cnt[N],num[N],tot,mp[lj][lj],a[lj],b[lj],c[lj]  FQR  
zyssss s[81]  FQR  
void change(yeyou26 x,yeyou26 y,yeyou26 v) xhc777 
	a[x]^=(1<<v)  FQR  
	b[y]^=(1<<v)  FQR  
	c[f(x,y)]^=(1<<v)  FQR  
 zwh 
zyssss ycm(yeyou26 idx) xhc777 
	if(idx==tot+1) fdsama true  FQR  
	yeyou26 Minv=0x3f3f3f3f,x=0,y=0  FQR  
	lmt(yeyou26 i=0  FQR  i<9  FQR  i++) xhc777 
		lmt(yeyou26 j=0  FQR  j<9  FQR  j++) xhc777 
			if(mp[i][j]!=-1) continue  FQR  
			yeyou26 sstzer=a[i] & b[j] & c[f(i,j)]  FQR  
			if(cnt[sstzer]<Minv) Minv=cnt[sstzer],x=i,y=j  FQR  
		 zwh 
	 zwh 
	yeyou26 sstzer=a[x]&b[y]&c[f(x,y)]  FQR  
	lmt(  FQR  sstzer  FQR  sstzer-=sstzer&-sstzer) xhc777 
		yeyou26 val=num[sstzer&-sstzer]  FQR  
		mp[x][y]=val  FQR  
		change(x,y,val)  FQR  
		if(ycm(idx+1)) fdsama true  FQR  
		change(x,y,val)  FQR  
		mp[x][y]=-1  FQR  
	 zwh 
	fdsama false  FQR  
 zwh 

void work() xhc777 
	tot=0  FQR  
	lmt(yeyou26 i=0  FQR  i<9  FQR  i++) xhc777 
		a[i]=b[i]=c[i]=(1<<9)-1  FQR  
	 zwh 
	lmt(yeyou26 i=0  FQR  i<9  FQR  i++) xhc777 
		lmt(yeyou26 j=0  FQR  j<9  FQR  j++) xhc777 
			zyssss ch=s[i*9+j]  FQR  
			if(ch=='.') tot++,mp[i][j]=-1  FQR  
			else mp[i][j]=ch-'1',change(i,j,mp[i][j])  FQR  
		 zwh 
	 zwh 
	ycm(1)  FQR  
	lmt(yeyou26 i=0  FQR  i<9  FQR  i++) xhc777 
		lmt(yeyou26 j=0  FQR  j<9  FQR  j++) xhc777 
			ljr<<mp[i][j]+1  FQR  
		 zwh 
	 zwh 
	ljr<<"\n"  FQR  
 zwh 
yeyou26 main() xhc777 
	ios::sync_with_stdio(0)  FQR  
	cin.tie(0)  FQR  
	ljr.tie(0)  FQR  
	
	lmt(yeyou26 i=0  FQR  i<9  FQR  i++) num[1<<i]=i  FQR  
	lmt(yeyou26 i=0  FQR  i<(1<<9)  FQR  i++) xhc777 
		lmt(yeyou26 j=i  FQR  j  FQR  j-=j&-j) xhc777 
			cnt[i]++  FQR  
		 zwh 
	 zwh 
	while(cin>>s && s[0]!='e') work()  FQR  
	fdsama 0  FQR  
 zwh 

C:

點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
int n,ovo,num[27];
char s[4][27],ch[27];
bool vis[27],used[27];

bool check(){
	int x=0;
	for(int i=n;i>=1;i--){
		
		int a=num[s[1][i]-'A'],
		b=num[s[2][i]-'A'],
		c=num[s[3][i]-'A'];
		if(a!=-1 && b!=-1 && c!=-1){
			if(x!=-1){
				if((a+b+x)%n!=c){
					return false;
				}
				if(i==1 && a+b+x>=n){
					return false;
				}
				x=(a+b+x)/n;
			}
			else{
				if((a+b+0)%n!=c && (a+b+1)%n!=c) return false;
				if(i==1 && a+b>=n) return false;
			}
		}
		else x=-1;
	}
	return true;
}

bool dfs(int x){
	if(x==ovo+1) return true;
	for(int i=0;i<n;i++){
		if(!used[i]){
			num[ch[x]-'A']=i;
			used[i]=true;
			if(check() && dfs(x+1)){
				return true;
			}
			num[ch[x]-'A']=-1;
			used[i]=false;
		}
	}
	return false;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	memset(num,-1,sizeof(num));
	cin>>n;
	for(int i=1;i<=3;i++){
		cin>>s[i]+1;
	}
	for(int j=n;j>=1;j--){
		for(int i=1;i<=3;i++){
			if(!vis[s[i][j]-'A']){
				vis[s[i][j]-'A']=true;
				ch[++ovo]=s[i][j];
			}
		}
	}
	dfs(1);
	for(int i=0;i<n;i++){
		cout<<num[i]<<" ";
	}
	return 0;
}

D:

點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
int ans=2e9;
int n,m;
void dfs(int t,int s,int v,int lr,int lh){
	if(s>=ans) return ;
	if(t==m+1 && v==n){
		ans=min(ans,s);
		return ;
	}
	if(v>=n) return ;
	int k=m-t+1;
	if(k*lr*lr*lh+v<n) return;
	if(k*2+s>ans) return ;
	if(t==1){
		for(int r=lr;r>=m;r--){
			for(int h=m;h<=lh;h++){
				dfs(t+1,s+r*r+2*h*r,v+r*r*h,r,h);
			}
		}
	}
	else{
		for(int r=lr-1;r>=m-t+1;r--){
			for(int h=m-t+1;h<lh;h++){
				dfs(t+1,s+2*r*h,v+r*r*h,r,h);
			}
		}
	}
}
int main(){
	cin>>n>>m;
	dfs(1,0,0,28,28);
	if(ans==2e9) putchar('0');
	else{
		cout<<ans<<'\n';
	}
	return 0;
}

E:

點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
const int maxn=1.3e6;
int n,m,u,v;
int a[maxn],b[2],c[2][maxn];
void dfs(int bo,int x,int t,int s){
	if(x>t){
		c[bo][++b[bo]]=s;
		return ;
	}
	dfs(bo,x+1,t,s);
	dfs(bo,x+1,t,s+a[x]);
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>n>>m;
	u=n>>1;
	for(int i=1;i<=u;i++){
		cin>>a[i];
	}
	dfs(0,1,u,0);
	v=n-u;
	for(int i=1;i<=v;i++){
		cin>>a[i];
	}
	dfs(1,1,v,0);
	sort(c[1]+1,c[1]+b[1]+1);
	int ans=0;
	for(int i=1;i<=b[0];i++){
		if(m>=c[0][i]){
			int x=upper_bound(c[1]+1,c[1]+b[1]+1,m-c[0][i])-c[1]-1;
			ans=max(ans,c[0][i]+c[1][x]);
		}
	}
	cout<<ans<<'\n';
	return 0;
}

F:

點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
int n,t;
int ans[6][6]={
	{0,0,0,0,0,0},
	{0,1,1,1,1,1},
	{0,0,1,1,1,1},
	{0,0,0,2,1,1},
	{0,0,0,0,0,1},
	{0,0,0,0,0,0}
};
int nxtx[]={1,1,2,2,-2,-2,-1,-1};
int nxty[]={2,-2,1,-1,1,-1,2,-2};
int a[10][10],k;
int judge;
int check(){
	for(int i=1;i<=5;i++){
		for(int j=1;j<=5;j++){
			if(ans[i][j]!=a[i][j]) return 0;
		}
	}
	return 1;
}

int test(int step){
	int cnt=0;
	for(int i=1;i<=5;i++){
		for(int j=1;j<=5;j++){
			if(ans[i][j]!=a[i][j]){
				if(++cnt+step>k) return 0;
			}
		}
	}
	return 1;
}

void astar(int step,int x,int y,int pre){
	if(step==k){
		if(check()) judge=1;
		return ;
	}
	if(judge) return ;
	for(int i=0;i<8;i++){
		int nx=x+nxtx[i],ny=y+nxty[i];
		if(nx<1 || nx>5 || ny<1 || ny>5 || i+pre==7){
			continue;
		}
		swap(a[x][y],a[nx][ny]);
		if(test(step) && !judge){
			astar(step+1,nx,ny,i);
		}
		swap(a[x][y],a[nx][ny]);
	}
	
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--){
		int x,y;
		judge=0;
		for(int i=1;i<=5;i++){
			char ss[7];
			cin>>ss;
			for(int j=0;j<5;j++){
				if(ss[j]=='*') a[i][j+1]=2,x=i,y=j+1;
				else a[i][j+1]=ss[j]-'0';
			}
		}
		for(k=1;k<=15;k++){
			astar(0,x,y,-1);
			if(judge){
				cout<<k<<"\n";
				break;
			}
		}
		if(!judge) cout<<"-1\n";
	}
	
	return 0;
}

G:

點選檢視程式碼
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;

const int N=13;
const int prime[N]={2,3,5,7,11,13,17,19,23,29,31,37,41};
int n;
int ndiv=0;
int ans=0;

void dfs(const int &cur,const int &prod,const int &limd,const int &curd,const int &nd){
	int res=nd*(curd+1);
	
	if(res>ndiv || (res==ndiv && prod<ans)) {
		ndiv=res,ans=prod;
	}
	
	if(curd+1<=limd && prod*prime[cur]<=n) 
		dfs(cur,prod*prime[cur],limd,curd+1,nd);
		
		
	if(prod*prime[cur+1]<=n){
		dfs(cur+1,prod*prime[cur+1],curd,1,nd*(curd+1));
	}
	
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	dfs(0,1,53,0,1);
	cout<<ans<<'\n';
	return 0;
}

H:

點選檢視程式碼
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)

#define Rep(i,a,b) for(int i=(a);i<(b);++i)

#define rrep(i,a,b) for(int i=(a);i>=(b);--i)

using namespace std;
template <typename T>
inline void read(T &x){
    x=0;char ch=getchar();bool f=0;
    while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    if(f)x=-x;
}
template <typename T,typename ...Args>
inline void read(T &tmp,Args &...tmps){read(tmp);read(tmps...);}
const int N = 20;
int a[N],n,m,b[N];
bool dfs(int l,int r){
    if(r - l + 1 < m)return 0;
    if(r - l + 1 == m){
        int flag = 1;
        rep(i,l,r)if(a[i] != b[i-l+1])flag = 0;
        if(flag)return 1;
        flag = 1;
        rep(i,1,m)if(a[r-i+1] != b[i])flag = 0;
        return flag;
    }
    int c[N],res = 0;
    memcpy(c,a,sizeof(a));
    rep(i,l,r-1){
        if(i - l + 1 > r - i){
            int x = i,y = i + 1;
            for(;y<=r;x--,y++)a[x] += a[y];
            res |= dfs(l,i);
        }
        else{
            int x = i,y = i + 1;
            for(;x>=l;x--,y++)a[y] += a[x];
            res |= dfs(i+1,r);
        }
        memcpy(a,c,sizeof(a));
    }
    return res;
}
signed main(){
    while(scanf("%d",&n) != EOF){
        rep(i,1,n)read(a[i]);
        read(m);
        rep(i,1,m)read(b[i]);
        puts(dfs(1,n) ? "S" : "N");
    }
}

//抄襲絕非良策,理解才是正道

相關文章