病毒按照時間的推移感染防御等級不一樣的電腦,無法用常規的搜索以時間的改變進行多次計算。這里用了優先隊列,在一次搜索的過程中不斷更新時間和病毒類型的排列,使得在時間相同的時候先從病毒等級低的節點搜索,否則時間在前面的先搜索。牛逼啊。,沒有自己寫過優先隊列當時居然想不到用這個功能來解決在時間變化的過程中的麻煩。知其然不知其所以然的水平也就是這個程度了,一轉眼就要畢業了回顧每天接觸的東西 大概也只算是一個入門
#include<iostream>
#include<queue>
#include <cstdio>
#include <cstring>
using namespace std;
struct Node


{
int vi;
int vj;
int day;
int type;
friend bool operator < (Node a,Node b)

{
if(a.day != b.day)
return a.day > b.day;
else
return a.type > b.type;
}
};
priority_queue<Node>Q;

int dir[4][2] =
{-1,0,1,0,0,-1,0,1};
int m,n;
int gra[501][501];
int sum[250002];
void init()


{
int i,j;
Node p;
memset(sum,0,sizeof(sum));
for(i=1;i<=m;i++)

{
for(j=1;j<=n;j++)

{
scanf("%d",&gra[i][j]);
if(gra[i][j] > 0)

{
p.vi = i;
p.vj = j;
p.day = 1;
p.type = gra[i][j];
Q.push(p);
sum[gra[i][j]] ++;
}
}
}
}

void bfs()


{
int k,dmax;
Node p,q;
while(!Q.empty())

{
q = Q.top();
Q.pop();
dmax = -111111111;
for(k=0;k<4;k++)

{
p.vi = q.vi + dir[k][0];
p.vj = q.vj + dir[k][1];
if(p.vi>=1 && p.vi<=m && q.vi>=1 && q.vj<=n )

{
if(gra[p.vi][p.vj] < 0 )

{
if( gra[p.vi][p.vj] + q.day >= 0)

{
p.type = q.type;
p.day = q.day;
gra[p.vi][p.vj] = p.type;
Q.push(p);
sum[p.type] ++;
}
else if(gra[p.vi][p.vj] > dmax)//尋找其周圍最快傳染的機子~

{
dmax = gra[p.vi][p.vj] ;
}
}
}//for(k=0;k<4;k++)
}
if(dmax != -111111111)

{
q.day = dmax * (-1);
Q.push(q);
}
}
}
int main()


{
int a,T;
while(cin>>m>>n)

{
init();
bfs();
cin>>T;
while(T--)

{
scanf("%d",&a);
printf("%d\n",sum[a]);
}
}
return 0;
}


posted on 2011-04-13 18:01
mr_chen 閱讀(387)
評論(0) 編輯 收藏 引用 所屬分類:
搜索