5.22总结
5.22总结
课堂总结
{\color{Orange}{P16556}} [ICPC 2026 LAC] Jaime' s Palace
题目传送门(比赛)
考察知识点
- 栈
- 模拟
- 贪心
错误原因
-
### 错误思路 使用栈进行模拟,得到$t$的最大值 ### 错误代码${\color{Red}{(WA)}}$: :::error[错误代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; stack<int>s; int p,d,k,ans; vector<int>dishes; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>p>>d; for(int i=1;i<=p;i++)s.push(0);//初始化 for(int i=1;i<=d;i++){ cin>>k; while(k--){ int t=s.top(); s.pop(); dishes.push_back(t+1);//使用盘子,使用次数+1 } sort(dishes.begin(),dishes.end());//排序 for(auto j:dishes){ s.push(j);//放回堆栈 } dishes.clear(); } while(s.size()){ ans=max(ans,s.top()); s.pop(); } cout<<ans<<endl; return 0; } ``` ::: ### 正确思路 使用栈进行模拟,得到最坏情况下$t$的值 ### 正确代码${\color{Green}{(AC)}}$: :::success[正确代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; stack<int>s; int p,d,k,ans; vector<int>dishes; bool cmp(int x,int y){ return x>y; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>p>>d; for(int i=1;i<=p;i++)s.push(0); for(int i=1;i<=d;i++){ cin>>k; while(k--){ int t=s.top(); s.pop(); dishes.push_back(t+1);//使用盘子,使用次数+1 } sort(dishes.begin(),dishes.end(),cmp);//降序排序(最坏情况) for(auto j:dishes){ s.push(j);//放回堆栈 } dishes.clear(); } while(s.size()){ ans=max(ans,s.top()); s.pop(); } cout<<ans<<endl; return 0; } ``` ::: ### ${\color{Orange}{T763327}}$ 月色的神奇迷宫 [题目传送门(比赛)](https://www.luogu.com.cn/problem/T763327?contestId=328272) ### 考察知识点 - 广度优先搜索(
BFS )错误原因
-
### 错误思路 $BFS$模板,需要增加方向判断 ### 错误代码${\color{Yellow}{(76分)}}$: :::error[错误代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int n,m,sx,sy,fx,fy; char ma[1005][1005]; bool st[1005][1005]; int dx[4]={0,0,-1,1}; int dy[4]={-1,1,0,0}; struct node{ int x,y,step,dir;//dir:当前方向 }; queue<node>q; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>ma[i][j]; if(ma[i][j]=='S')sx=i,sy=j; if(ma[i][j]=='G')fx=i,fy=j; } } st[sx][sy]=1; q.push({sx,sy,0,4}); while(q.size()){ node t=q.front(); q.pop(); if(t.x==fx&&t.y==fy){ cout<<t.step<<endl; return 0; } for(int i=0;i<4;i++){ int nx=t.x+dx[i]; int ny=t.y+dy[i]; if(nx>0&&nx<=n&&ny>0&&ny<=m&&!st[nx][ny]&&ma[nx][ny]!='#'&&t.dir!=i){ st[nx][ny]=1; q.push({nx,ny,t.step+1,i}); } } } cout<<-1<<endl; return 0; } ``` ::: ### 正确思路 方向判断除了需要判断是否不相等,还要判断是否不再同一条轴上,其余为$BFS$模板 ### 正确代码${\color{Green}{(100分)}}$: :::success[正确代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int n,m,sx,sy,fx,fy; char ma[1005][1005]; bool st[1005][1005][5]; int dx[4]={0,0,-1,1}; int dy[4]={-1,1,0,0}; struct node{ int x,y,step,dir;//dir: 0横向,1纵向,2起点 }; queue<node>q; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>ma[i][j]; if(ma[i][j]=='S')sx=i,sy=j; if(ma[i][j]=='G')fx=i,fy=j; } } st[sx][sy][2]=1; q.push({sx,sy,0,2}); while(q.size()){ node t=q.front(); q.pop(); if(t.x==fx&&t.y==fy){ cout<<t.step<<endl; return 0; } for(int i=0;i<4;i++){ int nx=t.x+dx[i]; int ny=t.y+dy[i]; int d=dx[i]!=0;//当前方向 if(nx>0&&nx<=n&&ny>0&&ny<=m&&!st[nx][ny][d]&&ma[nx][ny]!='#'&&(d!=t.dir||t.dir==2)){//方向不等&不在同一条轴上 st[nx][ny][d]=1; q.push({nx,ny,t.step+1,d}); } } } cout<<-1<<endl; return 0; } ``` ::: ### ${\color{Yellow}{T763330}}$ 月色的真假钥匙 [题目传送门(比赛)](https://www.luogu.com.cn/problem/T763330?contestId=328272) ### 考察知识点 - 枚举
- 深度优先搜索(
DFS )错误原因
- 没有解题思路
正确思路
### 正确代码${\color{Green}{(100分)}}$: :::success[正确代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int n,m,k,ans; vector<int>a[105]; bool st[15]; char c[105]; void dfs(int x){ if(x>n){ for(int i=1;i<=m;i++){ int sum=0;//真钥匙总数 for(int j=0;j<a[i].size();j++){ if(st[a[i][j]])sum++; } //满足条件 if(c[i]=='o'&&sum>=k)continue; if(c[i]=='x'&&sum<k)continue; //不满足条件 return; } ans++; return; } //选 st[x]=1; dfs(x+1); //不选 st[x]=0; dfs(x+1); } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m>>k; for(int i=1;i<=m;i++){ int x; cin>>x; while(x--){ int u; cin>>u; a[i].push_back(u); } cin>>c[i]; } dfs(1); cout<<ans<<endl; return 0; } ``` :::