dungeon escape TA
Kotori
c_cpp
a year ago
3.3 kB
6
Indexable
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<queue> using namespace std; #define inf 99999 int N, stamina, dungeon[500][500]; int dx[4] = { -1, 0, 1, 0 }; int dy[4] = { 0, 1, 0, -1 }; int cnt_gate; int dist[205][205]; int mecung[500][500]; class vitri { public: int x, y; }; class Gate { public: int row, col, id; Gate() { row = col = id = -1; } void resetgate() { row = col = id = -1; } }; class State { public: int id, len; bool operator < (const State& other) const { return len > other.len; } }; Gate gate[500]; int store_gate[205]; void bfs(int start) { for (int j = 0; j <= 205; j++) { dist[start][j] = inf; } dist[start][start] = 0; queue<vitri> q; //mecung[start] q.push({ gate[start].row, gate[start].col }); mecung[gate[start].row][gate[start].col] = 0; while (!q.empty()) { vitri cur_v = q.front(); q.pop(); if (dungeon[cur_v.x][cur_v.y] > 0 && dungeon[cur_v.x][cur_v.y] != start) { dist[start][dungeon[cur_v.x][cur_v.y]] = mecung[cur_v.x][cur_v.y]; } for (int i = 0; i < 4; i++) { int r = cur_v.x + dx[i]; int c = cur_v.y + dy[i]; if (r >= 0 && r < N && c >= 0 && c < N && dungeon[r][c] != -1 && mecung[cur_v.x][cur_v.y] + 1 <= stamina /*&& mecung[cur_v.x][cur_v.y] + 1 <= mecung[r][c]*/) { mecung[r][c] = mecung[cur_v.x][cur_v.y] + 1; q.push({ r,c }); } } } } void addGate(int mGate, int mRow, int mCol) { gate[mGate].row = mRow; gate[mGate].col = mCol; gate[mGate].id = mGate; dungeon[mRow][mCol] = mGate; store_gate[cnt_gate] = mGate; cnt_gate++; return; } void removeGate(int mGate) { dungeon[gate[mGate].row][gate[mGate].col] = 0; gate[mGate].resetgate(); return; } int getMinTime(int mStart, int mEnd) { for (int i = 0; i < cnt_gate; i++) { bfs(store_gate[i]); } int d[205]; for (int i = 0; i < 205; i++) { d[i] = inf; } d[mStart] = 0; priority_queue<State> pq; pq.push({ mStart, 0 }); while (!pq.empty()) { State cur_s = pq.top(); pq.pop(); for (int i = 0; i < 205; i++) { if (dist[cur_s.id][i] != inf && dist[cur_s.id][i] > 0) { if (d[i] > d[cur_s.id] + dist[cur_s.id][i]) { d[i] = d[cur_s.id] + dist[cur_s.id][i]; pq.push({ i, d[i] }); } } } } return d[mEnd] == inf ? -1 : d[mEnd]; } int main() { N = 12; stamina = 6; cnt_gate = 0; freopen("input.txt", "r", stdin); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> dungeon[i][j]; dungeon[i][j] *= -1; cout << dungeon[i][j] << "\t"; mecung[i][j] = inf; } cout << endl; } addGate(1, 2, 1); addGate(2, 1, 5); addGate(3, 7, 1); addGate(4, 1, 8); addGate(5, 4, 4); addGate(6, 9, 5); addGate(7, 10, 10); cout << "#############\n"; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << dungeon[i][j] << "\t"; } cout << "\n"; } int res = getMinTime(2, 3); res = getMinTime(4, 3); removeGate(2); res = getMinTime(4, 3); res = getMinTime(5, 6); addGate(8, 7, 7); res = getMinTime(5, 6); return 0; }
Editor is loading...
Leave a Comment