HDU1026

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26760433/article/details/86425227

这题我用广搜+优先队列做的,而且每个节点都需要保存父节点,这样根据最后一个节点就可以把路径倒序输出。

#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int mark[102][102];      //标记已经走过的路径
char maze[102][102];     //地图
int weight[102][102];    //怪兽
int n,m;
class step
{
public:
	int x,y;
	int steps;
	step *last;
	step(){}
	step(int x1,int y1,int s,step *l):x(x1),y(y1),steps(s),last(l){}
	friend bool operator<(const step &s1,const step &s2)
	{
		return s1.steps>s2.steps;
	}
};
int main()
{
	step array[10005];
	int flag;
	while(cin>>n>>m)
	{
		int index=0;
		flag=0;             //还没找到路
		memset(mark,0,sizeof(mark));
		memset(maze,'\0',sizeof(maze));
		memset(weight,0,sizeof(weight));
		for(int i=0;i<=m+1;i++)
		{
			maze[0][i]=maze[n+1][i]='X';
		}
		for(int i=0;i<n+1;i++)
		{
			maze[i][0]=maze[i][m+1]='X';
		}
		for(int i=1;i<=n;i++)
		{
			getchar();
			for(int j=1;j<=m;j++)
			{
				cin>>maze[i][j];
				if(maze[i][j]>='1'&&maze[i][j]<='9')
				{
					weight[i][j]=maze[i][j]-'0';
				}
			}
		}

		priority_queue<step> aqueue;
		aqueue.push(step(1,1,0,NULL));
		step *ans;
		while(!aqueue.empty())
		{
			step s;
			s=aqueue.top();
		
			if(weight[s.x][s.y]!=0)
			{
				step *s1 = new step;
				s1->x=s.x;
				s1->y=s.y;
				s1->last=s.last;
				s1->steps=s.steps;

				mark[s.x][s.y]=1;
				weight[s.x][s.y]--;
				aqueue.push(step(s.x,s.y,s.steps+1,s1));
				aqueue.pop();
				continue;
			}

			if(s.x==n&&s.y==m)
			{
				cout<<"It takes "<<s.steps<<" seconds to reach the target position, let me show you the way."<<endl;
				ans=&s;
				flag=1;              //找到路
				break;
			}

			if( maze[s.x][s.y+1]!='X' && mark[s.x][s.y+1]!=1 )
			{
				step *s1 = new step;
				s1->x=s.x;
				s1->y=s.y;
				s1->last=s.last;
				s1->steps=s.steps;

				mark[s.x][s.y+1]=1;
				aqueue.push(step(s.x,s.y+1,s.steps+1,s1));
			}

			if( maze[s.x+1][s.y]!='X' && mark[s.x+1][s.y]!=1 )
			{
				step *s1 = new step;
				s1->x=s.x;
				s1->y=s.y;
				s1->last=s.last;
				s1->steps=s.steps;

				mark[s.x+1][s.y]=1;
				aqueue.push(step(s.x+1,s.y,s.steps+1,s1));
			}

			if( maze[s.x][s.y-1]!='X' && mark[s.x][s.y-1]!=1 )
			{
				step *s1 = new step;
				s1->x=s.x;
				s1->y=s.y;
				s1->last=s.last;
				s1->steps=s.steps;

				mark[s.x][s.y-1]=1;
				aqueue.push(step(s.x,s.y-1,s.steps+1,s1));
			}

			if( maze[s.x-1][s.y]!='X' && mark[s.x-1][s.y]!=1 )
			{
				step *s1 = new step;
				s1->x=s.x;
				s1->y=s.y;
				s1->last=s.last;
				s1->steps=s.steps;

				mark[s.x-1][s.y]=1;
				aqueue.push(step(s.x-1,s.y,s.steps+1,s1));
			}
			aqueue.pop();
		}
		

		if(flag==1)
		{
			stack<step> astack;
			for(;ans!=NULL;ans=ans->last)
			{
				astack.push(*ans);
			}
			int times=1;
			while(!astack.empty())
			{
				step s;
				step s2;
				s=astack.top();
				astack.pop();

				if(!astack.empty())
				{
					cout<<times<<"s:";
					times++;
					s2=astack.top();
					if(s.x-1==s2.x-1&&s.y-1==s2.y-1)
						cout<<"FIGHT AT "<<"("<<s.x-1<<","<<s.y-1<<")"<<endl;
					else
						cout<<"("<<s.x-1<<","<<s.y-1<<")"<<"->"<<"("<<s2.x-1<<","<<s2.y-1<<")"<<endl;
				}
			}
			cout<<"FINISH"<<endl;
		}
		
		else
		{
			cout<<"God please help our poor hero."<<endl;
			cout<<"FINISH"<<endl;
		}

	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_26760433/article/details/86425227
今日推荐