暑假刷题第20天--8/3

B-序列的与和_2023河南萌新联赛第(四)场:河南大学 (nowcoder.com)(dfs)

cpp 复制代码
#include<iostream>
#include<string>
using namespace std;
#define ull unsigned long long
int n,k;
ull a[21];
ull ans=0;
int check(ull sum){
	int q=0;
	while(sum){
		q++;
        sum&=(sum-1);
	}
	return q;
}
void dfs(int x,ull sum){
	if(check(sum)==k)ans++;
	for(int i=x+1;i<=n;i++){
		dfs(i,sum&a[i]);
	}
}
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		dfs(i,a[i]);
	}
	cout<<ans<<endl;
}

D-幂运算_2023河南萌新联赛第(四)场:河南大学 (nowcoder.com)(快速幂类似)

cpp 复制代码
#include<iostream>
#include<string>
using namespace std;
long long qowe(long long a,long long n,long long p){//指数幂
	long long ans=2;
	while(n){
		ans=(long long)ans*ans%p;
			a=(long long)a*a%p;
			n=n-1;
	}
	return ans;
}
int main(){
    int n,k;
	cin>>n>>k;
	cout<<qowe(2,n,k);
}

P4554 小明的游戏 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)(双端队列bfs--模板)

cpp 复制代码
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<cstring>
using namespace std;
int n,m;
struct node{
	int xx,yy,step;
};
string s[505];
int x1,y1,a,b;
bool vis[505][505];
int cnt[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int in(int x,int y){
	if(x<0||y<0||x>=n||y>=m)return 0;
	return 1;
}
int bfs(int x,int y){
	node c;
	c.xx=x,c.yy=y,c.step=0;
	deque<node>q;
	q.push_front(c);
	while(!q.empty()){
		node temp=q.front();
		q.pop_front();
		node now;
		vis[temp.xx][temp.yy]=true;
		if(temp.xx==a&&temp.yy==b)return temp.step;
		for(int i=0;i<4;i++){
			now.xx=temp.xx+cnt[i][0];
			now.yy=temp.yy+cnt[i][1];
			if(!vis[now.xx][now.yy]&&in(now.xx,now.yy)&&s[temp.xx][temp.yy]==s[now.xx][now.yy]){
				now.step=temp.step;
				q.push_front(now);
			}
			else if(!vis[now.xx][now.yy]&&in(now.xx,now.yy)&&s[temp.xx][temp.yy]!=s[now.xx][now.yy]){
				now.step=temp.step+1;
				q.push_back(now);
			}
		}
	}
	return 0;
}
int main(){
	while(cin>>n>>m&&(n||m)){
		for(int i=0;i<n;i++){
			cin>>s[i];
		}
		cin>>x1>>y1>>a>>b;
		memset(vis,0,sizeof(vis));
		cout<<bfs(x1,y1)<<"\n";
	}
}

Labyrinth - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)(双端队列bfs)

