CRJ J3结课考试总结

· · 个人记录

A T618144 迷宫之输出最短步数

赛时代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1005;
struct node{
    int x,y;
    int step;
};
char a[maxn][maxn];
int n,m;
bool vis[maxn][maxn];
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
bool check(int x,int y){
    if(x<1||x>n||y<1|y>m){
        return true;
    }
    if(a[x][y]=='1'){
        return true;
    }
    if(vis[x][y]==true){
        return true;
    }
    return false;
}
int bfs(int posx,int posy,int yyy1,int yyy2){
    queue<node>q;
    q.push({posx,posy,0});
    vis[posx][posy]=true;
    while(!q.empty()){
        node t=q.front();
        if(t.x==yyy1&&t.y==yyy2){
            return t.step;
        }
        q.pop();
        for(int i=0;i<4;i++){
            int nx=dx[i]+t.x;
            int ny=dy[i]+t.y;
            if(check(nx,ny)){
                continue;
            }
            vis[nx][ny]=true;
            q.push({nx,ny,t.step+1});
        }
    }
    return -1;
}
signed main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    cout<<bfs(1,1,n,m);
    return 0;
} 

B T618146 统计线路

赛时代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1005;
char a[maxn][maxn];
bool vis[maxn][maxn];
int n;
int cnt=0;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
bool check(int x,int y){
    if(x<1||x>n||y<1|y>n){
        return true;
    }
    if(a[x][y]=='1'){
        return true;
    }
    if(vis[x][y]==true){
        return true;
    }
    return false;
}
void dfs(int posx,int posy){
    if(a[posx][posy]=='2'){
        cnt++;
        return ;
    }
    for(int i=0;i<4;i++){
        int nx=dx[i]+posx;
        int ny=dy[i]+posy;
        if(check(nx,ny)){
            continue;
        }
        vis[nx][ny]=true;
        dfs(nx,ny);
        vis[nx][ny]=false;
    }
}
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
        }
    }
    vis[1][1]=true;
    dfs(1,1);
    cout<<cnt;
    return 0;
}

C T618168 全排列问题

赛时代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=105;
int ans[maxn],n;
bool vis[maxn];
void dfs(int x){
    if(x>n){
        for(int i=1;i<=n;i++){
            cout<<setw(5)<<ans[i];
        }
        cout<<"\n";
        return ;
    }
    for(int i=1;i<=n;i++){
        if(vis[i]==false){
            vis[i]=true;
            ans[x]=i;
            dfs(x+1);
            vis[i]=false;
            ans[x]=0;
        }
    }
}
signed main(){
    cin>>n;
    dfs(1);
    return 0;
}

T618173 学籍管理

赛时代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
map<string,int>mp;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    int cnt=0;
    while(n--){
        int op;
        cin>>op;
        if(op==1){
            string name;
            int score;
            cin>>name>>score;
            if(mp[name]==0||mp[name]==INT_MIN){
                cnt++;
            }
            mp[name]=score;
            cout<<"OK\n";
        }
        if(op==2){
            string name;
            cin>>name;
            if(mp[name]==INT_MIN){
                cout<<"Not found\n";
            }
            else{
                cout<<mp[name]<<"\n";
            }
        }
        if(op==3){
            string name;
            cin>>name;
            if(mp[name]==INT_MIN){
                cout<<"Not found\n";
            }
            else{
                cnt--;
                mp[name]=INT_MIN;
                cout<<"Deleted successfully\n";
            }
        }
        if(op==4){
            cout<<cnt<<"\n";
        }
    }
    return 0;
}

错因: 对于名字为 s 的同学,如果它之前没有过成绩,有两种情况:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
map<string,int>mp;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    int cnt=0;
    while(n--){
        int op;
        cin>>op;
        if(op==1){
            string name;
            int score;
            cin>>name>>score;
            if(mp[name]==0||mp[name]==INT_MIN){//删除了或之前压根没有它的成绩记录
                cnt++;
            }
            mp[name]=score;
            cout<<"OK\n";
        }
        if(op==2){
            string name;
            cin>>name;
            if(mp[name]==INT_MIN||mp[name]==0){
                cout<<"Not found\n";
            }
            else{
                cout<<mp[name]<<"\n";
            }
        }
        if(op==3){
            string name;
            cin>>name;
            if(mp[name]==INT_MIN||mp[name]==0){
                cout<<"Not found\n";
            }
            else{
                cnt--;
                mp[name]=INT_MIN;
                cout<<"Deleted successfully\n";
            }
        }
        if(op==4){
            cout<<cnt<<"\n";
        }
    }
    return 0;
}

T619294 二分查找左侧边界

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
int a[maxn],n;
void work(){
    int x;
    cin>>x;
    int lt=0,rt=n+1;
    while(lt+1<rt){
        int mid=(lt+rt)/2;
        if(a[mid]>=x){
            rt=mid;
        }
        else{
            lt=mid;
        }
    }
    if(a[rt]==x){
        cout<<rt<<" ";
    }
    else{
        cout<<-1<<" ";
    }
    return ;
}
signed main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int q;
    cin>>q;
    while(q--){
        work();
    }
    return 0;
}

P1873 [COCI 2011/2012 #5] EKO / 砍树

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
int n,m;
int a[maxn];
bool check(int x){
    int ans=0;
    for(int i=1;i<=n;i++){
        if(a[i]<=x){
            continue;
        }
        ans+=a[i]-x;
    }
    return ans>=m;
}
signed main(){
    cin>>n>>m;
    int lt=0,rt=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        rt=max(rt,a[i]);
    }
    rt++;
    int ans=0;
    while(lt+1<rt){
        int mid=(lt+rt)/2;
        if(check(mid)){
            ans=mid;
            lt=mid;
        }
        else{
            rt=mid;
        }
    }
    cout<<ans;
    return 0;
}