杭电1026

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026

 

提交了几遍,用DFS超时,又该用BFS,但是内存又超。

交了几遍还是不知怎么解决,后来才知道,需要一个vis数组来表示已经入队的点,解决重复入队的问题。

代码如下:

 

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define Max 0xfffffff
#include<queue>

struct node{
    int x,y;
}o;
using namespace std;

int map[110][110],dis[110][110],vis[110][110];
int N,M;
node front[110][110];
int m_x[]={-1,0,1,0},m_y[]={0,1,0,-1};

void bfs(){
    for(int i=0;i<110;i++)
        for(int j=0;j<110;j++)
            dis[i][j]=Max;
    dis[0][0]=0;
    queue<node> q;
    o.x=0;o.y=0;
    q.push(o);
    vis[0][0]=1;
    while(!q.empty())
    {
        node tmp;
        tmp=q.front();
        q.pop();
        vis[tmp.x][tmp.y]=0;
        for(int i=0;i<4;i++)
                {
                    int tx=tmp.x+m_x[i],ty=tmp.y+m_y[i];
                    if(tx>=0 && tx<N && ty>=0 && ty<M)
                        if(map[tx][ty]!=-1 && (dis[tmp.x][tmp.y]+map[tx][ty])<dis[tx][ty])
                        {
                            dis[tx][ty]=dis[tmp.x][tmp.y]+map[tx][ty]+1;//cout<<tx<<" "<<ty<<" "<<dis[tx][ty]<<endl;
                            front[tx][ty].x=tmp.x;
                            front[tx][ty].y=tmp.y;
                            o.x=tx;o.y=ty;
                            if(!vis[tx][ty]){
                                q.push(o);
                                vis[tx][ty]=1;
                            }
                        }
                }
    }
}


void disp(int n,int m,int t){
    if(front[n][m].x+front[n][m].y==0)
    {
        printf("%ds:(%d,%d)->(%d,%d)\n",1,front[n][m].x,front[n][m].y,n,m);
        for(int i=0;i<map[n][m];i++)
        {
            printf("%ds:FIGHT AT (%d,%d)\n",i+2,n,m);
        }
        return;
    }
    else
    {
        disp(front[n][m].x,front[n][m].y,t-map[n][m]-1);
        printf("%ds:(%d,%d)->(%d,%d)\n",t-map[n][m],front[n][m].x,front[n][m].y,n,m);
        for(int i=0;i<map[n][m];i++)
            printf("%ds:FIGHT AT (%d,%d)\n",t+i+1-map[n][m],n,m);
    }
}
int main(){
    while(cin>>N)
    {
        cin>>M;
        char ch;getchar();
        memset(vis,0,sizeof(vis));
        for(int i=0;i<N;i++){
            for(int j=0;j<M;j++)
            {
                cin>>ch;
                if(ch=='X') map[i][j]=-1;
                else if(ch=='.') map[i][j]=0;
                else map[i][j]=ch-'0';
            }
            getchar();
        }
        bfs();
        if(dis[N-1][M-1]==Max) 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",dis[N-1][M-1]);
            disp(N-1,M-1,dis[N-1][M-1]);
        }
        cout<<"FINISH\n";
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值