BZOJ 2464 中山市选2009 小明的游戏 SPFA

来源:互联网 发布:手机照相特效软件 编辑:程序博客网 时间:2024/05/01 16:36

题目大意:给定一个由'#'和'@'构成的二维矩阵,走到不同的字符需要代价1,求s到t的最短路

签到题+1

这操作符重载要不要写的这么高大上……

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 510using namespace std;typedef pair<int,int> abcd;template<typename T>struct Reader{T xx[M][M];T& operator [] (abcd x){return xx[x.first][x.second];}};int m,n;abcd q[1<<16],s,t;unsigned short r,h;Reader<char>map;Reader<int>f;Reader<bool>v;const int dx[]={0,0,1,-1};const int dy[]={1,-1,0,0};void SPFA(){int i;memset(&f,0x3f,sizeof f);v[s]=1;f[s]=0;q[++r]=s;while(r!=h){abcd x=q[++h];v[x]=0;for(i=0;i<4;i++){abcd y(x.first+dx[i],x.second+dy[i]);if(y.first<=0||y.second<=0||y.first>m||y.second>n)continue;if(f[y]>f[x]+(map[x]!=map[y]) ){f[y]=f[x]+(map[x]!=map[y]);if(!v[y])v[y]=1,q[++r]=y;}}}}int main(){int i;while(cin>>m>>n,m||n){for(i=1;i<=m;i++)scanf("%s",map.xx[i]+1);scanf("%d%d",&s.first,&s.second);s.first++;s.second++;scanf("%d%d",&t.first,&t.second);t.first++;t.second++;SPFA();printf("%d\n",f[t]);}}


0 0
原创粉丝点击