1.羊、狼、农夫过河
题目描述
羊、狼、农夫都在岸边,当羊的数量小于狼的数量时,狼会攻击羊,农夫则会损失羊。农夫有一艘容量固定的船,能够承载固定数量的动物。
要求求出不损失羊情况下将全部羊和狼运到对岸需要的最小次数。
只计算农夫去对岸的次数,回程时农夫不会运送羊和狼。
备注:农夫在或农夫离开后羊的数量大于狼的数量时狼不会攻击羊。
输入描述 第一行输入为M,N,X, 分别代表羊的数量,狼的数量,小船的容量。
输出描述 输出不损失羊情况下将全部羊和狼运到对岸需要的最小次数(若无法满足条件则输出0)。
用例1
输入:5 3 3 输出:3
说明: 第一次运2只狼 第二次运3只羊 第三次运2只羊和1只狼
用例2
输入:5 4 1 输出:0
说明: 如果找不到不损失羊的运送方案,输出0
#include<bits/stdc++.h>
using namespace std;
int res=INT_MAX;
vector<vector<int>> dp;
int dfs(int sheep,int wolf,int boat,int oppsheep,int oppwolf,int stepCount){
if(sheep==0&&wolf==0){
res=min(res,stepCount);
return stepCount;
}
if(sheep+wolf<=boat){
res=min(res,stepCount+1);
return stepCount+1;
}
if(stepCount>=res){
return INT_MAX;
}
if(dp[sheep][wolf]!=INT_MAX){
return dp[sheep][wolf];
}
int curRes=INT_MAX;
for (int i = min(boat, sheep); i >= 0; i--) {
for (int j = min(boat, wolf); j >= 0; j--) {
if(i+j>boat) continue;
if(sheep-i<=wolf-j&&sheep-i!=0){
continue;
}
if(oppsheep+i<=oppwolf+j&&oppsheep+i!=0){
break;
}
int tmp=dfs(sheep-i,wolf-j,boat,oppsheep+i,oppwolf+j,stepCount+1);
curRes=min(tmp,curRes);
}
if(curRes!=INT_MAX){
break;
}
}
dp[sheep][wolf]=curRes;
return curRes;
}
int main() {
int m,n,x;
cin>>m; cin>>n; cin>>x;
dp.resize(6000,vector<int>(6000,INT_MAX));
dfs(m,n,x,0,0,0);
if(res!=INT_MAX){
cout<<res;
}else{
cout<<0;
}
return 0;
}
思路:想好每一种边界条件,同时去维护一个最小运输次数
2.寻找最优的路测线路
题目描述
评估一个网络的信号质量,其中一个做法是将网络划分为栅格,然后对每个栅格的信号质量计算。
路测的时候,希望选择一条信号最好的路线(彼此相连的栅格集合)进行演示。
现给出 R 行 C 列的整数数组 Cov,每个单元格的数值 S 即为该栅格的信号质量(已归一化,无单位,值越大信号越好)。
要求从 [0, 0] 到 [R-1, C-1]设计一条最优路测路线。返回该路线得分。
规则:
路测路线可以上下左右四个方向,不能对角 路线的评分是以路线上信号最差的栅格为准的,例如路径 8→4→5→9 的值为4,该线路评分为4。线路最优表示该条线路的评分最高。 输入描述 第一行表示栅格的行数 R
第二行表示栅格的列数 C
第三行开始,每一行表示栅格地图一行的信号值,如5 4 5
输出描述 最优路线的得分
备注 1 ≤ R,C ≤ 20 0 ≤ S ≤ 65535
用例1
输入:
3
3
5 4 5
1 2 6
7 4 6
输出:
4
说明:路线为:5→4→5→6→6
用例2
输入:
6
5
3 4 6 3 4
0 2 1 1 7
8 8 3 2 7
3 2 4 9 8
4 1 2 0 0
4 6 5 4 3
输出:
3
说明:
路线为:3→4→6→3→4→7→7→8→9→4→3→8→8→3→4→4→6→5→4→3
#include <bits/stdc++.h>
using namespace std;
int R, C;
vector<vector<int>> Cov;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
bool canPass(int X) {
if (Cov[0][0] < X) return false;
queue<pair<int, int>> q;
vector<vector<bool>> visited(R, vector<bool>(C, false));
q.push({0, 0});
visited[0][0] = true;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
if (x == R - 1 && y == C - 1) return true;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx >= 0 && nx < R && ny >= 0 && ny < C && !visited[nx][ny] && Cov[nx][ny] >= X) {
visited[nx][ny] = true;
q.push({nx, ny});
}
}
}
return false;
}
int findMaxMinPath() {
int left = 0, right = 65535, ans = 0;
while (left <= right) {
int mid = (left + right) / 2;
if (canPass(mid)) {
ans = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return ans;
}
int main() {
cin >> R >> C;
Cov.resize(R, vector<int>(C));
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
cin >> Cov[i][j];
cout << findMaxMinPath() << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
struct Node {
int x, y, score;
bool operator<(const Node &other) const {
return score < other.score;
}
};
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int main() {
int R, C;
cin >> R >> C;
vector<vector<int>> Cov(R, vector<int>(C));
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
cin >> Cov[i][j];
priority_queue<Node> pq;
vector<vector<int>> best(R, vector<int>(C, 0));
pq.push({0, 0, Cov[0][0]});
best[0][0] = Cov[0][0];
while (!pq.empty()) {
Node cur = pq.top();
pq.pop();
if (cur.x == R - 1 && cur.y == C - 1) {
cout << cur.score << endl;
return 0;
}
for (int d = 0; d < 4; d++) {
int nx = cur.x + dx[d];
int ny = cur.y + dy[d];
if (nx >= 0 && nx < R && ny >= 0 && ny < C) {
int new_score = min(cur.score, Cov[nx][ny]);
if (new_score > best[nx][ny]) {
best[nx][ny] = new_score;
pq.push({nx, ny, new_score});
}
}
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
class UnionFind {
public:
vector<int> parent, rank;
UnionFind(int n) {
parent.resize(n);
rank.resize(n, 1);
for (int i = 0; i < n; i++) parent[i] = i;
}
int find(int x) {
if (parent[x] != x)
parent[x] = find(parent[x]);
return parent[x];
}
void unite(int x, int y) {
int rootX = find(x);
int rootY = find(y);
if (rootX != rootY) {
if (rank[rootX] > rank[rootY])
parent[rootY] = rootX;
else if (rank[rootX] < rank[rootY])
parent[rootX] = rootY;
else {
parent[rootY] = rootX;
rank[rootX]++;
}
}
}
bool connected(int x, int y) {
return find(x) == find(y);
}
};
int R, C;
vector<vector<int>> Cov;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool canConnect(int minSignal) {
UnionFind uf(R * C);
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (Cov[i][j] < minSignal) continue;
int index = i * C + j;
for (int d = 0; d < 4; d++) {
int ni = i + dx[d], nj = j + dy[d];
if (ni >= 0 && ni < R && nj >= 0 && nj < C && Cov[ni][nj] >= minSignal) {
int neighborIndex = ni * C + nj;
uf.unite(index, neighborIndex);
}
}
}
}
return uf.connected(0, R * C - 1);
}
int main() {
cin >> R >> C;
Cov.resize(R, vector<int>(C));
for (int i = 0; i < R; i++)
for (int j = 0; j < C; j++)
cin >> Cov[i][j];
int low = 0, high = 65535;
while (low +1< high) {
int mid = low+(high-low)/2;
if (canConnect(mid)) {
low = mid;
} else {
high = mid;
}
}
cout << low << endl;
return 0;
}
思路:根据题目中的输出所有线路的最高评分,同时这个线路的评分是这条线路中的最小值决定的(这种最大最小值的情况一般是要进行二分)。
那如何分析单调性呢?
问题转换:我们需要找到一条路径,使得路径上所有栅格的信号质量都不小于某个值 mid
如果存在这样的路径,那么我们可以尝试更大的 mid;否则,我们需要尝试更小的 mid
单调性:如果存在一个 mid 使得路径上所有栅格的信号质量都不小于 mid,那么对于所有小于 mid 的值,也一定存在这样的路径。
反之,如果不存在这样的路径,那么对于所有大于 mid 的值,也一定不存在这样的路径。
注:关于二分的写法可以看我之前发的博客
如果对于解法有不太懂的地方, 可以在评论区留言