1460: [蓝桥杯2019初赛]糖果

2019省赛A组第9题 糖果

题目链接http://oj.ecustacm.cn/problem.php?id=1460

#include<iostream>
#include<cstring>
using namespace std;
int a[110];
int vis[1<<20] = {
    
    0};
int main() {
    
    
	int n, m, k;
	cin >> n >> m >> k;
	int temp;
	//memset(vis, 0, sizeof(vis));
	for (int i = 1; i <= n; i++) {
    
    
		int t = 0;
		for (int j = 1; j <= k; j++) {
    
    
			cin >> temp;
			t |= (1 << temp- 1);
		}
		a[i] = t;
		vis[t] = 1;
	}
	int tot = (1 << m) - 1;//要有括号,找了好久的错误.+、-运算符的优先级高于<< >>位移运算符
	for (int i = 1; i <=tot; i++) {
    
    
		if (vis[i]) {
    
    
			for (int j = 1; j <= n; j++) {
    
    
				if (vis[i | a[j]] == 0 || vis[i | a[j]] > vis[i] + 1) {
    
    
					vis[i | a[j]] = vis[i] + 1;
				}
			}
		}
	}
	if (vis[tot]!=0)
		cout << vis[tot];
	else 
		cout << -1;
	return 0;
}


罗老师代码链接https://blog.csdn.net/weixin_43914593/article/details/112979691

<<、>>、+、-运算符的优先级关系

猜你喜欢

转载自blog.csdn.net/weixin_46028214/article/details/113598574