hdu 1026 哎,又做了这么久 bfs

  有点烦的题,思路,怎么说呢,好似不是难,就是那个记录路径烦,以前也做过,不过应该大有不同吧

这代码很乱,现在我自己也不愿去分析了,其实我应该再写义多一个结构体去,记录每步情况,把初始结点情况与其分开,以免过程中改变了

初始的情况,而这里是明显不应改变的

       有一点值得注意的就是,<的重载,其实自对优先级队列不熟,所以一开始重载错了,如没const和形参用的引用之类的,以后还是先自己写比较函吧

等到偶看了stl再搞起哈

#include<iostream>
#include<algorithm>
#include<queue>
#define N 101
#define M 101
using namespace std;

struct node{
	int m_t;
	int x,y;
	int pos_x;
	int pos_y;
	int cost;
	bool operator<( node t) const {
		return m_t > t.m_t;
	}
};


char map[N][M];
node m_time[N][M];
int n,m;
int d[4][2] = {{1,0},{0,1},{0,-1},{-1,0}};

bool in(int& x,int& y){
	return x >= 0 && x < n && y >= 0 && y < m;
}

bool X(int& x,int& y){
	return map[x][y] == 'X';
}

void input(){
	int i,j;
	for(i = 0;i < n;i++){
		for(j = 0;j < m;j++){
		    cin >>map[i][j];
			m_time[i][j].m_t = 0;
			if(map[i][j] == '.')
				m_time[i][j].cost = 1;
			else if(map[i][j] == 'X')
				m_time[i][j].cost = 0;
			else
				m_time[i][j].cost = map[i][j]-'0'+1;
		}
	}
}

bool bfs(){
	priority_queue<node> q;
	node now;
	now.m_t = 0; now.x = 0; now.y = 0; now.pos_x = -1; now.pos_y = -1; now.cost = 1;
	map[0][0] = 'X';
	m_time[0][0] = now;
	q.push(now);
	while(!q.empty()){
		node t = q.top();//printf("(%d,%d,(%d))\n",t.x,t.y,t.m_t);//cout <<m_time[0][0].cost <<endl;
		q.pop();
		for(int i = 0; i < 4;i++){
			node next;
			next.pos_x = t.x;
			next.pos_y = t.y;
			next.x = t.x+d[i][0];
			next.y = t.y+d[i][1];
			next.cost = m_time[next.x][next.y].cost;
			if(in(next.x,next.y) && !X(next.x,next.y)){
				next.m_t = t.m_t + m_time[next.x][next.y].cost;
				m_time[next.x][next.y] = next;
				//cout << t.m_t << m_time[next.x][next.y].cost << m_time[next.x][next.y].m_t <<endl;
				q.push(next);
				if(next.x == n-1 && next.y == m-1){
					//m_time[n-1][m-1] = next;
					//cout << next.m_t <<endl;
					return true;
				}
				map[next.x][next.y] = 'X';
			}
		}
	}
	return false;
}

void print1(){
	cout <<"It takes " <<m_time[n-1][m-1].m_t <<" seconds to reach the target position, let me show you the way." <<endl;
	int x,y;
	x = n-1; y = m-1;
	while(x != 0 || y != 0){
		int now_x = m_time[x][y].pos_x;
		int now_y = m_time[x][y].pos_y;
		int _x = x;
		int _y = y;
		m_time[now_x][now_y].x = _x;
		m_time[now_x][now_y].y = _y;
		x = now_x; y = now_y;
	}
	int step = 0;
	while(true){//cout <<m_time[n-1][m-1].cost<<endl;
		if(m_time[x][y].cost == 1){
			cout <<++step <<"s:" <<"(" <<x <<"," <<y <<")->("
			     <<m_time[x][y].x <<"," <<m_time[x][y].y <<")" <<endl;
			int temp = x;
			x = m_time[x][y].x;
			y = m_time[temp][y].y;
		}
		while(m_time[x][y].cost > 1){
			cout <<++step <<"s:" <<"FIGHT AT (" <<x <<"," <<y <<")" <<endl;
			m_time[x][y].cost--;
		}
		if(x == n-1 && y == m-1){
		    cout <<"FINISH" <<endl;
			return ;
		}
	}
}

void print2(){
	cout <<"God please help our poor hero." <<endl <<"FINISH" <<endl;
}

int main()
{
	while(cin >>n >>m){
		input();
		if(bfs())
			print1();
		else print2();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值