站点图标 陌路寒暄

A Bug

A Bug

时间: 1ms        内存:128M

描述:

Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.

 

输入:

The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.

输出:

The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.

示例输入:

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

示例输出:

Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

提示:

参考答案(内存最优[1288]):

#include<iostream>
#include<queue>
#include<utility>
#include<set>
using namespace std;

int maxRow, maxCol;
pair <int, int> start;
pair <int, int> end;
int sr, sc, er, ec;
int r[] = {0, 0, 1, -1};
int c[] = {1, -1, 0, 0};
char graph[110][110];

int bfs() {
	set < pair <int, int> > s;
	queue < pair <int, int> > que;
	queue < int > move;
	pair < int, int > posi;
	pair < int, int > step[4];
	que.push(start);
	move.push(0);
	int visited = 0;
	int moving;
	while(1) {
		if(que.empty()) return -1;
		posi = que.front();
		moving = move.front();
		//cout << posi.first << ' ' << posi.second << ' ' << moving << endl;
		que.pop();
		move.pop();
		if(graph[posi.first][posi.second] == 'E') return moving;
		visited = moving+1;
		for(int i=0; i<4; i++) step[i] = posi;
		step[0].first++;
		step[1].first--;
		step[2].second++;
		step[3].second--;
		for(int i=0; i<4; i++) {
			if(step[i].first < 0 || step[i].first >= maxRow) continue;
			if(step[i].second < 0 || step[i].second >= maxRow) continue;
			if(s.find(step[i]) != s.end()) continue;
			if(graph[step[i].first][step[i].second] == '#') continue;
			que.push(step[i]);
			move.push(visited);
			s.insert(step[i]);
		}
	}
	return -1;
}

int main(void) {
	int tcase;
	cin >> tcase;
	while(tcase--) {
		cin >> maxRow >> maxCol;
		for(int i=0; i<maxRow; i++) {
			for(int j=0; j<maxCol; j++) {
				cin >> graph[i][j];
				if(graph[i][j] == 'S') { start.first = i; start.second = j; }
				if(graph[i][j] == 'E') { end.first = i; end.second = j; }
			}
		}
		cout << bfs() << endl;
	}
}

参考答案(时间最优[0]):

#include<iostream>
#include<queue>
#include<utility>
#include<set>
using namespace std;

int maxRow, maxCol;
pair <int, int> start;
pair <int, int> end;
int sr, sc, er, ec;
int r[] = {0, 0, 1, -1};
int c[] = {1, -1, 0, 0};
char graph[110][110];

int bfs() {
	set < pair <int, int> > s;
	queue < pair <int, int> > que;
	queue < int > move;
	pair < int, int > posi;
	pair < int, int > step[4];
	que.push(start);
	move.push(0);
	int visited = 0;
	int moving;
	while(1) {
		if(que.empty()) return -1;
		posi = que.front();
		moving = move.front();
		//cout << posi.first << ' ' << posi.second << ' ' << moving << endl;
		que.pop();
		move.pop();
		if(graph[posi.first][posi.second] == 'E') return moving;
		visited = moving+1;
		for(int i=0; i<4; i++) step[i] = posi;
		step[0].first++;
		step[1].first--;
		step[2].second++;
		step[3].second--;
		for(int i=0; i<4; i++) {
			if(step[i].first < 0 || step[i].first >= maxRow) continue;
			if(step[i].second < 0 || step[i].second >= maxRow) continue;
			if(s.find(step[i]) != s.end()) continue;
			if(graph[step[i].first][step[i].second] == '#') continue;
			que.push(step[i]);
			move.push(visited);
			s.insert(step[i]);
		}
	}
	return -1;
}

int main(void) {
	int tcase;
	cin >> tcase;
	while(tcase--) {
		cin >> maxRow >> maxCol;
		for(int i=0; i<maxRow; i++) {
			for(int j=0; j<maxCol; j++) {
				cin >> graph[i][j];
				if(graph[i][j] == 'S') { start.first = i; start.second = j; }
				if(graph[i][j] == 'E') { end.first = i; end.second = j; }
			}
		}
		cout << bfs() << endl;
	}
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

退出移动版