005柯南破案之凶手是谁

题目来源:

Problem Statement
Dr. Black was just killed somewhere in his mansion. You are investigating the murder.

There are six suspects: "White", "Green", "Peacock", "Plum", "Scarlett", and "Mustard".

There are eight rooms: "Hall", "Kitchen", "Ballroom", "Conservatory", "Cellar", "Library", "Lounge", and "Study".

Initially (at time 0) all suspects were in the Hall.

Dr. Black was staying in the room murderRoom for the whole time, and that is also where he was killed. An autopsy of his body has shown that the time of the murder was murderTime.

You have collected a chronologically ordered sequence of events. For each valid index i, there is an event of the following form: "At time eventTime[i], the suspect eventPerson[i] moved to the room eventRoom[i]."

Find the murderer! That is, if there is a single suspect who was in the murder room at the time of the murder, return their name. Otherwise, return an empty string.

Definition
Class: Alibi
Method: findMurderer
Parameters: string, int, vector , vector , vector 
Returns: string
Method signature: string findMurderer(string murderRoom, int murderTime, vector  eventTime, vector  eventPerson, vector  eventRoom)
(be sure your method is public)
Limits
Time limit (s): 2.000
Memory limit (MB): 256
Notes
- You can determine the number of events by looking at the length of the eventTime.
Constraints
- murderRoom will be one of the rooms listed in the statement.
- murderTime will be between 1 and 1000, inclusive.
- eventTime will contain between 0 and 50 elements, inclusive.
- eventTime, eventPerson and eventRoom will each contain the same number of elements.
- Each element of eventTime will be between 1 and 1000, inclusive.
- Elements of eventTime will be sorted in a strictly increasing order.
- Elements of eventTime will all be distinct from murderTime.
- Each element of eventPerson will be one of the suspects listed in the statement.
- Each element of eventRoom will be one of the rooms listed in the statement.
Examples
0)
"Library"
10
{ 5, 7, 9 }
{ "Plum", "Scarlett", "Plum" }
{ "Library", "Library", "Cellar" }
Returns: "Scarlett"
Mr. Black was killed in the Library at time 10. At that time, Miss Scarlett was the only suspect in the library -- professor Plum was there earlier but at time 10 he was already in the Cellar. Thus, Scarlett must be the murderer.
1)
"Library"
10
{}
{}
{}
Returns: ""
According to our data, everybody was still in the Hall at the time of the murder, so there is no clear murderer.
2)
"Library"
8
{ 5, 7, 9 }
{ "Plum", "Scarlett", "Plum" }
{ "Library", "Library", "Cellar" }
Returns: ""
At time 8 there were two suspects in the Library so we do not know who's the murderer.
3)
"Hall"
11
{ 2, 4, 6, 8, 10, 12 }
{ "White", "Green", "Peacock", "Plum", "Scarlett", "Mustard" }
{ "Kitchen", "Ballroom", "Conservatory", "Library", "Lounge", "Study" }
Returns: "Mustard"
At time 11 the only suspect still in the Hall was colonel Mustard.
4)
"Library"
10
{3, 7}
{"Plum", "Plum"}
{"Library", "Library"}
Returns: "Plum"
The new room for a person can be the same as their old room. In that case, they just stay where they are. In this case, professor Plum went from the Hall to the Library at time 3 and then nothing changed at time 7.

思路:首先初始化,每个嫌疑犯都在角落里。然后根据题意,死者死亡的时间是什么时候,超过这个时间进入房间的(除非凶手是在角落被杀)可以排除,然后看在死者被杀的时间之前有谁进入了和死者死亡相同的房间里。用一个动态数组维护,最后输出。

#include
using namespace std;
class H
{
public:
	
string findMurderer(string murderRoom, int murderTime, vector  eventTime, vector  eventPerson, vector  eventRoom){
	mapR;
	///cout<<666;
	vector C={"White", "Green", "Peacock", "Plum", "Scarlett","Mustard"};
	for (auto &c:C){
		R[c]="Hall";
	}
	int i;
	for(i=0;imurderTime)break;
		R[eventPerson[i]]=eventRoom[i];
	}
	vectorV;
	for(auto &c:C){
		if(R[c]==murderRoom)V.push_back(c);
	}
		if(V.size()==1)return V[0];
		return "";
	
}	
};
int main() {
    H h;
    string ans="";
	ans=h.findMurderer("Library",10,{3,7},{"Plum","Plum"},{"Library", "Library"});
    //cout<

 

你可能感兴趣的:(noip普及/提高)