yxf 的 信息学奥赛复赛练习2

hanoi双塔问题

思路

递推鸟题

\[F_0=0\\ F_1=1\\ F_n=2F_{n-2}+2 \]

时间复杂度

预处理 \(O(n)\)
作答 \(O(1)\)

蒟蒻代码

#include <bits/stdc++.h>
#define re register
using namespace std;
typedef long long ll;

const int N=410;
int n;
ll ans[N];

void make_table(){
    ans[0]=0; ans[1]=1;
    for(re int i=1;i<=405;i++)
        ans[i]=2*ans[i-2]+2;
}

int main()
{
    ios::sync_with_stdio(0);
    clock_t c1 = clock();
#ifdef LOCAL
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
#endif
// ======================================================================
    make_table();
    cin>>n;
    cout<<ans[n<<1];

// ======================================================================
end:
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
    return 0;
}

天使的起誓

思路

m 每位处理,利用 mod 运算性质

时间复杂度

\(O(\log_{2}m)\)

蒟蒻代码

#include <bits/stdc++.h>
#define re register
using namespace std;

const int N=1e3+10;
int n;
int ans=0;
string m;
int base=1;

int main()
{
    ios::sync_with_stdio(0);
    clock_t c1 = clock();
#ifdef LOCAL
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
#endif
// ======================================================================
    cin>>n>>m;
    for(re int i=m.length()-1;i>=0;i--){
        ans=(ans+(m[i]-'0')*base)%n;
        base=base*10%n;
    }
    if(!ans) cout<<n;
    else cout<<ans;

// ======================================================================
end:
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
    return 0;
}

飞跃原野

思路

三维 BFS 鸟题

蒟蒻代码

#include <bits/stdc++.h>
#define re register
#define LOCAL
using namespace std;

struct rec{
    int x;
    int y;
    int cnt;
    int rest;    // 剩下飞行 
    rec(int _x,int _y,int _cnt,int _rest):x(_x),y(_y),cnt(_cnt),rest(_rest){}
};

const int N=110;
const int M=110;
const int D=110;
const int dx[4]={-1,0,0,1};
const int dy[4]={0,-1,1,0};

char a[N][M];
int n,m,d;
bool f[N][M][D];    // 到 (m,n) 位置且 还剩下 d 次飞行是否搜索过 
queue<rec> q;

inline bool valid(int x,int y){
    return x>0&&x<=m&&y>0&&y<=n&&a[y][x]=='P';
}

int main(){
    ios::sync_with_stdio(0);
# ifdef LOCAL
    freopen("fly.in","r",stdin);
    freopen("fly.out","w",stdout);
# endif
// ====================================================
    cin>>n>>m>>d;
    for(re int i=1;i<=n;i++)
        for(re int j=1;j<=m;j++)
            cin>>a[i][j];
    memset(f,0,sizeof(f));
    q.push(rec(1,1,0,d));
    while(!q.empty()){
        rec r=q.front(); q.pop();
        
        if(r.x==m&&r.y==n){
            cout<<r.cnt<<endl;
            goto end;
        }
        for(re int i=0;i<4;i++){
            int nx,ny;
            // 走路
            nx=r.x+dx[i];
            ny=r.y+dy[i];
            if(valid(nx,ny) && !f[ny][nx][r.rest]){
                f[ny][nx][r.rest]=1;
                q.push(rec(nx,ny,r.cnt+1,r.rest));    
            }        
            // 飞行
            for(re int j=2;j<=r.rest;j++){
                nx=r.x+j*dx[i];
                ny=r.y+j*dy[i];
                if(valid(nx,ny) && !f[ny][nx][r.rest-j]){
                    f[ny][nx][r.rest-j]=1;
                    q.push(rec(nx,ny,r.cnt+1,r.rest-j));
                }
                
            }
        }
    }
    puts("impossible");
    
// ====================================================
end:
    return 0;
}

yxf 的 \(20\) 分代码

依旧, 不出所料, yxf写的 BFS 又让他抓耳挠腮了......
找半天错没找出来......
此处挂上 yxf 的错误代码

