bonus3 Ans

bonus3 Ans - while!Q.empty 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(! return>; 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.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 Document Right 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()) {; 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.ty=p.ty; if(!Bound(temp.x,temp.y)) continue; if(maze[temp.x][temp.y]<0) { if(>=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) {; 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 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