HDU-1026Ignatius and the Princess I

来源:互联网 发布:小仙女的网络意思 编辑:程序博客网 时间:2024/05/01 06:39
#include<iostream>#include<queue>#include<algorithm>#include<fstream>#include<stdio.h>#define N 105#define M 1000struct  point{friend bool operator==(const point&l ,const point& r);friend bool operator<(const point&l,const point&r);int x;int y;int p;int flag;point():x(0),y(0),p(0),flag(0){}};int n,m;int t;char map[N][N];point   path[N][N];int  isvisted[N][N];int dir[][2]={{-1,0},{0,1},{1,0},{0,-1}};bool operator==(const point&l,const point& r){return l.x==r.x&&l.y==r.y;}bool operator<(const point&l,const point&r){return l.p>r.p;}point st,ed;int BFS(int x,int y){std::priority_queue<point> q; bool flag=false;point tmp;tmp.x=x;tmp.y=y;q.push(tmp);isvisted[x][y]=1;while (!q.empty()){tmp=q.top();q.pop();if(tmp==ed){flag=true;return tmp.p;}point tmp2;for(int i=0;i!=4;i++){tmp2.x=tmp.x+dir[i][0];tmp2.y=tmp.y+dir[i][1];if(tmp2.x>=0&&tmp2.x<n&&tmp2.y>=0&&tmp2.y<m&&isvisted[tmp2.x][tmp2.y]==0&&map[tmp2.x][tmp2.y]!='X'){if(map[tmp2.x][tmp2.y]=='.'){tmp2.p=tmp.p+1;tmp.flag=0;}else{tmp2.p=tmp.p+1+map[tmp2.x][tmp2.y]-'0';tmp.flag=map[tmp2.x][tmp2.y]-'0';}isvisted[tmp2.x][tmp2.y]=1;q.push(tmp2);path[tmp2.x][tmp2.y]=tmp;}}}if(flag==true){return 1;}else{return 0;}}void out(int x,int y){if(x==0&&y==0){return;}out(path[x][y].x,path[x][y].y);printf("%ds: (%d,%d->(%d,%d)\n",++t,path[x][y].x,path[x][y].y,x,y);if(path[x][y].flag!=0){for(int i=0;i!=path[x][y].flag;i++){printf("%ds: FIGHT AT (%d,%d)\n",++t,x,y);}}}int main(){//std::fstream fin("1.txt");while (std::cin>>n>>m){for(int i=0;i!=n;i++){for(int j=0;j!=m;j++){std::cin>>map[i][j];}}ed.x=n-1;ed.y=m-1;int res=BFS(0,0);if(res==0){std::cout<<"God please help our poor hero."<<std::endl;}else{std::cout<<"It takes "<<res<<" seconds to reach the target position, let me show you the way."<<std::endl;out(n-1,m-1);}std::cout<<"FINISH"<<std::endl;t=0;memset(map,0,sizeof(map));memset(isvisted,0,sizeof(isvisted));memset(path,0,sizeof(path));}}

原创粉丝点击