HDU 1026 (BFS+记录路径)

BFS + 路径记录:

/* 
 *problem ID: HDU 1026
 * Author ID: fuqiang11 
 *      TIME: 2013-07-17 
 * Algorithm: BFS  
 *    Status: (Accept) 
*/  

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 103

char map[maxn][maxn];
int N,M;
int value[maxn][maxn];  //记录步数
struct point
{
	int x;
	int y;
}st,ed,a,pre[maxn][maxn];  //pre 记录前一个点
int xx[] = {1,-1,0,0};
int yy[] = {0,0,1,-1};

void bfs(int x,int y)
{
	memset(value,0x3f,sizeof(value));
	value[0][0] = 0;
	queue <point> q;
	st.x = x;
	st.y = y;
	q.push(st);
	while(!q.empty())
	{
		a = q.front();
		q.pop();
		for(int i = 0; i < 4; i++)
		{
			ed.x = a.x + xx[i];
			ed.y = a.y + yy[i];
			if(ed.x<0||ed.x>N-1||ed.y<0||ed.y>M-1||map[ed.x][ed.y]=='X')
				continue;
			if(map[ed.x][ed.y]=='.')
			{
				if(value[a.x][a.y] + 1 < value[ed.x][ed.y])
				{
					value[ed.x][ed.y] = value[a.x][a.y] + 1;
					pre[ed.x][ed.y].x = a.x;
					pre[ed.x][ed.y].y = a.y;
					q.push(ed);
				}	
			}
			else
			{
				if(value[a.x][a.y] + map[ed.x][ed.y] - '0' + 1 < value[ed.x][ed.y])
				{
					value[ed.x][ed.y] = value[a.x][a.y] + map[ed.x][ed.y] - '0' + 1;
					pre[ed.x][ed.y].x = a.x;
					pre[ed.x][ed.y].y = a.y;
					q.push(ed);
				}
			}
		}
	}
}

void prin_pre(int i, int j)  //输出路径
{
    if(pre[i][j].x == -1)
        return ;
    prin_pre(pre[i][j].x, pre[i][j].y);
    if(map[i][j]=='.')
        printf("%ds:(%d,%d)->(%d,%d)\n",value[i][j],pre[i][j].x,pre[i][j].y,i,j);
    else
    {
        int num = map[i][j]-'0';
        int t = value[i][j] - num;
        printf("%ds:(%d,%d)->(%d,%d)\n",t++,pre[i][j].x,pre[i][j].y,i,j);
        for(int k = 1; k <= num; k++)
        {
            printf("%ds:FIGHT AT (%d,%d)\n",t++,i,j);
        }
    }
}
int main()
{
	while(scanf("%d%d",&N,&M)!=EOF)
	{
		getchar();
		for(int i = 0; i < N; i++)
			gets(map[i]);
		pre[0][0].x = pre[0][0].y = -1;
		bfs(0,0);
		if(value[N-1][M-1]!=INF)
		{
			printf("It takes %d seconds to reach the target position, let me show you the way.\n",value[N-1][M-1]);
			prin_pre(N-1,M-1);
		}
		else
		{
			printf("God please help our poor hero.\n");
		}
		puts("FINISH");
	}
}


DFS超时(明显的,N、M <= 100,我居然还这样做的!)

如果N.M较小的话,还是能承受的,比如10、20、30

超时的代码先挂这吧,以示警醒;

/*
 *problem ID: HDU 1026
 * Author ID: fuqiang11
 *      TIME: 2013-07-16
 * Algorithm: DFS 
 *    Status: (RunTimeError)
*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#define INF 0x3f3f3f3f
using namespace std;
#define maxn 103

char map[maxn][maxn];
int v[maxn][maxn];
struct Point
{
	int x;
	int y;
}pre[maxn][maxn];


int N,M;
int xplus[] = {1,-1,0,0};
int yplus[] = {0,0,1,-1};

void dfs(int x,int y)
{
	if(x==N-1 && y==M-1)
		return ;
	int xx,yy;
	for(int i = 0; i < 4; i++)
	{
		xx = x + xplus[i];
		yy = y + yplus[i];
		if(xx<0 || xx>N-1 || yy<0 || yy>M-1)
			continue;
		if(map[xx][yy]=='.')
		{
			if(v[x][y] + 1 < v[xx][yy])
			{
				v[xx][yy] = v[x][y] + 1;
				pre[xx][yy].x = x;
				pre[xx][yy].y = y;
				dfs(xx,yy);
			}
		}
		if(map[xx][yy]>='1' && map[xx][yy]<='9')
		{
			if(v[x][y] + 1 + map[xx][yy] - '0' < v[xx][yy])
			{
				v[xx][yy] = v[x][y] + 1 + map[xx][yy] - '0';
				pre[xx][yy].x = x;
				pre[xx][yy].y = y; 
				dfs(xx,yy);
			}
		}
	}
}
void prin_pre(int i, int j)  //输出路径
{
	if(pre[i][j].x == -1)
		return ;
	prin_pre(pre[i][j].x, pre[i][j].y);
	if(map[i][j]=='.')
		printf("%ds:(%d,%d)->(%d,%d)\n",v[i][j],pre[i][j].x,pre[i][j].y,i,j);
	else
	{
		int num = map[i][j]-'0';
		int t = v[i][j] - num;
		printf("%ds:(%d,%d)->(%d,%d)\n",t++,pre[i][j].x,pre[i][j].y,i,j);
		for(int k = 1; k <= num; k++)
		{
			printf("%ds:FIGHT AT (%d,%d)\n",t++,i,j);
		}
	}
}

int main()
{
	while(scanf("%d%d",&N,&M)!=EOF)
	{
		cin.get();
		for(int i = 0; i < N; i++)
			gets(map[i]);
		memset(v,0x3f,sizeof(v));
		v[0][0] = 0;
		pre[0][0].x = pre[0][0].y = -1;
		dfs(0,0);
		if(v[N-1][M-1] != INF)
		{
			printf("It takes %d seconds to reach the target position, let me show you the way.\n",v[N-1][M-1]);
			prin_pre(N-1,M-1);
		}
		else
		{
			printf("God please help our poor hero.\n");
		}
		puts("FINISH");
	}
}


你可能感兴趣的:(HDU 1026 (BFS+记录路径))