Dressing

Dressing

Wangpeng has N clothes, M pants and K shoes so theoretically he can have N×M×K different combinations of dressing.
One day he wears his pants Nike, shoes Adiwang to go to school happily. When he opens the door, his mom asks him to come back and switch the dressing. Mom thinks that pants-shoes pair is disharmonious because Adiwang is much better than Nike. After being asked to switch again and again Wangpeng figure out all the pairs mom thinks disharmonious. They can be only clothes-pants pairs or pants-shoes pairs.
Please calculate the number of different combinations of dressing under mom’s restriction.
 

Input

There are multiple test cases.
For each case, the first line contains 3 integers N,M,K(1≤N,M,K≤1000) indicating the number of clothes, pants and shoes.
Second line contains only one integer P(0≤P≤2000000) indicating the number of pairs which mom thinks disharmonious.
Next P lines each line will be one of the two forms“clothes x pants y” or “pants y shoes z”.
The first form indicates pair of x-th clothes and y-th pants is disharmonious(1≤x≤N,1 ≤y≤M), and second form indicates pair of y-th pants and z-th shoes is disharmonious(1≤y≤M,1≤z≤K).
Input ends with “0 0 0”.
It is guaranteed that all the pairs are different.
 

Output

For each case, output the answer in one line.
 

Sample Input

2 2 2
0
2 2 2
1
clothes 1 pants 1
2 2 2
2
clothes 1 pants 1
pants 1 shoes 1
0 0 0

Sample Output

8
6
5

Source

 
题目大意:
  第一行输入N,M,K,表示有N件衣服,M件裤子,K双鞋子,每次搭配一套衣服是从中各选取一件进行搭配,然后,第二行是表示不能搭配的的情况,输入P,表示有P种情况不能够搭配。而且输入的要求只有clothes-pants pairs or pants-shoes pairs.表示输入的只会是:衣服a-裤子b或者裤子a-鞋子b这样的情况。
  最后,问你还能够搭配的情况有几种,输出答案即可。
解法:
  用Map1[i][j],Map2[i][j]来表示衣服-裤子和裤子-鞋子的搭配的情况。如果直接遍历N*M*K的话会超时,则是不可行的。因此,还需要设置一个状态数组Sign[i],用来表示搭配裤子i,所还能够搭配鞋子的情况个数。刚开始把Map1[i][j]和Map[i][j]初始化为1,在输入P不能够搭配的情况的时候,分别把对应的情况的Map1[i][j]或者Map2[i][j]变成0,表示不可行。如果是输入裤子-鞋子的情况的话,还需要根据Map2[i][j]的情况,改变状态数组的情况。
  输出答案的话,只需要遍历N*M次(每次分别在累计上Sign[Mi]的数值即可)。累加和即是答案、
 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 using namespace std;
 5 int a,b,c;
 6 int Map1[1010][1010];
 7 int Map2[1010][1010];
 8 int Sign[1010];
 9 void Cread()/*初始化*/
10 {
11     int i,j,k;
12     for(i=0;i<=a;i++)
13         for(j=0;j<=b;j++)
14             Map1[i][j]=1;/*初始化衣服-裤子的情况*/
15     for(i=0;i<=b;i++)
16     {
17         Sign[i]=c;/*初始化裤子i所能够搭配的情况*/
18         for(j=0;j<=c;j++)
19             Map2[i][j]=1;/*初始化裤子-鞋子的情况*/
20     }
21 }
22 int main()
23 {
24     int i,j,k,M;
25     int aa,bb;
26     char aaa[10],bbb[10];
27     while(scanf("%d%d%d",&a,&b,&c)!=EOF)
28     {
29         if(a==0&&b==0&&c==0)break;
30         Cread();/*初始化*/
31         scanf("%d",&M);
32         for(i=0;i<M;i++)
33         {
34             scanf(" %s %d %s %d",aaa,&aa,bbb,&bb);
35             if(aaa[0]=='c'&&bbb[0]=='p')
36             {
37                  Map1[aa][bb]=0;
38             }
39             else
40             {
41                  if(Map2[aa][bb])
42                  {
43                      Sign[aa]--;/*如果这边还没有去除,则--*/
44                      Map2[aa][bb]=0;
45                  }
46             }
47         }
48         int SUM=0;
49         for(i=1;i<=a;i++)
50         {
51             for(j=1;j<=b;j++)
52             {
53                 if(Map1[i][j])
54                     SUM+=Sign[j];
55             }
56         }
57         printf("%d\n",SUM);
58     }
59     return 0;
60 }
View Code

 

posted @ 2015-06-16 15:04  Wurq  阅读(199)  评论(0编辑  收藏  举报