My T-shirt suits me
My T-shirt suits me |
Our friend Victor participates as an instructor in an environmental volunteer program. His boss asked Victor to distribute N T-shirts to Mvolunteers, one T-shirt each volunteer, where N is multiple of six, and NM. There are the same number of T-shirts of each one of the six available sizes: XXL, XL, L, M , S, and XS. Victor has a little problem because only two sizes of the T-shirts suit each volunteer.
You must write a program to decide if Victor can distribute T-shirts in such a way that all volunteers get a T-shirt that suit them. If N M, there can be some remaining T-shirts.
Input
The first line of the input contains the number of test cases. For each test case, there is a line with two numbers N and M. N is multiple of 6, 1N36, and indicates the number of T-shirts. Number M, 1M30, indicates the number of volunteers, with NM. Subsequently, M lines are listed where each line contains, separated by one space, the two sizes that suit each volunteer (XXL, XL, L, M , S, or XS).
Output
For each test case you are to print a line containing YES if there is, at least, one distribution where T-shirts suit all volunteers, or NO, in other case.
Sample Input
3 18 6 L XL XL L XXL XL S XS M S M L 6 4 S XL L S L XL L XL 6 1 L M
Sample Output
YES NO YES
大致题意:有m件衣服,6种型号,每种m/6件;有n个志愿者,每个志愿者有两种型号适合她,问是否每个志愿者都能找到合适的衣服穿?
思路:暴力回溯搜索~
AC代码:
#include<stdio.h>
#include<string.h>
char shirt[6][10] = {"XXL", "XL", "L", "M", "S", "XS"};
int have[50];
int v[50][2];
int n, m;
int find(char *s) {
int i;
for(i = 0; i < 6; i++) {
if(strcmp(s, shirt[i]) == 0)
return i;
}
}
int dfs(int p) {
int i;
if(p == m)
return 1;
for(i = 0; i < 2; i++) {
if(have[v[p][i]]) {
have[v[p][i]]--;
if(dfs(p+1))
return 1;
have[v[p][i]]++;
}
}
return 0;
}
int main() {
int T;
scanf("%d", &T);
int i, j;
while(T--) {
scanf("%d %d", &n, &m);
for(i = 0; i < 6; i++)
have[i] = n/6;
char str[10];
for(i = 0; i < m; i++) {
for(j = 0; j < 2; j++) {
scanf("%s", str);
v[i][j] = find(str);
}
}
if(dfs(0))
puts("YES");
else
puts("NO");
}
return 0;
}