题意:
要求从左下角到右上角,中间会经过旋转门,而旋转门的房间固定只有一个房间能进入.旋转门每转90度,会消耗时间d,然而每个旋转门所消耗的时间都不同.而一般进入房间消耗单位1时间,求最少时间!
分析:其实是个很简单的最短路的题,,,,只是弱菜的我题意看错,每个旋转门消耗时间不同....还有写代码的能力不强,...老写错和漏掉什么的!唉~
只要简单处理下旋转门即可!
// File Name: 10967.cpp // Author: Zlbing // Created Time: 2013/5/29 10:18:14 #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #include<cstdio> #include<set> #include<map> #include<vector> #include<cstring> #include<stack> #include<cmath> #include<queue> using namespace std; #define CL(x,v); memset(x,v,sizeof(x)); #define INF 0x3f3f3f3f #define LL long long #define REP(i,r,n) for(int i=r;i<=n;i++) #define RREP(i,n,r) for(int i=n;i>=r;i--) const int MAXN=205; int n,m,T; char G[MAXN][MAXN]; int D[MAXN][MAXN]; int dx[4]= {0,0,1,-1}; int dy[4]= {1,-1,0,0}; int vis[MAXN][MAXN]; struct node { int x,y,cost; bool operator <(const node& rsh)const { return cost>rsh.cost; } }; int find(char a) { if(a=='W')return 1; if(a=='N')return 2; if(a=='E')return 3; if(a=='S')return 4; if(a=='.')return 0; if(a=='#')return -1; } int cha(int ax,int ay,int bx,int by) { if(ax==bx&&by>ay)return 1; if(ay==by&&ax<bx)return 2; if(ax==bx&&ay>by)return 3; if(ay==by&&ax>bx)return 4; } int cha2(int a,int b) { if(b==1){ b=3; }else if(b==2){ b=4; }else if(b==3)b=1; else if(b==4)b=2; if(abs(a-b)<=2)return abs(a-b); if(abs(a-b)>2)return 1; } int dfs() { priority_queue<node> Q; CL(vis,-1); Q.push((node) { n-1,0,0 }); node t,tt; vis[n-1][0]=0; while(!Q.empty()) { t=Q.top(); Q.pop(); // printf("now---x=%d y=%d cost=%d\n",t.x,t.y,t.cost); if(t.x==0&&t.y==m-1)return t.cost; for(int i=0; i<4; i++) { int xx=t.x+dx[i]; int yy=t.y+dy[i]; if(xx<0||xx>n-1||yy<0||yy>m-1)continue; int to=find(G[xx][yy]); int too=cha(t.x,t.y,xx,yy); if(find(G[t.x][t.y])==0&&to==0) { if(vis[xx][yy]==-1||t.cost+1<vis[xx][yy]) { vis[xx][yy]=t.cost+1; // printf("1pushinto---x=%d y=%d cost=%d\n",xx,yy,t.cost+1); Q.push((node) { xx,yy,t.cost+1 }); } } else if(too==to||to==0) { if(find(G[t.x][t.y])==0) { if(vis[xx][yy]==-1||t.cost+1<vis[xx][yy]) { vis[xx][yy]=t.cost+1; // printf("2pushinto---x=%d y=%d cost=%d\n",xx,yy,vis[xx][yy]); Q.push((node) { xx,yy,t.cost+1 }); } } else { int c=cha2(find(G[t.x][t.y]),too); // printf("too=%d c=%d\n",too,c); if(vis[xx][yy]==-1||t.cost+1+D[t.x][t.y]*c<vis[xx][yy]) { vis[xx][yy]=t.cost+1+D[t.x][t.y]*c; // printf("3pushinto---x=%d y=%d cost=%d\n",xx,yy,vis[xx][yy]); Q.push((node) { xx,yy,t.cost+1+D[t.x][t.y]*c }); } } } else continue; } } return vis[0][m-1]; } int main() { scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); CL(D,0); REP(i,0,n-1) scanf("%s",G[i]); REP(i,0,n-1) REP(j,0,m-1) { if(G[i][j]!='.'&&G[i][j]!='#') scanf("%d",&D[i][j]); } int ans=dfs(); if(ans==-1)printf("Poor Kianoosh\n"); else printf("%d\n",ans); } return 0; }