2025.07.29课堂总结
闲话
哎呀,差点就成神了!!!
T638298 小明搭公交
这道题就是一道简单的模拟题,上代码!:
#include<bits/stdc++.h>
using namespace std;
struct S{
int s,t;
}a[114];
bool cmp(S x, S y){
return x.s < y.s;
}
int n, x, maxx;
int main(){
cin >> n >> x;
for(int i = 1; i <= n; i++) cin>> a[i].s >> a[i].t;
sort(a + 1, a + n + 1, cmp);//排序
for(int i = 1; i <= n; i++){
if(a[i].s + a[i].t <= x) maxx = max(maxx, a[i].s);//满足条件时找最晚出发
}
if(maxx == 0) cout<< -1;//特判
else cout << maxx;//输出答案
return 0;
} T638280 兜兜转转还是你
这道题挂了个黄题,有难度的,正解如下:
#include<bits/stdc++.h>
using namespace std;
int n, m, si, sj, flag;
vector<vector<char> > f;
vector<vector<int> > vis;
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void dfs(int x, int y, int step){//搜索
for(int i = 0;i < 4; i++){
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && !vis[xx][yy]){
if(f[xx][yy] == 'S' && step >= 3){
flag = 1;
return ;
}
if(f[xx][yy] == '.'){
vis[xx][yy] = 1;
dfs(xx, yy, step + 1);
}
}
}
}
int main(){
cin >> n >> m;
f.resize(n + 1, vector<char>(m + 1));
vis.resize(n + 1, vector<int>(m + 1, 0));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> f[i][j];
if(f[i][j] == 'S'){
si = i;
sj = j;
}
}
}
for(int i = 0; i < 4;i ++){
int xx = si + dir[i][0];
int yy = sj + dir[i][1];
if(xx >= 1&& xx <= n && yy >= 1&& yy <= m && f[xx][yy] == '.'){//检查连通性
vis[xx][yy] = 1;
dfs(xx, yy, 1);
vis[xx][yy] = 0;
if(flag) break;
}
}
cout << (flag ? "Yes" : "No");//输出
return 0;
}