zoukankan      html  css  js  c++  java
  • Kilani and the Game-扩散形式的搜索

    Kilani and the Game

    思路:这种扩散走法的并且有速度。我们需要一层一层的入队, 而且 根据题目要求 按编号处理 
    例如q1队列中有 1 1 1 2 2 2 2 3 3 3 3 3 3 3  那么我们需要 把 id = 1 的一起处理 

    把 1 1 1 push 到 q2 然后只要不超过   s [  id = 1  ] 的速度 ,就一直进行bfs ,当到达了 s [ id = 1 ]的那些点可以放入q1中,

    .

    #include<bits/stdc++.h>
    using namespace std;
    #define maxn 1234
    int to[5][3]= {{1,0},{0,1},{0,-1},{-1,0}};
    char mmp[maxn][maxn];
    int n,m,p,s[23],pre;
    int dp[maxn][maxn],tong[55];
    bool judge(int x,int y)
    {
        if(x<0||y<0||x>=n||y>=m)return false;
        if(dp[x][y]!=0||mmp[x][y]=='#')return false;
        return true;
    }
    struct node
    {
        int x,y,id,step;
    } top,tp,op;
    vector<node>st[25];
    void bfs()
    {
        queue<node>q1,q2;
        for(int i=1; i<=p; i++)
            for(int j=0; j<st[i].size(); j++)
                q1.push(st[i][j]);
        pre=1;
        while(!q1.empty())
        {
            top=q1.front();
            while(top.id==pre&&!q1.empty())
            {
                q1.pop();
                q2.push(top);
                if(q1.empty())break;
                top=q1.front();
            }
            pre=top.id;
            while(!q2.empty())
            {
                op=q2.front();
                q2.pop();
                for(int i=0; i<4; i++)
                {
                    tp=op;
                    tp.x+=to[i][0];
                    tp.y+=to[i][1];
                    if(judge(tp.x,tp.y))
                    {
                        tp.step++;
                        dp[tp.x][tp.y]=tp.id;
                        if(tp.step==s[tp.id])
                            tp.step=0,q1.push(tp);
                        else q2.push(tp);
                    }
                }
            }
        }
    }
    int main()
    {
        scanf("%d%d%d",&n,&m,&p);
        for(int i=1; i<=p; i++)
            scanf("%d",&s[i]);
        for(int i=0; i<n; i++)
        {
            scanf("%s",mmp[i]);
            for(int j=0; j<m; j++)
            {
                if(mmp[i][j]!='#'&&mmp[i][j]!='.')
                {
                    int id=mmp[i][j]-'0';
                    st[id].push_back(node{i,j,id,0});
                    dp[i][j]=id;
                }
            }
        }
        bfs();
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                tong[dp[i][j]]++;
        for(int i=1; i<=p; i++)
        {
            printf("%d",tong[i]);
            if(i<p)printf(" ");
            else printf("
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    UVa 12174 (滑动窗口) Shuffle
    UVa 1607 (二分) Gates
    CodeForces ZeptoLab Code Rush 2015
    HDU 1525 (博弈) Euclid's Game
    HDU 2147 (博弈) kiki's game
    UVa 11093 Just Finish it up
    UVa 10954 (Huffman 优先队列) Add All
    CodeForces Round #298 Div.2
    UVa 12627 (递归 计数 找规律) Erratic Expansion
    UVa 714 (二分) Copying Books
  • 原文地址:https://www.cnblogs.com/SDUTNING/p/10300533.html
Copyright © 2011-2022 走看看