Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Candy.cpp #57

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Candy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* Gary is a teacher at XYZ school. To reward his N students he bought a packet of N candies all with different flavours. But the problem is some students like certain flavour while some doesn't. Now Gary wants to know the number of ways he can distribute these N candies to his N students such that every student gets exactly one candy he likes.
Input Format :
Line 1 : An integer N (1<= N <= 16) denoting number of students and candies.
Next N lines : N integers describing the preferences of one student. 1 at i'th (0 <= i < N) position denotes that this student likes i'th candy , 0 means he doesn't.
Assume input to be 0-indexed based.
Output Format :
Return the number of ways Gary can distribute these N candies to his N students such that every student gets exactly one candy he likes.``
Sample Input:
3
1 1 1
1 1 1
1 1 1
Sample Output:
6 */



int candies(int like[][MAXN], int n, int person, int mask, int *dp)
{
if (person >= n)
{
return 1;
}
if(dp[mask]!=0)
{
return dp[mask];
}
int ans = 0;
for (int i = 0; i < n; i++)
{
if (!(mask & (1 << i)) && like[person][i])
{
ans += candies(like, n, person + 1, mask | (1 << i), dp);
}
}
dp[mask]=ans;
return ans;
}
long long solve(int like[][MAXN],int n)
{
int *dp = new int[1 << n];
for (int i = 0; i < (1 << n); i++)
{
dp[i] = 0;
}
int ans= candies(like, n, 0, 0, dp);
delete[]dp;
return ans;
}