Submission #1094304

#TimeUsernameProblemLanguageResultExecution timeMemory
1094304stomic07Portals (BOI14_portals)C++17
20 / 100
1076 ms4956 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 1005;
int bio[N][N];
int fmov[4] = {-1, 0, 1, 0};
int smov[4] = {0, 1, 0, -1};

void portalfill(const vector<string>& v, const pair<int, int>& curr, queue<pair<int, int>>& que, bool prvi){
	for (int i=0; i<4; ++i){
		for (int j=1; ; ++j){
			pair<int, int> novi = make_pair(curr.first + fmov[i] * j, curr.second + smov[i] * j);
			if (v[novi.first][novi.second] == '#'){
				if (bio[novi.first - fmov[i]][novi.second - smov[i]] == -1){
					que.push(make_pair(novi.first - fmov[i], novi.second - smov[i]));
				}
				break;
			}
		}
	}
	bool zid = 0;
	for (int i=0; i<4; ++i){
		int f = curr.first + fmov[i];
		int s = curr.second + smov[i];
		if (v[f][s] == '#') zid = 1;
	}
	if (!prvi && zid) return;
	for (int i=0; i<4; ++i){
		pair<int, int> sus = make_pair(curr.first + fmov[i], curr.second + smov[i]);
		int br = bio[sus.first][sus.second];
		if (br != -1 && br == bio[curr.first][curr.second] - 1){
			portalfill(v, sus, que, false);
		}
	}
}

void bfs(const vector<string>& v, pair<int, int> start){
	queue<pair<int, int>> que;
	que.push(start);
	for (int steps = 0; !que.empty(); ++steps){
		queue<pair<int, int>> novi;
		while (!que.empty()){
			pair<int, int> curr = que.front();
			que.pop();
			if (bio[curr.first][curr.second] != -1) continue;
			bio[curr.first][curr.second] = steps;
			if (v[curr.first][curr.second] == 'C') return;
			bool zid = 0;
			for (int i=0; i<4; ++i){
				pair<int, int> sus = make_pair(curr.first + fmov[i], curr.second + smov[i]);
				if (v[sus.first][sus.second] != '#'){
					novi.push(sus);
				}
				else{
					zid = 1;
				}
			}
			if (zid){
				portalfill(v, curr, novi, true);
			}
		}
		que = novi;
	}
}

void reset(){
	for (int i=0; i<N; ++i){
		for (int j=0; j<N; ++j){
			bio[i][j] = -1;
		}
	}
}

int main(){
	reset();
	int n, m;
	cin >> n >> m;
	string nule = "";
	for (int i=0; i<m+2; ++i){
		nule += '#';
	}
	vector<string> v;
	v.push_back(nule);
	pair<int, int> start;
	pair<int, int> cilj;
	for (int i=0; i<n; ++i){
		string red = "";
		red += '#';
		for (int j=0; j<m; ++j){
			char c;
			cin >> c;
			red += c;
			if (c == 'S'){
				start.first = i+1;
				start.second = j+1;
			}
			if (c == 'C'){
				cilj.first = i+1;
				cilj.second = j+1;
			}
		}
		red += '#';
		v.push_back(red);
	}
	v.push_back(nule);
	bfs(v, start);
	cout << bio[cilj.first][cilj.second] << "\n";
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...