Forests

来源:互联网 发布:linux sublime tarbz2 编辑:程序博客网 时间:2024/05/16 01:24
1389.   Forests
If a tree falls in the forest, and there's nobody there to hear, does it make a sound? This classic conundrum was coined by George Berkeley (1685-1753), the Bishop and influential Irish philosopher whose primary philosophical achievement is the advancement of what has come to be called subjective idealism. He wrote a number of works, of which the most widely-read areTreatise Concerning the Principles of Human Knowledge (1710) and Three Dialogues between Hylas and Philonous (1713) (Philonous, the "lover of the mind," representing Berkeley himself).

A forest contains T trees numbered from 1 to T and P people numbered from 1 toP. Standard input consists of a line containing P and T followed by several lines, containing a pair of integersi and j, indicating that person i has heard tree j fall. People may have different opinions as to which trees, according to Berkeley, have made a sound. How many different opinions are represented in the input? Two people hold the same opinion only if they hear exactly the same set of trees. You may assume thatP < 100 and T < 100.


Sample Input

3 41 23 31 32 23 22 4

Output for Sample Input

2

题目解析:
一共3个人 4棵树其中第1个人认为第2和第3棵树倒了其中第2个人认为第2和第4棵树倒了其中第3个人认为第2和第3棵树倒了以上共有2种不同的看法,其中第1个人和第3个人的看法视为一致 
刚看到题目的时候不知道怎么办,后来查阅了别人的代码后明白了,先定义一个二维数组num[][],初始化为0,输入一对i,j就把对应的num[i][j]置为1;然后通过判断数组中有几行不同的0,1组合来输出结果。
#include<iostream>#include<string>#include<stdio.h>#define N 100using namespace std;int main(){int i,j,P,T,num[N][N],count,flag;char s[30];while(cin>>P>>T){getchar();for(i=1;i<=P;i++)for(j=1;j<=T;j++)num[i][j]=0;while(gets(s)&&s[0]){sscanf(s,"%d %d",&i,&j);num[i][j]=1;}count=P; for(i=2;i<=P;i++)          for(j=1;j<i;j++)          {              if(num[j][1]==-1)                continue;              flag=1;              for(int k=1;k<=T;k++)                if(num[i][k]!=num[j][k])                 { flag=0;break;}              if(flag)              {                  count--;                  num[i][1]=-1;              }          }cout<<count<<endl;}system("pause");return 0;}



0 0
原创粉丝点击