标签:mst iss tor numbers which accept its 数据 using
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 26952 | Accepted: 12721 |
Description
Input
Output
Sample Input
3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1
Sample Output
5 28 0
Source
#include<cstdio> #include<cstring> #include<queue> using namespace std; const int MAX=305; int l,sx,sy,ex,ey; int dx[8]={-2,-2,-1,-1,1,1,2,2}; int dy[8]={1,-1,2,-2,2,-2,1,-1}; bool vis[MAX][MAX]; struct Node { int x,y,step; bool operator < (const Node &a) const{ return a.step<step; } }; priority_queue<Node> q;//利用优先队列,每次出队的为步数较小的。 void bfs() { Node now,next; now.x=sx; now.y=sy; now.step=0; while(!q.empty()) //由于多组数据,每次bfs都要清空q { q.pop(); } q.push(now); while(!q.empty()) { now = q.top(); q.pop(); if(now.x==ex&&now.y==ey) //bfs结束,找到出口 { printf("%d\n",now.step); break; } for(int i=0;i<8;i++) { next.x = now.x+dx[i]; next.y = now.y+dy[i]; if(next.x>=0&&next.x<l &&next.y>=0&&next.y<l && !vis[next.x][next.y]) { vis[next.x][next.y]=true; next.step = now.step+1; q.push(next); } } } } int main() { int Case; scanf("%d",&Case); while(Case--) { memset(vis,false,sizeof(vis)); scanf("%d",&l); scanf("%d %d",&sx,&sy); scanf("%d %d",&ex,&ey); bfs(); //printf("bfs-end\n"); } }
标签:mst iss tor numbers which accept its 数据 using
原文地址:http://www.cnblogs.com/WWkkk/p/7296302.html