제출 #1147713

#제출 시각아이디문제언어결과실행 시간메모리
1147713jiahngMaze (JOI23_ho_t3)C++20
100 / 100
522 ms106108 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define ll int
typedef pair<int,int> pi;
typedef pair<ll,ll> pill;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define INF (ll)1e18
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
typedef pair <pi,pi> pipi;
#define maxn 500010

int N,M,K,sr,sc,gr,gc;

int dx[8] = {0,0,-1,1,-1,1,-1,1};
int dy[8] = {-1,1,0,0,1,-1,-1,1};
int32_t main(){
	fast;
	cin >> N >> M >> K;
	cin >> sr >> sc >> gr >> gc;
	
	bool swapped = 0;
	if (M < N){
		swap(M,N);
		swap(sr,sc); swap(gr,gc);
		swapped = 1;
	}
	
	bool A[N+1][M+1]; pi dist[N+1][M+1];
	char ch;
	if (swapped){
		FOR(i,1,M) FOR(j,1,N){
			cin >> ch; A[j][i] = (ch == '.');
		}
	}else{
		FOR(i,1,N) FOR(j,1,M){
			cin >> ch; A[i][j] = (ch == '.');
		}
	}
	//~ cout << N << ' ' << M << '\n';
	FOR(i,1,N) FOR(j,1,M) dist[i][j] = pi(-1,-1);
	deque <pipi> dq;
	dist[sr][sc] = pi(0,0);
	dq.push_front(pipi(pi(sr,sc), pi(0,0)));
	while (!dq.empty()){
		pipi cur = dq.front(); dq.pop_front();
		int x = cur.f.f, y = cur.f.s, d1 = cur.s.f, d2 = cur.s.s;
		if (dist[x][y] != pi(d1,d2)) continue;
		
		if (x == gr && y == gc){
			cout << d1 << '\n'; return 0;
		}
		if (d2 < 0){
			FOR(i,0,7){
				int nx = x + dx[i], ny = y + dy[i];
				if (nx > 0 && ny > 0 && nx <= N && ny <= M && (dist[nx][ny] == pi(-1,-1) || dist[nx][ny] > pi(d1,d2+1))){
					dist[nx][ny] = pi(d1,d2+1);
					dq.push_back(pipi(pi(nx,ny), pi(d1,d2+1)));
				}
			}
		}else{
			FOR(i,0,3){
				int nx = x + dx[i], ny = y + dy[i];
				if (nx > 0 && ny > 0 && nx <= N && ny <= M){
					if (!A[nx][ny] && (dist[nx][ny] == pi(-1,-1) || dist[nx][ny] > pi(d1+1,-K+1))){
						dist[nx][ny] = pi(d1+1,-K+1);
						dq.push_back(pipi(pi(nx,ny), pi(d1+1,-K+1)));
					}
					if (A[nx][ny] && nx > 0 && ny > 0 && nx <= N && ny <= M && A[nx][ny] && (dist[nx][ny] == pi(-1,-1) || dist[nx][ny] > pi(d1, 0))){
						dist[nx][ny] = pi(d1,0);
						dq.push_front(pipi(pi(nx,ny), pi(d1,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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...