HDU2612-Find a way

题目链接:HDU2612

这题目有个坑点很类似前面那个fire(UVA11624)。

先说思路,我一开始记录下每个KFC的位置,然后对每个KFC,跑Y和M的bfs 求最小时间 然后T了

我看了看网上的题解,正解是用Y和M 跑整个图,记录下跑每个点的时间。然后对于每个KFC直接算时间就好了

但是这要考虑一个特殊情况,特判,当前KFC两者必须都能够走到,(WA了好多次)

ACcode:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn=220;
bool vis[maxn][maxn];
char mp[maxn][maxn];
int n,m,sx,sy,ex,ey,tot;
int dis[3][maxn][maxn];
struct dot{
int x,y;
}kfc[maxn];

struct node{
    int x,y;
    int step;
};


int bfs(int x,int y,int id){
    queue<node> q;
    memset(vis,false,sizeof(vis));
    node t1,t2,t3;
    t1.x=x;
    t1.y=y;
    t1.step=0;
    dis[id][x][y]=0;
    q.push(t1);
    vis[x][y]=1;
    while(!q.empty()){
        t2=q.front();
        q.pop();
        int cx=t2.x;
        int cy=t2.y;

        if(cx+1<n&&!vis[cx+1][cy]&&mp[cx+1][cy]!='#'){
            t3.x=cx+1;
            t3.y=cy;
            t3.step=t2.step+1;
            vis[cx+1][cy]=1;
            dis[id][t3.x][t3.y]=t3.step;
            q.push(t3);
        }
        if(cx-1>=0&&!vis[cx-1][cy]&&mp[cx-1][cy]!='#'){
            t3.x=cx-1;
            t3.y=cy;
            t3.step=t2.step+1;
            vis[cx-1][cy]=1;
            dis[id][t3.x][t3.y]=t3.step;
            q.push(t3);
        }
        if(cy+1<m&&!vis[cx][cy+1]&&mp[cx][cy+1]!='#'){
            t3.x=cx;
            t3.y=cy+1;
            t3.step=t2.step+1;
            vis[cx][cy+1]=1;
            dis[id][t3.x][t3.y]=t3.step;
            q.push(t3);
        }
        if(cy-1>=0&&!vis[cx][cy-1]&&mp[cx][cy-1]!='#'){
            t3.x=cx;
            t3.y=cy-1;
            t3.step=t2.step+1;
            vis[cx][cy-1]=1;
            dis[id][t3.x][t3.y]=t3.step;
            q.push(t3);
        }
    }
    return 0;
}

int main(){
   // freopen("n.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF){
        tot=0;
        memset(dis,0,sizeof(dis));
        for(int i=0;i<n;i++){
            scanf("%s",mp[i]);
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(mp[i][j]=='Y'){
                    sx=i;
                    sy=j;
                }else if(mp[i][j]=='M'){
                    ex=i;
                    ey=j;
                }else if(mp[i][j]=='@'){
                    kfc[tot].x=i;
                    kfc[tot].y=j;
                    tot++;
                }
            }
        }
        /*check */
        /*
        printf("(%d,%d)\n",sx,sy);
        printf("(%d,%d)\n",ex,ey);
        for(int i=0;i<tot;i++){
            printf("%d:(%d,%d)\n",i,kfc[i].x,kfc[i].y);
        }
      //  */

        bfs(sx,sy,0);
        bfs(ex,ey,1);
        /*
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                printf("(%d,%d):%d\n",i,j,dis[0][i][j]);
                printf("(%d,%d):%d\n",i,j,dis[1][i][j]);
            }
        }
        */
        int ans=999999;
       // printf("tot=%d\n",tot);
        for(int i=0;i<tot;i++){
            int kfcx=kfc[i].x;
            int kfcy=kfc[i].y;
           // printf("kfcx=%d kfcy=%d\n",kfcx,kfcy);
            //printf("(%d,%d):%d\n",kfcx,kfcy,dis[0][kfcx][kfcy]);
         //   printf("(%d,%d):%d\n",kfcx,kfcy,dis[1][kfcx][kfcy]);
            int t1=dis[0][kfcx][kfcy];
            int t2=dis[1][kfcx][kfcy];
           // printf("t1=%d t2=%d\n",t1,t2);
            if(t1+t2<ans&&t1!=0&&t2!=0) ans=t1+t2;
            //这句话WA因为 可能有一方到不了KFC,所以要多加一个判断
        }
        printf("%d\n",ans*11);

    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值