专栏文章

春季搜索+位运算章节测试

个人记录参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@mipje0de
此快照首次捕获于
2025/12/03 12:58
3 个月前
此快照最后确认于
2025/12/03 12:58
3 个月前
查看原文

本次一共五道题,难度橙、橙、黄、黄、黄。

CPP
|U555204 春季章节测试-2025-位运算|

|U555207 春季章节测试-奇数次-位运算|

|U555226 春季章节测试-选数-深度优先搜索|

|U555228 春季章节测试-离开中山路-广度优先搜索|

|U555230 春季章节测试-最短路迷宫-广度优先搜索|


1.U555204 春季章节测试-2025-位运算

CPP
#include<bits/stdc++.h>
using namespace std;
int main()
{
//	freopen("T1.in", "r", stdin);
//	freopen("T1.out", "w", stdout);
	int x;
	cin>>x;
	for(int i=0;i<2025;i++){
		if((x & i) + (x | i)==2025){
			cout<<i;
			return 0;
		}
	}
	cout<<"-1";
	return 0;
}

2.U555207 春季章节测试-奇数次-位运算

CPP
#include<bits/stdc++.h>
using namespace std;
int main(){
    //freopen("T2.in", "r", stdin);
    //freopen("T2.out", "w", stdout);
    long long n;
	  cin>>n;
	  long long sum=0;
	  for(long long i=1;i<=n;i++){
  		long long x;
  		cin>>x;
  		sum^=x;
	}
	cout<<sum;
	return 0;
}

3.U555226 春季章节测试-选数-深度优先搜索

CPP
#include<bits/stdc++.h>
using namespace std;
int aa[50];
int ans=0;
int n,k;
bool node(int x){
	if(x<=1){
		return 0;
	}
	for(int i=2;i*i<=x;i++){
		if(x%i==0){
			return 0;
		} 
	}
	return 1;
}
void dfs(int a,int b,int c){
	if(b==k){
		if(node(c)){
			ans++;
		}
		return ;
	}
	if(a>n){
		return; 
	}
	dfs(a+1,b+1,c+aa[a]);
	dfs(a+1,b,c);
	return ;
}
int main(){
  //freopen("T2.in", "r", stdin);
  //freopen("T2.out", "w", stdout);
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>aa[i];
	}
	dfs(1,0,0);
	cout<<ans;
	return 0;
}

4.U555228 春季章节测试-离开中山路-广度优先搜索

CPP
#include<bits/stdc++.h>
using namespace std;
int dx[4]={0,0,1,-1};
int dy[4]={-1,1,0,0};
int a[1025][1025];
int sx,sy,fx,fy;
int n;
struct node{
	int x,y,qaz;
};
bool qwe[1005][1005];
int bfs(int sx,int sy){
//	memset(qwe,0,sizeof(qwe));
	queue<node>Q;
	node cur={sx,sy,0};
	qwe[sx][sy]=1;
	Q.push(cur);
	while(Q.empty()==0){
		cur=Q.front();
		Q.pop();
//		cout << cur.qaz << endl;
		if(cur.x == fx && cur.y == fy){
//			cout << cur.qaz << endl;
			return cur.qaz;
		}
		for(int i=0;i<=3;i++){
			int nx=cur.x+dx[i];
			int ny=cur.y+dy[i];
			if(nx>=1 && nx<=n && ny>=1 && ny<=n && a[nx][ny]!=1 && qwe[nx][ny]!=1){
				node abc ={nx,ny,cur.qaz+1};
				Q.push(abc);
				qwe[nx][ny]=1;
			}
		}
	}
	return -1;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
		}
	}
	cin>>sx>>sy>>fx>>fy;
	cout<<bfs(sx,sy);
}

评论

0 条评论,欢迎与作者交流。

正在加载评论...