杭电hdu 1026 Ignatius and the Princess I 广度优先搜索

http://acm.hdu.edu.cn/showproblem.php?pid=1026

这题是一个经典的广度优先算法,但是我刚开始做的时候用了深度优先策略,结果一直超时。嘿嘿。。。。。现在记录下

深度优先搜索:

//深度优先搜索
#include 

using namespace std;

#define MAX 101

typedef struct _node 
{
	int x;
	int y;
}node;

int n, m;

node road[MAX*MAX];
node roadtmp[MAX*MAX];
int k, t;

char map[MAX][MAX];

bool used[MAX][MAX];

int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};

int sum, sumtmp;

void dfs(node start)
{
	if(sum < sumtmp)return;
	if(start.x == n-1 && start.y == m-1){
		if(sum > sumtmp){
			sum = sumtmp;
			t = k;
			for(int i = 0; i < k; i ++){
				road[i].x = roadtmp[i].x;
				road[i].y = roadtmp[i].y;
			}
		}
		return;
	}
	for(int i = 0; i < 4; i ++){
		node tmp;
		tmp.x = start.x + dir[i][0];
		tmp.y = start.y + dir[i][1];
		if(tmp.x >= 0&&tmp.x < n&&tmp.y >= 0&&tmp.y < m){
			if(!used[tmp.x][tmp.y]&&map[tmp.x][tmp.y] != '#'){
				if(map[tmp.x][tmp.y] == '.'){
					used[tmp.x][tmp.y] = true;
					roadtmp[k].x = tmp.x;
					roadtmp[k].y = tmp.y;
					sumtmp += 1;
					k ++;
					dfs(tmp);
					k --;
					sumtmp -= 1;
					used[tmp.x][tmp.y] = false;
				}
				else if(map[tmp.x][tmp.y] >= '0' && map[tmp.x][tmp.y] <= '9'){
					used[tmp.x][tmp.y] = true;
					roadtmp[k].x = tmp.x;
					roadtmp[k].y = tmp.y;
					sumtmp += 1;
					sumtmp += map[tmp.x][tmp.y] - '0';
					k ++;
					dfs(tmp);
					k --;
					sumtmp -= map[tmp.x][tmp.y] - '0';
					sumtmp -= 1;
					used[tmp.x][tmp.y] = false;
				}
			}	
		}
	}
}

int main()
{
//	freopen("input.txt", "r", stdin);
	int i, j;
	while(scanf("%d%d", &n, &m)!=EOF){
//		scanf("%*c");
		memset(used, false, sizeof(used));
		k = 0;
		t = 0x7fffffff;
		sum = 0x7fffffff;
		sumtmp = 0;
		for(i = 0; i < n; i ++){
			for(j = 0; j < m; j ++){
				scanf(" %c", &map[i][j]);
			}
		}
		node st;
		st.x = st.y = 0;
		used[0][0] = true;
		dfs(st);
		
		if(t == 0x7fffffff)printf("God please help our poor hero.\n");
		else {
			printf("It takes %d seconds to reach the target position, let me show you the way.\n", sum);
			int l = 1;
			for(i = 0; i < t; i ++){
				if(i != 0){
					printf("%ds:(%d,%d)->(%d,%d)\n", l ++, road[i-1].x, road[i-1].y, road[i].x, road[i].y);
					if(map[road[i].x][road[i].y] >= '0' && map[road[i].x][road[i].y] <= '9'){
						int r = map[road[i].x][road[i].y]-'0';
						while(r--){
							printf("%ds:FIGHT AT (%d,%d)\n", l ++, road[i].x, road[i].y);
						}
					}
				}
				else{
					printf("%ds:(0,0)->(%d,%d)\n", l ++, road[i].x, road[i].y);
					if(map[road[i].x][road[i].y] >= '0' && map[road[i].x][road[i].y] <= '9'){
						int r = map[road[i].x][road[i].y]-'0';
						while(r--){
							printf("%ds:FIGHT AT (%d,%d)\n", l ++, road[i].x, road[i].y);
						}
					}
				}
			}
		}
		printf("FINISH\n");
	}
	return 0;
}
广度优先算法加优先队列加堆栈:

