22/6/29

1,cf Solve The Maze;2,acwing 1191.家谱树;3,acwing 1192.奖金;


1,cf solve the maze

题意:给出n*m 的矩阵,'.'代表空地,'#'代表墙,'B'代表坏人,'G'代表好人,你现在可以在任意空地上加墙,来使得好人可以到的第(n,m)块地,坏人无法到达;如果可以这样,输出yes,否则输出no;

思路:直接给坏人围墙,然后从(n,m)bfs,如果能遍历到所有好人,就是yes;

注意特判:没有好人就是yes;好人和坏人挨着,no;终点是墙,no;

细节:注意bfs标记st为1的时机,放入队列的时候就标记,而不是出队在标记;后者会导致tle;

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( int i=a;i<n;++i) 
#define rep2(i,a,n) for( int i=a;i<=n;++i) 
#define per1(i,n,a) for( int i=n;i>a;i--) 
#define per2(i,n,a) for( int i=n;i>=a;i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define mset(a,i,b) memset((a),(i),sizeof (b))
#define mcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int,int> PII;
typedef pair<long long,long long>PLL;
typedef pair<int,PII> PIII;
typedef long long LL;
typedef double dob;
const int N=110;
int n,m;
char a[N][N];
vector<PII>g,b;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int st[N][N];
void bfs()
{
    queue<PII>q;
    q.push({n,m});
    while(q.size())
    {
        auto t=q.front();
        q.pop();
        int x=t.yi,y=t.er;
        rep1(i,0,4)
        {
            int xx=x+dx[i];
            int yy=y+dy[i];
            if(xx<1||yy<1||xx>n||yy>m||a[xx][yy]=='#'||st[xx][yy])continue;
            q.push({xx,yy});
            st[xx][yy]=1;
        }
    }
}
void solve()
{
    cin>>n>>m;
    mset(a,0,a);
    mset(st,0,st);
    g.clear();
    b.clear();
    rep2(i,1,n)
        rep2(j,1,m)
        {
            cin>>a[i][j];
            if(a[i][j]=='B')b.pb({i,j});
            if(a[i][j]=='G')g.pb({i,j});
        }
    if(!g.size())
    {
        YES;
        return;
    }
    int bsiz=b.size();
    rep1(i,0,bsiz)
    {
        rep1(j,0,4)
        {
            int x=b[i].yi+dx[j];
            int y=b[i].er+dy[j];
            if(a[x][y]=='G')
            {
                NO;
                return;
            }
            if(a[x][y]=='.')a[x][y]='#';
        }
    }
    if(a[n][m]=='#')
    {
        NO;
        return;
    }
    bfs();
    int gsiz=g.size();
    rep1(i,0,gsiz)
    {
        if(!st[g[i].yi][g[i].er])
        {
            NO;
            return;
        }
    }
    YES;
}
signed main()
{
    quick_cin();
    int T;
    cin>>T;
    while(T--)solve();

    return 0;
}

浅记一下memset中,sizeof 和数字的区别

sizeof 跟一个数字,然后得到数组的长度,所以不能sizeof(40),这是错误的;

而单独数字,则代表40个字节长度,int 单位字节长度是4,所以10*4=40;

2,家谱树;

 思路:简单拓扑排序,注意add(a,b)和d[b]++;

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( int i=a;i<n;++i) 
#define rep2(i,a,n) for( int i=a;i<=n;++i) 
#define per1(i,n,a) for( int i=n;i>a;i--) 
#define per2(i,n,a) for( int i=n;i>=a;i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int,int> PII;
typedef pair<long long,long long>PLL;
typedef pair<int,PII> PIII;
typedef long long LL;
typedef double dob;
int n;
const int N=1e5+10;
int e[N],ne[N],h[N],idx;
int d[N];
void add(int a,int b)
{
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
vector<int>ans;
void topsort()
{
    queue<int>q;
    rep2(i,1,n)if(!d[i])q.push(i);
    while(q.size())
    {
        int t=q.front();
        q.pop();
        
        ans.pb(t);
        for(int i=h[t];~i;i=ne[i])
        {
            int j=e[i];
            d[j]--;
            if(d[j]==0)
            {
                q.push(j);
            }
        }
    }
}
signed main()
{
    quick_cin();
    memset(h,-1,h);
    cin>>n;
    rep2(i,1,n)
    {
        int x;
        while(cin>>x,x)
        {
            add(i,x);
            d[x]++;
        }
    }
    //rep2(i,1,n)cout<<d[i]<<endl;
    topsort();
    int siz=ans.size();
    rep1(i,0,siz)cout<<ans[i]<<" ";
    return 0;
}

3,奖金;

递推关系,拓扑序列; 

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( int i=a;i<n;++i) 
#define rep2(i,a,n) for( int i=a;i<=n;++i) 
#define per1(i,n,a) for( int i=n;i>a;i--) 
#define per2(i,n,a) for( int i=n;i>=a;i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int,int> PII;
typedef pair<long long,long long>PLL;
typedef pair<int,PII> PIII;
typedef long long LL;
typedef double dob;
int n,m;
const int N=1e5+10;
int e[N],ne[N],h[N],idx;
int d[N],dist[N];
void add(int a,int b)
{
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
vector<int>xl;
bool topsort()
{
    queue<int>q;
    rep2(i,1,n)if(!d[i])q.push(i);
    while(q.size())
    {
        int t=q.front();
        q.pop();
        
        xl.pb(t);
        for(int i=h[t];~i;i=ne[i])
        {
            int j=e[i];
            d[j]--;
            if(d[j]==0)
            {
                q.push(j);
            }
        }
    }
    return xl.size()==n;
}
signed main()
{
    quick_cin();
    memset(h,-1,h);
    cin>>n>>m;
    while (m--)
    {
        int a,b;
        cin>>a>>b;
        add(b,a);
        d[a]++;
    }
    if(!topsort())cout<<"Poor Xed";
    else
    {
        rep2(i,1,n)dist[i]=100;
        int siz=xl.size();
        rep1(i,0,siz)
        {
            int j=xl[i];
            for(int k=h[j];~k;k=ne[k])
            {
                int jj=e[k];
                dist[jj]=max(dist[jj],dist[j]+1);
            }
        }
        int ans=0;
        rep2(i,1,n)ans+=dist[i];
        cout<<ans;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dull丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值