bonus3 Ans

# bonus3 Ans - while!Q.empty p=Q.top Q.pop tt=1...

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

#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;

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

View Full Document
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<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]<0) { if(p.day>=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]>tt||tt==1) tt=maze[temp.x][temp.y]; } } } if(tt!=1) { p.day=-tt; Q.push(p); } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { int i,j,Q,type,s; Init(); BFS(); scanf("%d",&Q); while(Q--) { scanf("%d",&type); printf("%d\n",num[type]); } } return 0; }...
View Full Document

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online