可恶的数字逻辑

Accept:10     Submit:34
Time Limit:1000MS     Memory Limit:65536KB
Description
为了准备期末考试了,laprovence正被讨厌的数字逻辑搞的头昏脑胀,尤其后面的一堆乱七八糟的概念,简直不知所云@#~!*&~
这不有道简单的题就把他给难住了,题目大意是这样的:给出n个发光二极管,每个二极管都有两种状态,亮(on)与灭(off),然后给出一组每两个二极管之间的约束关系,
约束关系有以下3 种
1)a and b 表示第a个与第b个二极管必须同时亮
2)a or b表示第a个与第b个二极管至少有一个亮
3)a xor b 表示第a个与第b个二极管必须是一个亮,一个灭



Input
第一行两个整数n(二极管的个数,n<=10),m(m组约束关系,m<=50)
然后m行约束关系以a and b,a or b,a xor b的形式给出
多组测试数据,当n=0,m=0时结束



Output
输出每个二极管的状态(一行,每两个状态之间用空格隔开,最后一个不要空格,保证只有一组解),如果没有解输出No solution

Sample Input

2 1
1 and 2
3 3
1 and 2
2 xor 3
1 and 3
0 0


Sample Output

on on
No solution


View Code
  1 #include<iostream>
2 using namespace std;
3 struct node
4 {
5 int l;
6 char ch[5];
7 int r;
8 }map[60];
9 int ans[12];
10 int n,m;
11 bool finds()
12 {
13 int i;
14 for(i=0;i<m;i++)
15 {
16 if(map[i].ch[0]=='a')
17 {
18 if(ans[ map[i].l ]!=0 && ans[ map[i].r ]!=0 )
19 { ans[ map[i].l ]=1; ans[ map[i].r ]=1; continue;}
20 else return 0;
21 }
22 if(map[i].ch[0]=='x')
23 {
24 if(ans[ map[i].l ]==-1 && ans[ map[i].r ]==0)//-1 0
25 {
26 ans[ map[i].l ]=1;continue;
27 }
28 if(ans[ map[i].l ]==-1 && ans[ map[i].r ]==1)//-1 1
29 {
30 ans[ map[i].l ]=0;continue;
31 }
32 if(ans[ map[i].l ]==0 && ans[ map[i].r ]==-1)//0 -1
33 {
34 ans[ map[i].r ]=1;continue;
35 }
36 if(ans[ map[i].l ]==0 && ans[ map[i].r ]==0)//0 0
37 {
38 return 0;
39 }
40 if(ans[ map[i].l ]==0 && ans[ map[i].r ]==1)//0 1
41 {
42 continue;
43 }
44 if(ans[ map[i].l ]==1 && ans[ map[i].r ]==-1)//1 -1
45 {
46 ans[ map[i].r ]=0;continue;
47 }
48 if(ans[ map[i].l ]==1 && ans[ map[i].r ]==0)//1 0
49 {
50 continue;
51 }
52 if(ans[ map[i].l ]==1 && ans[ map[i].r ]==1)//1 1
53 {
54 return 0;
55 }
56 }
57 if(map[i].ch[0]=='o')
58 {
59 if(ans[ map[i].l ]==-1 && ans[ map[i].r ]==0)//-1 0
60 {
61 ans[ map[i].l ]=1;continue;
62 }
63 if(ans[ map[i].l ]==0 && ans[ map[i].r ]==-1)//0 -1
64 {
65 ans[ map[i].r ]=1;continue;
66 }
67 if(ans[ map[i].l ]==0 && ans[ map[i].r ]==0)//0 0
68 {
69 return 0;
70 }
71 }
72 }
73 return 1;
74
75 }
76 int main()
77 {
78 int i;
79 while(cin>>n>>m&&(n||m))
80 {
81 memset(ans,-1,sizeof(ans));
82 for(i=0;i<m;i++)
83 {
84 cin>>map[i].l>>map[i].ch>>map[i].r;
85 }
86 for(i=1;i<=n;i++)
87 {
88 if(ans[i]==-1)
89 {
90 if(finds()) i=0;
91 else break;
92 }
93 }
94 if(i>n)
95 {
96 for(i=1;i<=n;i++)
97 {
98 if(ans[i])
99 cout<<"on";
100 else
101 cout<<"off";
102 if(i!=n)
103 {
104 cout<<' ';
105 }
106 }
107 cout<<endl;
108 }else
109 cout<<"No solution"<<endl;
110 }
111 return 0;
112 }

 

posted @ 2012-03-07 14:24  qijinbiao1  阅读(191)  评论(0编辑  收藏  举报