zoukankan      html  css  js  c++  java
  • 4980

    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int tu[300][300];
    bool vis[300][300];
    const int nil=(1<<29);
    struct my{
      int x;
      int y;
      int dirt;
       bool operator<(const my &a)const{return dirt>a.dirt;}
    };
    int m,n;
    //@==1
    //#==2
    //x==3
    int dir[5][5]={{0,0},{0,1},{0,-1},{1,0},{-1,0}};
    int sx,sy,ex,ey;
    int ans[300][300];
    priority_queue<my>Q;
    bool bfs(){
        while(!Q.empty()) Q.pop();
         my u;
         u.dirt=0;
         u.x=sx;
         u.y=sy;
         Q.push(u);
         while(!Q.empty()){
            my v=Q.top();
            Q.pop();
            for (int i=1;i<=4;i++){
                u.dirt=v.dirt+1;
                int dx=v.x+dir[i][0];
                int dy=v.y+dir[i][1];
                if((tu[dx][dy]!=3&&dx<=m&&dy<=n&&dx>0&&dy>0)){
                       // printf("%d %d %d ",dx,dy,ans[ey][ex]);
                    u.x=dx;
                    u.y=dy;
                    if(tu[dx][dy]==1) {
                        u.dirt;
                        Q.push(u);
                    }
                    if(tu[dx][dy]==2){
                        ++u.dirt;
                        Q.push(u);
                    }
                    if(dx==ex&&dy==ey) {
                            printf("%d ",u.dirt);
                    return true;
                   }
               tu[dx][dy]=3;
                }
            }
         }
         return false;
    }
    int main(){
        int t;
        char c;
        scanf("%d",&t);
        while(t--){
            memset(vis,0,sizeof(vis));
            scanf("%d%d",&m,&n);
            scanf("%c",&c);
            for (int i=1;i<=m;i++){
                for (int j=1;j<=n;j++){
                    scanf("%c",&c);
                    if(c=='@') tu[i][j]=1;
                    if(c=='x') tu[i][j]=2;
                    if(c=='#') tu[i][j]=3;
                    if(c=='a') {
                            sx=i,sy=j;
                            tu[i][j]=3;
                            }
                    if(c=='r') {
                            ex=i,ey=j;
                            tu[i][j]=1;
                    }
                }
                scanf("%c",&c);
            }
            tu[sx][sy]=3;
            if(!bfs())
             printf("Impossible ");
        }
    return 0;
    }

  • 相关阅读:
    [国嵌攻略][152][I2C总线介绍]
    [国嵌攻略][151][nandflash驱动程序设计]
    [国嵌攻略][150][实际嵌入式系统环境搭建]
    [国嵌攻略][149][Yaffs2文件系统应用]
    [国嵌攻略][148][MTD系统架构]
    [国嵌攻略][147][简单块设备驱动设计]
    [国嵌攻略][146][块设备驱动实例分析]
    PhpStorm Git 配置
    PHP 创建重用数据库连接函数 mysqli与PDO
    【转载】php程序员:从1.5K到18K 一个程序员的5年成长之路
  • 原文地址:https://www.cnblogs.com/lmjer/p/7852971.html
Copyright © 2011-2022 走看看