bonus3 Ans

bonus3 Ans - while(!Q.empty()) { p=Q.top(); Q.pop(); tt=1;

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <queue> #include <cmath> #define MAX 502 using namespace std; typedef struct node { int x; int y; int day; int ty; friend bool operator < (node a,node b) { if(a.day!=b.day) return a.day>b.day; else return a.ty>b.ty; } }Point; int maze[MAX][MAX],n,m,sum,a[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; int num[MAX*MAX]; priority_queue<Point>Q; Point p,temp; void Init() { while(!Q.empty()) Q.pop(); int i,j; sum=0; for(i=0;i<MAX*MAX;i++) num[i]=0; for(i=0;i<n;i++) for(j=0;j<m;j++) { scanf("%d",&maze[i][j]); if(maze[i][j]>0) { p.x=i; p.y=j; p.day=1; p.ty=maze[i][j]; num[p.ty]++; Q.push(p); sum++; } } } bool Bound(int x,int y) { return 1; else return 0; } void BFS() { int i,tt;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: while(!Q.empty()) { p=Q.top(); Q.pop(); tt=1; for(i=0;i&lt;4;i++) { temp.x=p.x+a[i][0]; temp.y=p.y+a[i][1]; temp.day=p.day; temp.ty=p.ty; if(!Bound(temp.x,temp.y)) continue; if(maze[temp.x][temp.y]&lt;0) { if(p.day&gt;=abs(maze[temp.x][temp.y])) { maze[temp.x][temp.y]=p.ty; sum++; num[p.ty]++; if(sum==n*m) return ; Q.push(temp); } else { if(maze[temp.x][temp.y]&gt;tt||tt==1) tt=maze[temp.x][temp.y]; } } } if(tt!=1) { p.day=-tt; Q.push(p); } } } int main() { while(scanf(&quot;%d%d&quot;,&amp;n,&amp;m)!=EOF) { int i,j,Q,type,s; Init(); BFS(); scanf(&quot;%d&quot;,&amp;Q); while(Q--) { scanf(&quot;%d&quot;,&amp;type); printf(&quot;%d\n&quot;,num[type]); } } return 0; }...
View Full Document

Page1 / 2

bonus3 Ans - while(!Q.empty()) { p=Q.top(); Q.pop(); tt=1;

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online