hdu 1026 BFS

输出这么蛋疼的一道题我竟然一遍就过了!!!!!!!爽啊!!!!!!!!!!!!!!!!!

AC代码如下:

#include <iostream> 
#include <queue>
#include <cstring>
#include <cstdlib>
using namespace std;

typedef struct{
	int x, y;
	int time;
}Point;

int map[100][100];
int mark[100][100];
int N, M;
int moves[][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
Point front[100][100];

void myprint( int i, int j, int k, int t ){
	if( i != 0 || j != 0 ){
		myprint( front[i][j].x, front[i][j].y, i, j );
	}
	int mm = front[k][t].time + 1;
	printf( "%ds:(%d,%d)->(%d,%d)\n", mm, i, j, k, t );
	for( int i = 1; i <= map[k][t]; i++ ){
		printf( "%ds:FIGHT AT (%d,%d)\n", mm+i, k, t );
	}
}

void BFS(){
	queue<Point> q;
	Point begins;
	begins.x = begins.y = begins.time = 0;
	mark[0][0] = 0;
	q.push( begins );
	while( !q.empty() ){
		Point p;
		p = q.front();
		q.pop();
		for( int i = 0; i < 4; i++ ){
			Point temp = p;
			temp.x += moves[i][0];
			temp.y += moves[i][1];
			if( temp.x >=0 && temp.y >= 0 && temp.x < N && temp.y < M && map[temp.x][temp.y] != -1 ){
				temp.time += map[temp.x][temp.y] + 1;
				if( temp.time < mark[temp.x][temp.y] ){
					mark[temp.x][temp.y] = temp.time;
					front[temp.x][temp.y].x = p.x;
					front[temp.x][temp.y].y = p.y;
					front[temp.x][temp.y].time = p.time;
					q.push( temp );
				}
			}
		}
	}
	if( mark[N-1][M-1] < 999999 ){
		cout << "It takes " << mark[N-1][M-1] <<" seconds to reach the target position, let me show you the way." << endl;
		myprint( front[N-1][M-1].x, front[N-1][M-1].y, N-1, M-1 );
		cout << "FINISH" << endl;
	}else{
		cout << "God please help our poor hero.\nFINISH" << endl;
	}
}

int main(){
	char c;
	while( scanf( "%d%d", &N, &M ) != EOF ){
		memset( map, 0, sizeof( map ) );
		getchar();
		for( int i = 0; i < N; i++ ){
			for( int j = 0; j < M; j++ ){
				c = getchar();
				if( c == 'X' ){
					map[i][j] = -1;
				}else if( c >= 49 && c <= 57 ){
					map[i][j] = c - 48;
				}
				mark[i][j] = 999999;
			}
			getchar();
		}
		BFS();
	}	
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值