小鑫过河(优先队列+bfs) 字母重排 圆包含的方格个数 棋盘对角线穿越方格数

#include<cstdio>
#include<string.h>
#include<queue>

using namespace std;

struct Node{
  int x;
  int y;
  int t;
  Node(int xx=0, int yy=0, int tt=0):x(xx),y(yy),t(tt)
  {

  }
};

bool operator <(Node a, Node b)
{
    return a.t>b.t;
}

priority_queue<Node> q;
int map[11][11];
bool vis[11][11];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int n, m;
int ans;

void bfs(int x, int y)
{
    while(!q.empty())
    {
        q.pop();
    }
    Node tmp, tmp1;
    tmp.x=x; tmp.y=y;
    tmp.t=0;
    memset(vis, 0, sizeof(vis));
    if(map[x][y]==1)
        tmp.t++;
    vis[x][y]=1;
    q.push(tmp);
    while(!q.empty())
    {
        tmp=q.top(); q.pop();
        if(tmp.x==n-1)
        {
            if(ans>tmp.t)
                ans=tmp.t;
        }
        if(tmp.t>=ans) return;

        for(int i=0; i<4; i++)
        {
           tmp1.x=tmp.x+dx[i];
           tmp1.y=tmp.y+dy[i];
           if(tmp1.x>=0 && tmp1.x<n && tmp1.y>=0 &&tmp1.y<m &&!vis[tmp1.x][tmp1.y])
           {
                vis[tmp1.x][tmp1.y]=1;
               if(map[tmp1.x][tmp1.y])
                tmp1.t=tmp.t+1;
               else
                tmp1.t=tmp.t;
               q.push(tmp1);
           }
        }

    }
}

int main()
{
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
              scanf("%d", &map[i][j]);
        ans=100;
        for(int i=0; i<n; i++)
            bfs(0, i);
        printf("%d\n", ans);
    }


    return 0;
}
#include<cstdio>
#include<queue>
#include<string.h>

using namespace std;

bool vis[27];
int map[27][27];
char s[10][30];
int in[27];

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        memset(vis, 0, sizeof(vis));
        memset(map, 0, sizeof(map));
        memset(in, 0, sizeof(in));
        for(int i=0; i<n; i++)
        {
            scanf("%s", s[i]);
            int l = strlen(s[i]);
            for(int j=0; j<l; j++)
            {
                vis[s[i][j]-'a']=1;
            }
            for(int j=0; j<l-1; j++)
            {
                int x=s[i][j]-'a', y=s[i][j+1]-'a';
                if(s[i][j]!=s[i][j+1] &&!map[x][y]){
                    map[x][y]=1;
                    in[y]++;
                }
            }
        }
        queue<int> a;
        while(!a.empty()) a.pop();
        while(1)
        {
            int id=-1;
            for(int i=0; i<26; i++)
            {
                if(vis[i])
                {
                    if(in[i]==0)
                    {
                        id=i;break;
                    }

                }
            }
            if(id==-1)
            {
                break;
            }
            vis[id]=0;
            a.push(id);
            for(int i=0; i<26; i++)
                if(map[id][i])
                {
                   in[i]--;
                }
        }
        while(!a.empty())
        {
            printf("%c", a.front()+'a');
            a.pop();
        }
        printf("\n");
    }

    return 0;
}









#include<cstdio>
#include<cmath>
#include<algorithm>

using namespace std;

int main()
{
    double r;
    long long ans1, ans2;
    double y1, y2;
    while(~scanf("%lf", &r))
    {
        ans1=ans2=0;
        for(long long i=1; i<r; i++)
        {
            y1=sqrt(r*r-i*i);
            y2=sqrt(r*r-(i-1)*(i-1));
            ans1+=floor(y1);
            ans2+=ceil(y2)-floor(y1);
        }
        ans2+=ceil(y1);
        printf("%lld %lld\n", ans1*4, ans2*4);
    }

    return 0;
}


    #include<cstdio>
    #include<algorithm>

    using namespace std;

    long long gcd(long long a, long long b)
    {
        long long r = a%b;
        while(r!=0)
        {
            a=b;
            b=r;
            r=a%b;
        }
        return b;
    }

    int main()
    {
        long long n, m;
        while(~scanf("%lld%lld", &n, &m))
        {
            printf("%lld\n", n+m-gcd(n, m));
        }
        return 0;
    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值