//广度优先搜索
#include 

using namespace std;

#define MAX 101

#include 
#include 

typedef struct _node
{
	int x;
	int y;
	int x_next;
	int y_next;
	int seconds;
	friend bool operator<(_node a, _node b){return a.seconds > b.seconds;}
}node;

node path[MAX][MAX];

char map[MAX][MAX];

bool used[MAX][MAX];

int n, m;

priority_queue q;
node end;
stack S;

int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};

int bfs()
{
	int i;
	node tmp, no_pre;
	while(!q.empty()){
		no_pre = q.top();
		if(no_pre.x == n-1 && no_pre.y == m-1){
			end.x = no_pre.x;
			end.y = no_pre.y;
			end.x_next = 0;
			end.y_next = 0;
			return no_pre.seconds;
		}
		q.pop();
		for(i = 0; i < 4; i ++){
			tmp.x = no_pre.x + dir[i][0];
			tmp.y = no_pre.y + dir[i][1];
			if(tmp.x >= 0 && tmp.x < n && tmp.y >= 0 && tmp.y < m &&!used[tmp.x][tmp.y] && map[tmp.x][tmp.y] != 'X'){
				if(map[tmp.x][tmp.y] == '.'){
					tmp.seconds = no_pre.seconds + 1;
				}
				else if(map[tmp.x][tmp.y] >= '0' && map[tmp.x][tmp.y] <= '9'){
					tmp.seconds = no_pre.seconds + 1 + (int)(map[tmp.x][tmp.y] - '0');
				}
				used[tmp.x][tmp.y] = true;
				q.push(tmp);
				no_pre.x_next = tmp.x;
				no_pre.y_next = tmp.y;
				path[tmp.x][tmp.y] = no_pre;
			}//if
		}//for
	}//while
	return -1;
}

int main()
{
//	freopen("input.txt", "r", stdin);
	int i, j;
	while(scanf("%d%d", &n, &m)!=EOF){
		for(i = 0; i < n; i ++){
			for(j = 0; j < m; j ++){
				scanf(" %c", &map[i][j]);
			}
		}
		memset(used, false, sizeof(used));
		while(!q.empty())q.pop();
		node no_start;
		no_start.x = no_start.y = no_start.seconds = 0;
		used[0][0] = true;
		q.push(no_start);
		int flag = bfs();
		if(flag == -1)printf("God please help our poor hero.\n");
		else {
			printf("It takes %d seconds to reach the target position, let me show you the way.\n", flag);
			S.push(end);
			node temp = path[end.x][end.y];
			while(temp.x||temp.y){
				S.push(temp);
				temp = path[temp.x][temp.y];
			}
			S.push(temp);
			int num = 1;
			while(!S.empty()){
				temp = S.top();
				S.pop();
				if(map[temp.x][temp.y] == '.' && !(temp.x_next == 0 && temp.y_next == 0)){
					printf("%ds:(%d,%d)->(%d,%d)\n", num ++, temp.x, temp.y, temp.x_next, temp.y_next);
				}
				else if(map[temp.x][temp.y] >= '0' && map[temp.x][temp.y] <= '9'){
					int t = map[temp.x][temp.y] - '0';
					while(t --){
						printf("%ds:FIGHT AT (%d,%d)\n", num ++, temp.x, temp.y);
					}
					if(!(temp.x_next == 0 && temp.y_next == 0)){
						printf("%ds:(%d,%d)->(%d,%d)\n", num ++, temp.x, temp.y, temp.x_next, temp.y_next);
					}
				}
			}
		}
		printf("FINISH\n");
	}
	return 0;
}

你可能感兴趣的:(广度优先搜索)