A PUBG <dijkstra>
边权不为1时最短路使用dijkstra。
#include <stdio.h>
#include <bits/stdc++.h>
#define fst first
#define sed second
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int N = 110;
int g[N][N];
bool vis[N][N];
int dis[N][N];
int n, sx, sy, ex, ey;
int dir[4][2] = { -1, 0, 1, 0, 0, -1, 0, 1 };
struct node
{
int x, y, k;
bool operator < (const node &o) const
{
return k > o.k;
}
};
int dijkstra()
{
memset(vis, 0, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
dis[sx][sy] = 0;
priority_queue<node> pq;
pq.push({ sx, sy, 0 });
while (!pq.empty())
{
int x = pq.top().x, y = pq.top().y, k = pq.top().k; pq.pop();
vis[x][y] = 1;
for (int i = 0; i < 4; ++i)
{
int xx = x + dir[i][0], yy = y + dir[i][1];
if (xx == ex && yy == ey)
return k;
if (xx >= 1 && xx <= n && yy >= 1 && yy <= n && !vis[xx][yy] && dis[xx][yy] > k + g[xx][yy])
{
dis[xx][yy] = k + g[xx][yy];
pq.push({ xx, yy, dis[xx][yy] });
}
}
}
return -1;
}
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
while (cin >> n)
{
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
{
scanf("%d", &g[i][j]);
if (g[i][j] == -1)
sx = i, sy = j;
else if (g[i][j] == -2)
ex = i, ey = j;
}
int res = dijkstra();
cout << res << endl;
}
return 0;
}
B precise math function
有精度损失,不要用变量存直接printf输出。
#include <stdio.h>
#include <bits/stdc++.h>
#define fst first
#define sed second
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
int main()
{
#ifdef LOCAL
//freopen("C:/input.txt", "r", stdin);
#endif
int T;
cin >> T;
while (T--)
{
int n, x;
cin >> n >> x;
char s[] = "%.0f\n";
s[2] = '0' + x;
printf(s, pow(n, acos(-1)));
}
return 0;
}
D 打篮球 <模拟>
#include <stdio.h>
#include <bits/stdc++.h>
#define fst first
#define sed second
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
int a[110];
int n;
void solve()
{
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]);
int b[2] = { 1, 2 };
for (int i = 0; i < n; ++i)
{
if (b[0] != a[i] && b[1] != a[i])
{
cout << "NO" << endl;
return;
}
for (int j = 1; j <= 3; ++j)
if (j != b[0] && j != b[1])
{
b[1] = j;
b[0] = a[i];
break;
}
}
cout << "YES" << endl;
}
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
while (cin >> n)
solve();
return 0;
}
E 233 <高精度>
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int T;
T = sc.nextInt();
for (int ti = 1; ti <= T; ++ti)
{
BigInteger a, b;
a = sc.nextBigInteger();
b = sc.nextBigInteger();
System.out.println(a.multiply(b));
}
}
}
F 扫雷 <模拟>
要了解扫雷整块扩散的规则,沿着一个方向扩散的时候,遇到第一个非0块则不再继续向前。
每次扫描8个方向计算当前块数字。
#include <stdio.h>
#include <bits/stdc++.h>
#define fst first
#define sed second
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int N = 510;
int dir[8][2] = { -1, 0, 1, 0, 0, -1, 0, 1, -1, -1, -1, 1, 1, -1, 1, 1 };
int n, m, k;
char g[N][N], p[N][N];
int a[N], b[N];
void DFS(int x, int y, int z)
{
if (p[x][y] != '.' || g[x][y] == '*')
return;
int cnt = 0;
for (int i = 0; i < 8; ++i)
{
int xx = x + dir[i][0], yy = y + dir[i][1];
if (xx >= 1 && xx <= n && yy >= 1 && yy <= m)
{
if (g[xx][yy] == '*')
++cnt;
else if (p[xx][yy] == '0')
++z;
}
}
if (cnt && !z)
return;
p[x][y] = '0' + cnt;
for (int i = 0; i < 4; ++i)
{
int xx = x + dir[i][0], yy = y + dir[i][1];
if (xx >= 1 && xx <= n && yy >= 1 && yy <= m)
DFS(xx, yy, 0);
}
}
void solve()
{
for (int i = 1; i <= k; ++i)
{
int x = a[i], y = b[i];
if (g[x][y] == '*')
{
printf("Game over in step %d\n", i);
return;
}
DFS(x, y, 1);
/*
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
putchar(p[i][j]);
putchar('\n');
}
cout << endl;
*/
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
putchar(p[i][j]);
putchar('\n');
}
}
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
int T;
cin >> T;
while (T--)
{
memset(p, '.', sizeof(p));
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i)
scanf("%s", g[i] + 1);
for (int i = 1; i <= k; ++i)
scanf("%d%d", &a[i], &b[i]);
solve();
}
return 0;
}
G 火车上的2连座
#include <stdio.h>
#include <bits/stdc++.h>
#define fst first
#define sed second
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
char s[1010][100];
int n;
void solve()
{
for (int i = 1; i <= n; ++i)
scanf("%s", s[i]);
for (int i = 1; i <= n; ++i)
{
if (s[i][0] == 'O' && s[i][1] == 'O')
{
s[i][0] = s[i][1] = '+';
cout << "YES" << endl;
for (int j = 1; j <= n; ++j)
printf("%s\n", s[j]);
return;
}
if (s[i][3] == 'O' && s[i][4] == 'O')
{
s[i][3] = s[i][4] = '+';
cout << "YES" << endl;
for (int j = 1; j <= n; ++j)
printf("%s\n", s[j]);
return;
}
}
cout << "NO" << endl;
}
int main()
{
#ifdef LOCAL
//freopen("C:/input.txt", "r", stdin);
#endif
while (cin >> n)
solve();
return 0;
}
H 程序员的好印象
#include <stdio.h>
#include <bits/stdc++.h>
#define fst first
#define sed second
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int N = 110;
int a[N], f[2][N];
int main()
{
#ifdef LOCAL
//freopen("C:/input.txt", "r", stdin);
#endif
int n;
while (cin >> n)
{
memset(f, 0, sizeof(f));
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
int ans = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j < i; ++j)
{
f[0][i] = max(f[0][i], f[0][j]);
f[1][i] = max({ f[0][i], f[0][j], f[1][j] });
}
ans = max(ans, ++f[a[i]][i]);
}
cout << ans << endl;
}
return 0;
}

本文精选了多个算法竞赛题目并提供了详细的解决方案,包括最短路径、高精度计算、模拟问题等,涵盖了Dijkstra算法、高精度乘法、扫雷游戏模拟、火车座位优化等问题,深入解析了算法思想与实现细节。
491

被折叠的 条评论
为什么被折叠?



