Forests

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 are Treatise 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 to P. Standard input consists of a line containing P and T followed by several lines, containing a pair of integers i 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 that P < 100 and T < 100.


Sample Input

3 4
1 2
3 3
1 3
2 2
3 2
2 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 100
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值