跳到正文

个人记录

5.22总结

JWM918约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年5月23日
收录
2026年5月28日
更新
2026年11月14日
篇幅
约 9 分钟

版本

  1. v1当前

5.22总结

课堂总结

P16556{\color{Orange}{P16556}} [ICPC 2026 LAC] Jaime’ s Palace

题目传送门(比赛)

考察知识点

  • 栈
  • 模拟
  • 贪心

错误原因

  • WA{\color{Red}{WA}}:排序方向错误

错误思路

使用栈进行模拟,得到tt的最大值

错误代码(WA){\color{Red}{(WA)}}:

错误代码
C++
#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;
}

正确思路

使用栈进行模拟,得到最坏情况下tt的值

正确代码(AC){\color{Green}{(AC)}}:

正确代码
C++
#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;
}

T763327{\color{Orange}{T763327}} 月色的神奇迷宫

题目传送门(比赛)

考察知识点

  • 广度优先搜索(BFSBFS)

错误原因

  • WA{\color{Red}{WA}}:仅用二维stst标记单元格是否访问过,忽略了方向状态

错误思路

BFSBFS模板,需要增加方向判断

错误代码(76分){\color{Yellow}{(76分)}}:

错误代码
C++
#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;
}

正确思路

方向判断除了需要判断是否不相等,还要判断是否不再同一条轴上,其余为BFSBFS模板

正确代码(100分){\color{Green}{(100分)}}:

正确代码
C++
#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;
}

T763330{\color{Yellow}{T763330}} 月色的真假钥匙

题目传送门(比赛)

考察知识点

  • 枚举
  • 深度优先搜索(DFSDFS)

错误原因

  • 没有解题思路

正确思路

DFSDFS选和不选模板,再判断所选是否合法即可

正确代码(100分){\color{Green}{(100分)}}:

正确代码
C++
#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;
}
原文