#include<bits/stdc++.h>
using namespace std;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int a[105][105];
int q[1000005][5];
bool v[105][105][105];
int n,m,k;
int bfs(int x,int y,int s,int r)
{
    int front=0,rear=0;
    int xx,yy;
    rear++;
    q[rear][1]=x;
    q[rear][2]=y;
    q[rear][3]=s;
    q[rear][4]=r;
    v[1][1][r]=true;
    while(front<rear)
    {
        front++;
        x=q[front][1];
        y=q[front][2];
        r=q[front][4];
        for(int i=0;i<=3;i++)
        {
            xx=x+dx[i];
            yy=y+dy[i];
            if((a[xx][yy]==1)&&(!v[xx][yy][r]))
            {
                v[xx][yy][r]=true;
                rear++;
                q[rear][1]==xx;
                q[rear][2]=yy;
                q[rear][3]=q[front][3]+1;
                q[rear][4]=r;
                if(xx==n&&yy==m)
                {
                    cout<<q[rear][3];
                    return 0;
                }
                
            }
            for(int j=2;j<=r;j++)
            {
                xx=x+dx[i]*j;
                yy=y+dy[i]*j;
                if(a[xx][yy]==0)break;
                if(a[xx][yy]==1&&!v[xx][yy][r-j])
                {
                    v[xx][yy][r-j]=true;
                    rear++;
                    q[rear][1]==xx;
                    q[rear][2]=yy;
                    q[rear][3]=q[front][3]+1;
                    q[rear][4]=r-j;
                    if(xx==n&&yy==m)
                    {
                        cout<<q[rear][3];
                        return 0;
                    }
                }
            }
            
        }
    }
    cout<<"impossible";
}
int main()
{
   freopen("1.in","r",stdin);
   freopen("1.out","w",stdout);
   cin>>n>>m>>k;
   char ch;
    scanf("%c",&ch);//读掉回车
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++) {
            scanf("%c",&ch);
            if(ch=='P')a[i][j]=1;else a[i][j]=0;
        }
        scanf("%c",&ch);
    }
          
   if(a[1][1]!=1)
   {
        cout<<"impossible";
   }
   else
   {
       bfs(1,1,0,k);
   }
  return 0;
}

关系网络

思路

就建图,最后输出 \(SSSP(x\rightarrow y)-1\)

蒟蒻代码

#include <bits/stdc++.h>
#define re register
using namespace std;

struct Edge{
    int to;
    int nxt;
};

struct node{
    int ver;
    int d;
    node(int Ver,int D):ver(Ver),d(D){}
    bool operator <(const node& n1) const{
        return d>n1.d;
    }
};

const int N=110;
int n,x,y;
int tot=0,head[N];
Edge edge[N*N];

int dis[N];
bitset<N> vis;
priority_queue<node> pq;

void add(int x,int y){
    edge[++tot].to=y;
    edge[tot].nxt=head[x];
    head[x]=tot;
}

int main()
{
    ios::sync_with_stdio(0);
    clock_t c1 = clock();
#ifdef LOCAL
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
#endif
// ======================================================================
    cin>>n>>x>>y;
    memset(dis,0x3f,sizeof(dis));
    for(re int i=1;i<=n;i++){
        for(re int j=1;j<=n;j++){
            int tmp; cin>>tmp;
            if(tmp) add(i,j);
        }
    }

    // x->y
    dis[x]=0; pq.push(node(x,0));
    while(!pq.empty()){
        int a=pq.top().ver; pq.pop();
        if(vis[a]) continue;
        vis[a]=1;
        for(re int i=head[a];i;i=edge[i].nxt){
            int y=edge[i].to;
            if(dis[a]+1<dis[y]){
                dis[y]=dis[a]+1;
                pq.push(node(y,dis[y]));
            }
        }
    }
    cout<<dis[y]-1;
// ======================================================================
end:
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
    return 0;
}
posted @ 2021-10-11 12:47  不爱喝橙子汁的橙子  阅读(100)  评论(0编辑  收藏  举报