题意:有N个杯具,,其初始状态中杯具有的是向下,有的是向上。

        对其进行操作,,每次操作就是随意的同时翻转M个杯具,

       问至少要多少步能把所有的杯具都翻为向上。


思路:就是直接进行广搜,只是把要每一个状态进行标记。这里最多就只有10000状态,所以

             速度会很快的。



#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

using namespace  std;

#define N 10004
int n,m;

bool vist[N];
struct my
{
	int num;
	int step;
};

bool bfs(int num)
{
	my cur;
	cur.num=num;
	cur.step=0;
	queue<my> q;
	q.push(cur);
	memset(vist,false,sizeof(bool)*N);
	vist[num]=true;
	while (!q.empty())
	{
		my d=q.front();
		q.pop();
		if (d.num==0)
		{
			cout<<d.step<<endl;
			return false;
		}
		int k=min(m,d.num);
		for (int i=0;i<=k;i++) if (n-d.num>=m-i && !vist[d.num+m-i-i])
		{
			cur.num=d.num+m-i-i;
		 	vist[cur.num]=true;
			cur.step=d.step+1;
			q.push(cur); 
		}
	}
	return true;
}

int main()
{
	freopen("in.txt","r",stdin);
	int i,j,k;
	int num;
	int nc;
	cin>>nc;
	while (nc--)
	{
		num=0;
		cin>>n>>m;
		for (i=0;i<n;i++)
		{
			scanf("%d",&k);
			num+=k;
		}
		if (bfs(num))
			cout<<"Poor Girl"<<endl;
		
	}
	
	return 0;
}




Chiara’s “Beiju”

Time Limit: 1000 ms Memory Limit: 65535 kB Solved: 30 Tried: 92


Submit




Status




Best Solution




Back


Description



Chiara has N cups, numbered from 1 to N. Cups are laid on a table, each upward or downward. Chiara enjoys turning cups. Every time she will select M cups and play her silly game. If a cup is upward, it will be downward after her turning, and vice versa. One day, Chiara's N friends are coming to see her. She'd like to use the N cups for coffee serving (Chiara is addicted to coffee desperately, that's...OK). However, the cups are not all upward. She is wondering if it is possible to make them all upward after several turnings. Since time is limited, you have to give her the minimum number of steps.
Note: Chiara has to turn M cups each time.



Input



There are multiple cases. The first line is an integer T(1 <= T <= 20), then following T cases.
For each case:
The first line has two integers N(1 <= N <= 10000),M(1 <= M <= 10, M <= N). N is the total number of cups and M is the number of cups Chiara has to turn each time.
The second line has N numbers, either 0 or 1(0 for upward and 1 for downward), separated by spaces.



Output



If Chiara can achieve her goal, please output the minimum number of turnings, otherwise print "Poor Girl".



Sample Input



2
6
3
0 0 0 1 1 1
2
2
0 1



Sample Output


1


Poor Girl