Submission #141404

# Submission time Handle Problem Language Result Execution time Memory
141404 2019-08-07T22:32:47 Z Ort Portal (COCI17_portal) C++11
96 / 120
99 ms 16120 KB
#include<bits/stdc++.h>
#define MEM(a, b) memset(a, (b), sizeof(a))
#define ALL(c) (c).begin(),(c).end()
#define LINF (int)1e18
#define INF (int)1e9
#define ll long long
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define MAX 510

using namespace std;

typedef pair<int,int> pii;

int n, m, si, sj, ei, ej;
string mat[MAX];
vector<pii> adm[MAX][MAX];
int v[MAX][MAX];
int dy[] = {1, -1, 0, 0};
int dx[] = {0, 0, 1, -1};

struct point{
	int y, x, d;
	point(int y, int x, int d) {
		this->y = y;
		this->x = x;
		this->d = d;
	}
};

queue<point> q;

bool check(int y, int x) {return (y>=0 && y<n && x>=0 && x<m && mat[y][x]!='#');}

inline void preprocess() {
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++) {
			if(mat[i][j]=='#') continue;
			if(mat[i][j-1]=='#') {
				for(int k=j;k<m;k++) {
					if(mat[i][k]=='#') break;
					if(i+1<n && mat[i+1][k]=='#') adm[i][k].pb(mp(i,j));
					if(i-1>=0 && mat[i-1][k]=='#') adm[i][k].pb(mp(i,j));
					if(k+1<m && mat[i][k+1]=='#') adm[i][k].pb(mp(i,j)), adm[i][j].pb(mp(i,k));
				}
			}
			if(mat[i][j+1]=='#') {
				for(int k=j;k>=0;k--) {
					if(mat[i][k]=='#') break;
					if(i+1<n && mat[i+1][k]=='#') adm[i][k].pb(mp(i,j));
					if(i-1>=0 && mat[i-1][k]=='#') adm[i][k].pb(mp(i,j));
				}
			}
			if(mat[i-1][j]=='#') {
				for(int k=i;k<n;k++) {
					if(mat[k][j]=='#') break;
					if(j+1<m && mat[k][j+1]=='#') adm[k][j].pb(mp(i,j));
					if(j-1>=0 && mat[k][j-1]=='#') adm[k][j].pb(mp(i,j));
					if(k+1<n && mat[k+1][j]=='#') adm[k][j].pb(mp(i,j)), adm[i][j].pb(mp(k,j));
				}
			}
			if(mat[i+1][j]=='#') {
				for(int k=i;k>=0;k--) {
					if(mat[k][j]=='#') break;
					if(j+1<m && mat[k][j+1]=='#') adm[k][j].pb(mp(i,j));
					if(j-1>=0 && mat[k][j-1]=='#') adm[k][j].pb(mp(i,j));
				}
			}
		}
}

int main() {
//	IO;
	cin >> n >> m;
	for(int i=0;i<n;i++) cin >> mat[i];
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++) {
			if(mat[i][j]=='C') {
				si = i; sj = j;
			}
			if(mat[i][j]=='F') {
				ei = i; ej = j;
			}
		}
	MEM(v, -1);
	preprocess();
	q.push(point(si,sj,0));
	while(!q.empty()) {
		point c = q.front();
		q.pop();
		if(c.y==ei && c.x==ej) {
			cout << c.d;
			return 0;
		}
		if(v[c.y][c.x]!=-1) continue;
		v[c.y][c.x] = c.d;
		for(auto u:adm[c.y][c.x]) q.push(point(u.first,u.second,c.d+1));
		for(int k=0;k<4;k++) {
			int ny = c.y+dy[k];
			int nx = c.x+dx[k];
			if(check(ny,nx)) q.push(point(ny,nx,c.d+1));
		}
	}
	cout << "nemoguce";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7416 KB Output is correct
2 Correct 9 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7544 KB Output is correct
2 Correct 10 ms 7416 KB Output is correct
3 Correct 8 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8056 KB Output is correct
2 Correct 14 ms 8312 KB Output is correct
3 Correct 16 ms 8184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 9720 KB Output is correct
2 Correct 18 ms 9336 KB Output is correct
3 Correct 20 ms 8568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 12024 KB Output is correct
2 Correct 44 ms 12160 KB Output is correct
3 Correct 36 ms 10104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 15128 KB Output is correct
2 Correct 67 ms 16120 KB Output is correct
3 Correct 56 ms 11732 KB Output is correct
4 Incorrect 47 ms 10872 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 99 ms 15224 KB Output isn't correct
2 Halted 0 ms 0 KB -