HDU-1026Ignatius and the Princess I

#include<iostream>
#include<queue>
#include<algorithm>
#include<fstream>
#include<stdio.h>
#define N 105
#define M 1000
struct  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));
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值