답안 #1077106

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1077106 2024-08-27T00:41:43 Z pawned Toy (CEOI24_toy) C++17
0 / 100
1500 ms 85368 KB
#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;

const char nl = '\n';

void fastIO() {
	ios::sync_with_stdio(false);
	cin.tie(0);
}


int dir1[8] = {-1, 1, 0, 0, 0, 0, 0, 0};
int dir2[8] = {0, 0, -1, 1, 0, 0, 0, 0};
int dir3[8] = {0, 0, 0, 0, -1, 1, 0, 0};
int dir4[8] = {0, 0, 0, 0, 0, 0, -1, 1};

int main() {
	fastIO();
	int R, C, K, L;
	// K = horizontal width, L = vertical width
	cin>>C>>R>>K>>L;
	pair<ii, ii> start;
	// {horizontal left coords, vertical top coords}
	cin>>start.fi.se>>start.fi.fi>>start.se.se>>start.se.fi;
	ii goal;	// goal cell
	vector<vector<char>> grid(R, vector<char>(C, '.'));
	for (int i = 0; i < R; i++) {
		for (int j = 0; j < C; j++) {
			cin>>grid[i][j];
			if (grid[i][j] == '*')
				goal = {i, j};
		}
	}
	// prefix sums to check for filled
	vector<vi> pfs1(R, vi(C + 1, 0));	// horizontal prefix sums
	vector<vi> pfs2(R + 1, vi(C, 0));	// vertical prefix sums
	for (int i = 0; i < R; i++) {
		for (int j = 1; j <= C; j++) {
			pfs1[i][j] = pfs1[i][j - 1];
			if (grid[i][j - 1] == 'X')
				pfs1[i][j]++;
		}
	}
	for (int i = 0; i < C; i++) {
		for (int j = 1; j <= R; j++) {
			pfs2[j][i] = pfs2[j - 1][i];
			if (grid[j - 1][i] == 'X')
				pfs2[j][i]++;
		}
	}
/*	cout<<"pfs1: "<<endl;
	for (vi v : pfs1) {
		for (int x : v)
			cout<<x<<" ";
		cout<<endl;
	}
	cout<<"pfs2: "<<endl;
	for (vi v : pfs2) {
		for (int x : v)
			cout<<x<<" ";
		cout<<endl;
	}*/
	set<pair<ii, ii>> visited;
	queue<pair<ii, ii>> q;
	visited.insert(start);
	q.push(start);
	while (!q.empty()) {
		pair<ii, ii> x = q.front();
		q.pop();
//		cout<<"at ("<<x.fi.fi<<", "<<x.fi.se<<", "<<x.se.fi<<", "<<x.se.se<<")"<<endl;
		for (int i = 0; i < 8; i++) {
			pair<ii, ii> y = x;	// new state
			y.fi.fi += dir1[i];
			y.fi.se += dir2[i];
			y.se.fi += dir3[i];
			y.se.se += dir4[i];
//			cout<<"going to ("<<y.fi.fi<<", "<<y.fi.se<<", "<<y.se.fi<<", "<<y.se.se<<")"<<endl;
			// check if state is possible
			if (y.fi.fi < 0 || y.fi.fi >= R || y.fi.se < 0 || y.fi.se >= C - K + 1)
				continue;
			if (y.se.se < 0 || y.se.se >= C || y.se.fi < 0 || y.se.fi >= R - L + 1)
				continue;
			// state is in grid, check if none on it blocked
			if (pfs1[y.fi.fi][y.fi.se + K] - pfs1[y.fi.fi][y.fi.se] > 0)
				continue;
			if (pfs2[y.se.fi + L][y.se.se] - pfs2[y.se.fi][y.se.se] > 0)
				continue;
			// if state is possible and unvisited, add to queue
			if (visited.find(y) == visited.end()) {
				q.push(y);
				visited.insert(y);
			}
		}
	}
	// check if any of visited intersect at flag
/*	cout<<"visited: ";
	for (pair<ii, ii> p : visited)
		cout<<"("<<p.fi.fi<<", "<<p.fi.se<<", "<<p.se.fi<<", "<<p.se.se<<"); ";
	cout<<endl;*/
	bool found = false;
	for (pair<ii, ii> p : visited) {
		// find intersection
		// intersection has x of horizontal and y of vertical
		ii ist = {p.fi.fi, p.se.se};
//		cout<<"ist: "<<ist.fi<<", "<<ist.se<<endl;
		if (ist == goal)
			found = true;
	}
	if (found)
		cout<<"YES"<<endl;
	else
		cout<<"NO"<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 7 ms 860 KB Output is correct
4 Execution timed out 1589 ms 85196 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 7 ms 860 KB Output is correct
4 Execution timed out 1589 ms 85196 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Execution timed out 1555 ms 85368 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 7 ms 860 KB Output is correct
4 Execution timed out 1589 ms 85196 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 7 ms 860 KB Output is correct
4 Execution timed out 1589 ms 85196 KB Time limit exceeded
5 Halted 0 ms 0 KB -