标签:des c style class blog code
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 7373 | Accepted: 1715 |
Description
Input
Output
Sample Input
8 9 1 1 1 3 2 1 1 3 3 1 1 3 4 1 1 3 1 1 0 3 1 2 0 3 1 3 0 3 1 4 0 3 2 1 1 2 2 1 2 3 1 3 1 1 3 2 1 3 3 1 1 2 0 3 3 0 4 3 1 1.5 1.5 4 0 1 1 0 1 1 1 1 1 2 1 1 1 1 2 0 1 1.5 1.7
1 6 1 2 1 5 0 2 0 0 3 0 0 4 0 0 5 0 0 6 0 0 7 0 0.5 5.5
4 1 3 2 1 1 1 3 0 2 1 1 0 2 1 1 1 2 2 1 1 1.5 1.5 -1 -1
Sample Output
5 -1
2
0
题目意思很清晰了,直接上AC代码了,里面解释很清楚
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> using namespace std; #define MM 210 #define MX 9999999 struct Node{ int x,y,dis; friend bool operator <(const Node &a,const Node &b){ return a.dis>b.dis; //注意这里是>,因为优先队列是大顶堆结构 } Node(int X,int Y,int DIS){ x=X; y=Y; dis=DIS; } }; int xe[MM][MM],ye[MM][MM]; //xe表示横向边 ye表示纵向边 int dis[MM][MM]; //表示到 0 0的距离 int f_x,f_y; priority_queue <Node> q; //优先队列 int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int M,N; while(scanf("%d%d",&M,&N)!=EOF&&(M!=-1 || N!=-1)){ memset(xe,0,sizeof(xe)); memset(ye,0,sizeof(ye)); int x,y,d,t; int mx,my; mx=my=0; for(int i=0;i<M;i++){ //初始化墙 scanf("%d%d%d%d",&x,&y,&d,&t); if(d==1){ for(int j=0;j<t;j++) ye[x][y+j]=MX; //ye[x][y]表示 x y+1这条边 mx=max(mx,x); my=max(my,y+t); } else{ for(int j=0;j<t;j++) xe[x+j][y]=MX; mx=max(mx,x); my=max(my,y+t); } } for(int i=0;i<N;i++){ //初始化门 scanf("%d%d%d",&x,&y,&d); if(d==1){ ye[x][y]=1; mx=max(mx,x); my=max(my,y+1); } else{ xe[x][y]=1; mx=max(mx,x+1); my=max(my,y); } } float xd,yd; scanf("%f%f",&xd,&yd); if(xd>mx || yd>my){ printf("0\n"); continue; } f_x=(int)xd; f_y=(int)yd; for(int i=0;i<=mx;i++) for(int j=0;j<=my;j++) dis[i][j]=MX; dis[0][0]=0; int tmp_x,tmp_y; int flag=0; while(!q.empty()) q.pop(); q.push(Node(0,0,0)); while(!q.empty()){ //从0 0出发bfs直到到达目的地 tmp_x=q.top().x; tmp_y=q.top().y; q.pop(); if(tmp_x==f_x && tmp_y==f_y){ //到达目的地,退出 flag=1; break; } if(tmp_y+1<=my && dis[tmp_x][tmp_y+1]>dis[tmp_x][tmp_y]+xe[tmp_x][tmp_y+1]){ //向上走 dis[tmp_x][tmp_y+1]=dis[tmp_x][tmp_y]+xe[tmp_x][tmp_y+1]; q.push(Node(tmp_x,tmp_y+1,dis[tmp_x][tmp_y+1])); } if(tmp_x+1<=mx && dis[tmp_x+1][tmp_y]>dis[tmp_x][tmp_y]+ye[tmp_x+1][tmp_y]){ //向右走 dis[tmp_x+1][tmp_y]=dis[tmp_x][tmp_y]+ye[tmp_x+1][tmp_y]; q.push(Node(tmp_x+1,tmp_y,dis[tmp_x+1][tmp_y])); } if(tmp_y-1>=0 && dis[tmp_x][tmp_y-1]>dis[tmp_x][tmp_y]+xe[tmp_x][tmp_y]){ //向下走 注意:好好体会这里的 +xe[tmp_x][tmp_y] dis[tmp_x][tmp_y-1]=dis[tmp_x][tmp_y]+xe[tmp_x][tmp_y]; //而不是 +xe[tmp_x][tmp_y-1] q.push(Node(tmp_x,tmp_y-1,dis[tmp_x][tmp_y-1])); } if(tmp_x-1>=0 && dis[tmp_x-1][tmp_y]>dis[tmp_x][tmp_y]+ye[tmp_x][tmp_y]){ //向左走 注意:好好体会这里的 +ye[tmp_x][tmp_y] dis[tmp_x-1][tmp_y]=dis[tmp_x][tmp_y]+ye[tmp_x][tmp_y]; //而不是 +xe[tmp_x][tmp_y-1] q.push(Node(tmp_x-1,tmp_y,dis[tmp_x-1][tmp_y])); } } if(flag) printf("%d\n",dis[f_x][f_y]); else printf("-1\n"); } return 0; }
标签:des c style class blog code
原文地址:http://blog.csdn.net/my_acm/article/details/27565081