5.28 河西J3训练课-田

· · 个人记录

P9749 [CSP-J 2023] 公路

题目

给定一个 n ,会得到一个序列 b :1, 1, 2, 1, 2, 3, \dots, 1, ..., n 再给定一个长度为 m 的序列 a ,问这个序列在 b 中的出现次数。

100分写法

  1. 如果 a 数组出现了跨越一块的情况,出现次数一定是1
  2. 没有跨越,说明 a 数组一定是升序的,a[m] 一定是最大的数字,那么 a 序列第一次一定在第 a[m] 块出现,并且后面的每一块都会出现, n−a[m]+1
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define qwq ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
int t,a[100005]; 
signed main(){
    qwq;
    cin>>t;
    while(t--){
        int n,m,f=0;
        cin>>n>>m;
        for(int i=1;i<=m;i++){
            cin>>a[i];
        }
        for(int i=2;i<=m;i++){
            if(a[i]<=a[i-1]){
                cout<<1<<endl;
                f=1;
                break;
            }
        }
        if(f==0){
            cout<<n-a[m]+1<<endl;
        }
    }
    return 0;
} 

总结

只写了一个暴力解,拿了30分

月色的神奇迷宫

题目

你现在位于起点。你的目标是通过重复移动到当前单元格边相邻的单元格,最终到达终点。但有以下限制:不能移动到障碍物 # 或网格外,且必须交替进行纵向移动和横向移动(首次移动方向可任选)。

请判断能否到达终点,若可能则求出移动次数的最小值。

100分写法

就是用一个有多重状态的广搜写出这道题

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define qwq ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
struct st{
    int x,y,st;
    int k;
};
int n,m,sx,sy,vis[1005][1005][2]; 
char a[1005][1005];
int d[4][2]={
    {0,-1},{0,1},{-1,0},{1,0}
};
void bfs(){
    queue<st>q;
    q.push({sx,sy,0,-1});
    vis[sx][sy][0]=vis[sx][sy][1]=1;
    while(q.size()){
        st now=q.front();
        q.pop();
        if(a[now.x][now.y]=='G'){
            cout<<now.st;
            return ;
        }
        for(int i=0;i<4;i++){
            int nx=now.x+d[i][0],ny=now.y+d[i][1];
            if(i/2==now.k){
                continue;
            }
            if(nx<1 || nx>n || ny<1 || ny>m){
                continue;
            }
            if(vis[nx][ny][i/2]==1 || a[nx][ny]=='#'){
                continue;
            }
            q.push({nx,ny,now.st+1,i/2});

            vis[nx][ny][i/2]=1;
        }
    }
    cout<<-1;
}
signed main(){
    qwq;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
            if(a[i][j]=='S'){
                sx=i;
                sy=j;
            }
        }
    }
    bfs();
    return 0;
} 

总结

骗了点分