UVA 11045 (14.05.25)

  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 N$ \ge$M. 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 $ \neq$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 MN is multiple of 6, 1$ \le$N$ \le$36, and indicates the number of T-shirts. Number M1$ \le$M$ \le$30, indicates the number of volunteers, with N$ \ge$M. 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;
}


java建一个string类型的数组并填入数据:0.15,0.7,1.52,2.65,4.08,6.06,7.98,9.91,11.86,14.05,15.9,17.84,20.06,21.99,23.94,25.9,27.84,30.06,31.97,33.9,35.61,37.03,38.26,38.99,39.42,39.53,39.53,39.77,40.24,40.8,41.28,41.77,42.3,42.78,43.27,43.75,44.3,44.78,45.27,45.76,46.31,46.79,47.27,47.75,48.31,48.79,49.27,49.76,50.3,50.78,51.27,51.76,52.31,52.8,53.28,53.77,54.25,54.74,55.3,55.78,56.27,56.75,57.3,57.79,58.28,58.76,59.25,59.81,60.29,60.78,61.26,61.75,62.3,62.78,63.27,63.75,64.31,64.79,65.28,65.76,66.24,66.8,67.28,67.76,68.25,68.81,69.28,69.77,70.25,70.81,71.29,71.78,72.26,72.75,73.3,73.79,74.25,74.8,75.29,75.77,76.24,76.8,77.28,77.77,78.25,78.81,79.29,79.78,80.26,80.75,81.31,81.79,82.27,82.76,83.3,83.79,84.27,84.76,85.25,85.8,86.29,86.77,87.26,87.74,88.3,88.78,89.27,89.76,90.31,90.79,91.28,91.76,92.31,92.8,93.28,93.77,94.25,94.81,95.29,95.77,96.25,96.8,97.28,97.77,98.31,98.8,99.28,99.77,100.25,100.8,101.29,101.77,102.26,102.79,103.28,103.76,104.24,104.8,105.28,105.77,106.25,106.81,107.28,107.77,108.26,108.81,109.29,109.77,110.25,110.8,111.28,111.77,112.26,112.81,113.29,113.78,114.26,114.75,115.3,115.78,116.27,116.75,117.3,117.79,118.27,118.76,119.31,119.79,120.28,120.76,121.31,121.8,122.28,122.76,123.31,123.79,124.28,124.76,125.24,125.8,126.28,126.77,127.25,127.8,128.28,128.77,129.25,129.8,130.27,130.75,131.24,131.8,132.28,132.76,133.25,133.8,134.28,134.77,135.26,135.81,136.3,136.78,137.26,137.75,138.3,138.79,139.27,139.76,140.25,140.8,141.29,141.78,142.26,142.75,143.3,143.78,144.26,144.81,145.28,145.76,146.3,146.77,147.25,147.8,148.28,148.76,149.25,149.8,150.29,150.76,151.31,151.79,152.26,152.81,153.28,153.69,
最新发布
05-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值