D. Kilani and the Game(多源bfs)

题目链接:http://codeforces.com/contest/1105/problem/D

题目大意:给你n,m,p。然后n,m代表这个图的大小,接下来输入p个数,第i个数a[i]代表编号为i的行走速度,行走顺序是按照编号从小的开始的,每一次最多可以走a[i]步,可以小于,然后问你这个图上每个人占领的个数是多少。

具体思路:bfs,我们先将所有的点按照编号顺序进队列(同一个编号的点可能有多个),然后每一次将所有编号为1的塞入另一个队列,通过这个队列来进行bfs,这样就能保证当前的一定是编号在前面的先进行bfs,并且下一个点也一定是按照编号顺序进行bfs的。

AC代码:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 # define ll long long
  4 const int maxn = 1e3+100;
  5 const int mod = 1e9+7;
  6 int sp[maxn];
  7 int f[2][4]= {{1,-1,0,0},{0,0,1,-1}};
  8 char a[maxn][maxn];
  9 int vis[maxn][maxn];
 10 map<int,int>ans;
 11 struct node
 12 {
 13     int x,y;
 14     node() {}
 15     node(int xx,int yy)
 16     {
 17         x=xx;
 18         y=yy;
 19     }
 20 } ;
 21 struct node2
 22 {
 23     int x,y,step;
 24     node2() {}
 25     node2(int xx,int yy,int zz)
 26     {
 27         x=xx;
 28         y=yy;
 29         step=zz;
 30     }
 31 };
 32 int n,m,p;
 33 bool judge(int x,int y)
 34 {
 35     if(x>=1&&x<=n&&y>=1&&y<=m&&vis[x][y]==0&&a[x][y]=='.')
 36         return true;
 37     return false;
 38 }
 39 queue<node>q1;
 40 void bfs()
 41 {
 42     while(!q1.empty())
 43     {
 44         node t1=q1.front();
 45         q1.pop();
 46         queue<node2>q2;
 47         q2.push(node2(t1.x,t1.y,sp[vis[t1.x][t1.y]]));
 48         while(!q1.empty()&&(vis[q1.front().x][q1.front().y]==vis[t1.x][t1.y]))
 49         {
 50             q2.push(node2(q1.front().x,q1.front().y,sp[vis[t1.x][t1.y]]));
 51             q1.pop();
 52         }
 53         while(!q2.empty())
 54         {
 55             node2 tmp=q2.front();
 56             q2.pop();
 57             if(tmp.step<=0)
 58                 continue;
 59             for(int i=0; i<4; i++)
 60             {
 61                 int tx=tmp.x+f[0][i];
 62                 int ty=tmp.y+f[1][i];
 63                 if(!judge(tx,ty))
 64                     continue;
 65                 vis[tx][ty]=vis[tmp.x][tmp.y];
 66                 q2.push(node2(tx,ty,tmp.step-1));
 67                 q1.push(node(tx,ty));
 68             }
 69         }
 70     }
 71 }
 72 int main()
 73 {
 74     scanf("%d %d %d",&n,&m,&p);
 75     for(int i=1; i<=p; i++)
 76     {
 77         scanf("%d",&sp[i]);
 78     }
 79     for(int i=1; i<=n; i++)
 80     {
 81         scanf("%s",a[i]+1);
 82 //        for(int j=1; j<=m; j++)//如果找到就进入队列的话,无法保证是按照编号进行bfs的。
 83 //        {
 84 //                q1.push(node(i,j));
 85 //                vis[i][j]=(a[i][j]-'0');
 86 //        }
 87     }
 88     for(int k=1;k<=p;k++){
 89     for(int i=1;i<=n;i++){
 90     for(int j=1;j<=m;j++){
 91                 if(a[i][j]-'0'==k)
 92             {
 93 
 94             }
 95     }
 96     }
 97     }
 98     bfs();
 99     for(int i=1; i<=n; i++)
100     {
101         for(int j=1; j<=m; j++)
102         {
103             ans[vis[i][j]]++;
104         }
105     }
106     for(int i=1; i<=p; i++)
107     {
108         if(i==1)
109             printf("%d",ans[i]);
110         else
111             printf(" %d",ans[i]);
112     }
113     printf("\n");
114     return 0;
115 }

 

posted @ 2019-01-21 14:50  Let_Life_Stop  阅读(824)  评论(0编辑  收藏  举报