mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-12-21 14:41:45 +00:00
Compare commits
3 Commits
294780abb5
...
75c2df61c9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75c2df61c9 | ||
|
|
cbde29b00b | ||
|
|
66ae25f60c |
132
src/10/1/P3956.cpp
Normal file
132
src/10/1/P3956.cpp
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <queue>
|
||||||
|
#include <cstring>
|
||||||
|
#include <climits>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
const int MAXM = 105;
|
||||||
|
const int INF = INT_MAX;
|
||||||
|
|
||||||
|
|
||||||
|
const int dx[4] = {-1, 0, 1, 0};
|
||||||
|
const int dy[4] = {0, 1, 0, -1};
|
||||||
|
|
||||||
|
struct Node {
|
||||||
|
int x, y;
|
||||||
|
int cost;
|
||||||
|
int lastColor;
|
||||||
|
bool used;
|
||||||
|
|
||||||
|
|
||||||
|
bool operator>(const Node& other) const {
|
||||||
|
return cost > other.cost;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int m, n;
|
||||||
|
int grid[MAXM][MAXM];
|
||||||
|
int dist[MAXM][MAXM][2][3];
|
||||||
|
|
||||||
|
void init() {
|
||||||
|
|
||||||
|
memset(grid, -1, sizeof(grid));
|
||||||
|
|
||||||
|
for (int i = 1; i <= m; i++) {
|
||||||
|
for (int j = 1; j <= m; j++) {
|
||||||
|
for (int k = 0; k < 2; k++) {
|
||||||
|
for (int l = 0; l < 3; l++) {
|
||||||
|
dist[i][j][k][l] = INF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int bfs() {
|
||||||
|
priority_queue<Node, vector<Node>, greater<Node>> pq;
|
||||||
|
|
||||||
|
|
||||||
|
Node start;
|
||||||
|
start.x = 1;
|
||||||
|
start.y = 1;
|
||||||
|
start.cost = 0;
|
||||||
|
start.lastColor = grid[1][1];
|
||||||
|
start.used = false;
|
||||||
|
|
||||||
|
dist[1][1][0][grid[1][1]] = 0;
|
||||||
|
pq.push(start);
|
||||||
|
|
||||||
|
while (!pq.empty()) {
|
||||||
|
Node current = pq.top();
|
||||||
|
pq.pop();
|
||||||
|
|
||||||
|
int x = current.x;
|
||||||
|
int y = current.y;
|
||||||
|
int cost = current.cost;
|
||||||
|
int lastColor = current.lastColor;
|
||||||
|
bool used = current.used;
|
||||||
|
|
||||||
|
|
||||||
|
if (cost > dist[x][y][used][lastColor]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (x == m && y == m) {
|
||||||
|
return cost;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
int nx = x + dx[i];
|
||||||
|
int ny = y + dy[i];
|
||||||
|
|
||||||
|
|
||||||
|
if (nx < 1 || nx > m || ny < 1 || ny > m) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int nxtColor = grid[nx][ny];
|
||||||
|
if (nxtColor != -1) {
|
||||||
|
int addCost = 0;
|
||||||
|
if (lastColor != nxtColor) {
|
||||||
|
addCost = 1;
|
||||||
|
}
|
||||||
|
int newCost = cost + addCost;
|
||||||
|
int newLastColor = nxtColor;
|
||||||
|
bool newused = false;
|
||||||
|
if (newCost < dist[nx][ny][newused][newLastColor]) {
|
||||||
|
dist[nx][ny][newused][newLastColor] = newCost;
|
||||||
|
Node nxt;
|
||||||
|
nxt.x = nx;
|
||||||
|
nxt.y = ny;
|
||||||
|
nxt.cost = newCost;
|
||||||
|
nxt.lastColor = newLastColor;
|
||||||
|
nxt.used = newused;
|
||||||
|
pq.push(nxt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (nxtColor == -1 && !used) {
|
||||||
|
int magicCost = 2;
|
||||||
|
for (int magicColor = 0; magicColor <= 1; magicColor++) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
cin >> m >> n;
|
||||||
|
init();
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int x, y, c;
|
||||||
|
cin >> x >> y >> c;
|
||||||
|
grid[x][y] = c;
|
||||||
|
}
|
||||||
|
int result = bfs();
|
||||||
|
cout << result << endl;
|
||||||
|
}
|
||||||
89
src/10/1/P4162.cpp
Normal file
89
src/10/1/P4162.cpp
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <deque>
|
||||||
|
#include <cmath>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <algorithm>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
const int MAXN = 35;
|
||||||
|
const int dx[4] = {0, 1, 0, -1};
|
||||||
|
const int dy[4] = {1, 0, -1, 0};
|
||||||
|
|
||||||
|
int n, m, T;
|
||||||
|
char grid[MAXN][MAXN];
|
||||||
|
|
||||||
|
bool isValid(int x, int y) {
|
||||||
|
return x >= 0 && x < n && y >= 0 && y < m;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<vector<int>> bfs01(int sx, int sy) {
|
||||||
|
vector<vector<int>> dist(n, vector<int>(m, 0x3f3f3f3f));
|
||||||
|
vector<vector<bool>> visited(n, vector<bool>(m, false));
|
||||||
|
deque<pair<int, int>> dq;
|
||||||
|
|
||||||
|
int startcost = (grid[sx][sy] == '1') ? 1 : 0;
|
||||||
|
dist[sx][sy] = startcost;
|
||||||
|
dq.push_back({sx, sy});
|
||||||
|
|
||||||
|
while (!dq.empty()) {
|
||||||
|
auto [x, y] = dq.front();
|
||||||
|
dq.pop_front();
|
||||||
|
|
||||||
|
if (visited[x][y]) continue;
|
||||||
|
visited[x][y] = true;
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
int nx = x + dx[i];
|
||||||
|
int ny = y + dy[i];
|
||||||
|
|
||||||
|
if (!isValid(nx, ny)) continue;
|
||||||
|
|
||||||
|
int cost = (grid[nx][ny] == '1') ? 1 : 0;
|
||||||
|
if (dist[x][y] + cost < dist[nx][ny]) {
|
||||||
|
dist[nx][ny] = dist[x][y] + cost;
|
||||||
|
if (cost == 0) {
|
||||||
|
dq.push_front({nx, ny});
|
||||||
|
} else {
|
||||||
|
dq.push_back({nx, ny});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
double euladis(int x1, int y1, int x2, int y2) {
|
||||||
|
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
cin >> n >> m >> T;
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
cin >> grid[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double maxdis = 0.0;
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
auto dist = bfs01(i, j);
|
||||||
|
for (int x = 0; x < n; x++) {
|
||||||
|
for (int y = 0; y < m; y++) {
|
||||||
|
if (dist[x][y] <= T) {
|
||||||
|
maxdis = max(maxdis, euladis(i, j, x, y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("%.6f\n", maxdis);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user