7-20课程解析+总结(3.0版本)
题目解析+总结
P1135
在一个长度为的数轴上,第个点对应的数字为。从第个点上,可以实现以下操作:
- 左移:在不越界的情况下,可以移动到个格子上
- 右移:在不越界的情况下,可以移动到个格子上
那么,从第个点到第个点至少要多少次以上操作呢?
分析
这道题有两种方法来写这道题。 方法一:纯 BFS 搜索。 每次BFS遍历,都有两种选择:往左移动和往右移动。 在没有越界和没有走过的情况下,可以执行这两种操作。
if(top.x + k[top.x] <= n && !vis[top.x + k[top.x]]){
vis[top.x + k[top.x]] = 1;
op.push({top.x + k[top.x],top.step+1});
}
if(top.x - k[top.x] >= 1 && !vis[top.x - k[top.x]]){
vis[top.x - k[top.x]] = 1;
op.push({top.x - k[top.x],top.step+1});
}建议函数为int类型,在找到终点时直接返回操作次数,找不到则返回。
if(top.x == b) return top.step;代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a,b,k[205],vis[205];
struct node{
int x,step;
};queue<node>op;
int BFS(int cur){
vis[cur] = 1;
op.push({cur,0});
while(op.size()){
node top = op.front();
op.pop();
if(top.x == b) return top.step;
if(top.x + k[top.x] <= n && !vis[top.x + k[top.x]]){
vis[top.x + k[top.x]] = 1;
op.push({top.x + k[top.x],top.step+1});
}
if(top.x - k[top.x] >= 1 && !vis[top.x - k[top.x]]){
vis[top.x - k[top.x]] = 1;
op.push({top.x - k[top.x],top.step+1});
}
}
return -1;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>a>>b;
for(int i=1;i<=n;++i) cin>>k[i];
cout<<BFS(a)<<endl;
return 0;
}方法二:图论 + BFS 搜索。 输入时,若这个点在执行操作时满足条件,则将它存入图中。
for(int i=1;i<=n;++i){
int x;
cin>>x;
if(i + x <= n) pic[i].push_back(i+x);
if(i - x >= 1) pic[i].push_back(i-x);
}这样,在遍历的时候,直接遍历它能走到的点,就可以了。
for(int i=0;i<pic[top.x].size();++i){
if(!vis[pic[top.x][i]]){
vis[pic[top.x][i]] = 1;
op.push({pic[top.x][i],top.step+1});
}
}代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a,b,vis[205];
vector<int>pic[205];
struct node{
int x,step;
};queue<node>op;
int BFS(int cur){
vis[cur] = 1;
op.push({cur,0});
while(op.size()){
node top = op.front();
op.pop();
if(top.x == b) return top.step;
for(int i=0;i<pic[top.x].size();++i){
if(!vis[pic[top.x][i]]){
vis[pic[top.x][i]] = 1;
op.push({pic[top.x][i],top.step+1});
}
}
}
return -1;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>a>>b;
for(int i=1;i<=n;++i){
int x;
cin>>x;
if(i + x <= n) pic[i].push_back(i+x);
if(i - x >= 1) pic[i].push_back(i-x);
}
cout<<BFS(a)<<endl;
return 0;
}P5663
工厂里有位工人,编号为~,某些工人之间存在一个双向传送带。 若号工人要生产一个被加工到第阶段的零件,则所有与号工人有传送带直接相连的工人,都需要生产一个被加工到第阶段的零件或者提供原材料(但号工人自己无需生产第阶段的零件)。 给出张工单,表示号工人要生产一个被加工到第阶段的零件。号工人是否需要提供原材料?
分析
3 2 3
1 2
2 3
1 1
1 2
1 3①号员工要生产第阶段的零件,需要号员工提供原材料,输出”No”;
②号员工要生产第阶段的零件,需要号员工生产第阶段的零件,
号员工要生产第阶段的零件,需要号员工、号员工提供原材料,输出”Yes”;
③号员工要生产第阶段的零件,需要号员工生产第阶段的零件,
号员工要生产第阶段的零件,需要号员工、号员工生产第阶段的零件,
号员工要生产第阶段的零件,需要号员工提供原材料,输出”No”;
像这种情况,从出发,所有的偶数阶段都可以,否则都不可以
样例:
......
2 1
2 2
2 3
2 4
2 5
2 6输出:
Yes
No
Yes
Yes
Yes
Yes像这种情况,从出发,到的步数,且是偶数,一定可以从出发,到的步数为奇数,一定可以。
通过以上样例可以发现:
- 若是的偶数,一定可以;
- 若是奇数,比较奇数的最短路径。,一定可以。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,q,vis1[100005],vis2[100005];
vector<int>CSD[100005];
struct node{
int x,step;
};queue<node>op;
void BFS(){
memset(vis1,0x3f,sizeof vis1);
memset(vis2,0x3f,sizeof vis2);
op.push({1,0});
while(op.size()){
node f = op.front();
op.pop();
for(int i=0;i<CSD[f.x].size();i++){
int v = CSD[f.x][i];
if((f.step+1)%2 == 1){
if(f.step+1 < vis1[v]){
vis1[v] = f.step+1;
op.push({v,f.step+1});
}
}
else{
if(f.step+1 < vis2[v]){
vis2[v] = f.step+1;
op.push({v,f.step+1});
}
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>m>>q;
for(int i=1;i<=m;++i){
int x,y;
cin>>x>>y;
CSD[x].push_back(y);
CSD[y].push_back(x);
}
BFS();
while(q--){
int cur,yuan;
cin>>cur>>yuan;
if(yuan % 2 == 1){
if(vis1[cur] > yuan) cout<<"No\n";
else cout<<"Yes\n";
}
else{
if(vis2[cur] > yuan) cout<<"No\n";
else cout<<"Yes\n";
}
}
return 0;
}homework~
CF910A
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<int>op[105];
int n,d,vis[105];
string s;
struct node{
int x,step;
};
queue<node>LJ;
int BFS(int cur){
LJ.push({cur,0});
vis[cur] = 1;
while(LJ.size()){
node top = LJ.front();
LJ.pop();
if(top.x == n) return top.step;
for(int i=0;i<op[top.x].size();++i){
int v = op[top.x][i];
if(vis[v]) continue;
vis[v] = 1;
LJ.push({v,top.step+1});
}
}
return -1;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>d>>s;
s = " " + s;
for(int i=1;i<=n;++i){
if(s[i] == '1'){
for(int j=i+1;j<=i+d;++j){
if(s[j] == '1') op[i].push_back(j);
}
}
}
cout<<BFS(1)<<endl;
return 0;
}
CF115A
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,vis[2005];
vector<int>op[2005];
queue<int>CSP;
void BFS(int cur){
CSP.push(cur);
while(CSP.size()){
int top = CSP.front();
CSP.pop();
for(int i=0;i<op[top].size();++i){
int v = op[top][i];
if(vis[v] < vis[top] + 1) {
vis[v] = vis[top] + 1;
CSP.push(v);
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1;i<=n;++i){
int x;
cin>>x;
if(x != -1) op[x].push_back(i);
else vis[i] = 1;
}
for(int i=1;i<=n;++i){
if(vis[i] == 1) BFS(i);
}
int maxx = 0;
for(int i=1;i<=n;++i){
maxx = max(maxx,vis[i]);
}
cout<<maxx<<endl;
return 0;
}CF292B
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,P[100005],V[100005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;++i){
int x,y;
cin>>x>>y;
P[x]++,P[y]++;
}
for(int i=1;i<=n;++i) V[P[i]]++;
if(V[1] == 2 && V[2] == n-2) cout<<"bus topology\n";
else if(V[2] == n) cout<<"ring topology\n";
else if(V[n-1] == 1 && V[1] == n-1) cout<<"star topology\n";
else cout<<"unknown topology\n";
return 0;
}