P3842 [TJOI2007] 线段 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
思路:(实在没想出来,看的题解)
分为左端点和右段点两种,每一种端点又有多种不同的情况
题解 P3842 【[TJOI2007]线段】 - 洛谷专栏 (luogu.com.cn)
AC:
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
using namespace std;
typedef long long ll;
priority_queue<int, vector<int>, greater<int>> pq;
map<int, int>mp;
int f[2][20010];
int len[20010];
int r[20020], l[20020];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> l[i] >> r[i];
len[i] = r[i] - l[i];
}
f[0][1] = r[1] + 1 + len[1];
f[1][1] = r[1] - 1;
for (int i = 2; i <= n; i++)
{
f[0][i] = min(f[0][i - 1] + abs(r[i] - l[i - 1]), f[1][i - 1] + abs(r[i - 1] - r[i])) + len[i] + 1;
f[1][i] = min(f[0][i - 1] + abs(l[i - 1] - l[i]), f[1][i - 1] + abs(r[i - 1] - l[i])) + len[i] + 1;
}
cout << min(f[0][n] + n - l[n], f[1][n] + n - r[n]);
return 0;
}
P1825 [USACO11OPEN] Corn Maze S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
思路:
bfs题(基本上是模板但是有坑点),对每一次出队的点进行判断,若是传送门我们则需找到另一个传送门
AC:
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
using namespace std;
typedef long long ll;
priority_queue<int, vector<int>, greater<int>> pq;
map<int, int>mp;
int n, m;
char ch[305][305];
int vis[305][305];
struct node
{
int x;
int y;
int step;
};
queue<node>q;
int sx, sy;
int ans = 0;
void find(int &nx, int &ny, int &nt)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (ch[i][j] == ch[nx][ny] && (i != nx || j != ny))
{
nx = i;
ny = j;
return;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int next[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> ch[i][j];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (ch[i][j] == '@')
{
sx = i;
sy = j;
}
}
}
q.push({ sx,sy,0 });
vis[sx][sy] = 1;
while (!q.empty())
{
node qq = q.front();
q.pop();
if (ch[qq.x][qq.y] == '=')
{
cout << qq.step << endl;
return 0;
}
if (ch[qq.x][qq.y] >= 'A' && ch[qq.x][qq.y] <= 'Z')
{
find(qq.x, qq.y, qq.step);
}
for (int i = 0; i < 4; i++)
{
int tx = qq.x + next[i][0];
int ty = qq.y + next[i][1];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && vis[tx][ty] == 0 && ch[tx][ty] != '#')
{
vis[tx][ty] = 1;
q.push({ tx,ty,qq.step + 1 });
}
}
}
cout << ans << endl;
return 0;
}
P1433 吃奶酪 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
思路:
正常搜索只有50分,必须要动态压缩才能过(看了题解和视频才搞定)
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<math.h>
using namespace std;
typedef long long ll;
priority_queue<int, vector<int>, greater<int>> pq;
map<int, int>mp;
int n;
double a[20][2];
int b[20];
double mins = 1e9;
double dp[20][1 << 16];
double dfs(int pos, int deep, double len,int path)
{
double ans = 1e9;
if (deep == n)
return len;
if (dp[pos][path])
return len + dp[pos][path];
for (int i = 1; i <= n; i++)
{
if (b[i])continue;
b[i] = 1;
double r1 = a[i][0] - a[pos][0];
double r2 = a[i][1] - a[pos][1];
double r = sqrt(r1 * r1 + r2 * r2);
ans=min(ans,dfs(i, deep + 1, len + r,pos|(1<<i)));
b[i] = 0;
}
dp[pos][path] = ans - len;
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i][0] >> a[i][1];
}
mins=dfs(0, 0, 0,0);
printf("%.2lf", mins);
return 0;
}