cpp 复制代码
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<cstring>
#include<queue>
using namespace std;
const int N=2005;
int n,m;
struct node{
	int xx,yy,step1,step2;
};
string s[N];
int x1,y1,a,b;
bool vis[N][N];
int cnt[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
int ans=1;
int in(int x,int y){
	if(x<0||y<0||x>=n||y>=m)return 0;
	return 1;
}
int bfs(int x,int y){
	node c;
	c.xx=x,c.yy=y,c.step1=0,c.step2=0;
	deque<node>q;
	q.push_front(c);
	vis[x][y]=true;
	while(!q.empty()){
		node temp=q.front();
		q.pop_front();
		node now;
		for(int i=0;i<4;i++){
			now.xx=temp.xx+cnt[i][0];
			now.yy=temp.yy+cnt[i][1];
			if(!in(now.xx,now.yy)||vis[now.xx][now.yy]||s[now.xx][now.yy]=='*')continue;
			if(i==0||i==1){
				now.step1=temp.step1;
				now.step2=temp.step2;
				q.push_front(now);
				vis[now.xx][now.yy]=true;
				ans++;
			}
			else if(i==2){
				if(temp.step1>=a)continue;
				now.step1=temp.step1+1;
				now.step2=temp.step2;
				q.push_back(now);
				vis[now.xx][now.yy]=true;
				ans++;
			}
			else if(i==3){
				if(temp.step2>=b)continue;
				now.step1=temp.step1;
				now.step2=temp.step2+1;
				q.push_back(now);
				vis[now.xx][now.yy]=true;
				ans++;
			}
		}
	}
	return 0;
}
int main(){
	cin>>n>>m;
        cin>>x1>>y1>>a>>b;
		for(int i=0;i<n;i++){
			cin>>s[i];
		}
		bfs(x1-1,y1-1);
		cout<<ans<<"\n";
}

175. 电路维修 - AcWing题库(双端队列bfs--不是很理解待重新补)

cpp 复制代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <deque>
using namespace std;
typedef pair<int, int > PII;
int n, m;
const int N = 510;
char g[N][N];
int d[N][N];
inline bool check(int x, int y)
{
    if(x >= 0 && x <= n && y >= 0 && y <= m) return true;
    return false;
}
inline int bfs()
{
    memset(d, 0x3f, sizeof d);
    deque<PII> dq;
    dq.push_front({0, 0});
    d[0][0] = 0;
    int moved[4][2] = {{-1, -1},{-1, 1},{1, 1},{1, -1}};
    int movei[4][2] = {
      {-1, -1},
      {-1, 0},
      {0, 0},
      {0, -1},
    };
    char cp[] = "\\/\\/";

    while(dq.size())
    {
        auto t = dq.front();
        dq.pop_front();
        int x = t.first, y = t.second;
        for(int i = 0; i < 4; i ++)
        {
            int a = x + moved[i][0], b = y + moved[i][1];
            if(check(a, b))
            {
                int j = x + movei[i][0], k = y + movei[i][1];
                int w;
                if(g[j][k] != cp[i]) w = 1;
                else w = 0;

                if(d[a][b] > d[x][y] + w)
                {
                    d[a][b] = d[x][y] + w;
                    if(w) dq.push_back({a, b});
                    else dq.push_front({a, b});
                }
            }
        }
    }

    if(d[n][m] == 0x3f3f3f3f) return -1;
    else return d[n][m];
}

inline void solve()
{
    cin >> n >> m;
    for(int i = 0; i < n; i ++) cin >> g[i];

    int res = bfs();

    if(res == -1) puts("NO SOLUTION");
    else cout << res << endl;
}
int main()
{
    int t;
    cin >> t;
    while( t -- ) solve();
    return 0;
}
相关推荐
Felven18 分钟前
A. Ideal Generator
java·数据结构·算法
MoonBit月兔1 小时前
双周报Vol.70: 运算符重载语义变化、String API 改动、IDE Markdown 格式支持优化...多项更新升级!
ide·算法·哈希算法
How_doyou_do1 小时前
树状数组底层逻辑探讨 / 模版代码-P3374-P3368
数据结构·算法·树状数组
小鹿鹿啊1 小时前
C语言编程--14.电话号码的字母组合
c语言·开发语言·算法
小O的算法实验室2 小时前
2024年ESWA SCI1区TOP:量子计算蜣螂算法QHDBO,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
C语言魔术师2 小时前
509. 斐波那契数
算法·动态规划
Wendy_robot2 小时前
【前缀和计算和+哈希表查找次数】Leetcode 560. 和为 K 的子数组
c++·算法·leetcode
o独酌o2 小时前
算法习题-力扣446周赛题解
算法·leetcode
一只鱼^_2 小时前
第十六届蓝桥杯大赛软件赛省赛 C/C++ 大学B组 [京津冀]
c语言·数据结构·c++·算法·贪心算法·蓝桥杯·动态规划