zoukankan      html  css  js  c++  java
  • NYOJ--1100--WAJUEJI which home strong!

    /*
        Name: NYOJ--1100--WAJUEJI which home strong!
        Date: 15/04/17 21:02
        Description: bfs+优先队列 
    */
    #include<cstring>
    #include<iostream> 
    #include<queue>
    using namespace std;
    struct node{
        int x,y,lev;
        node():lev(0){
        };
        bool operator < (const node &a) const{
            return lev>a.lev;
        }
    };
    int bfs();
    node s,e;
    char map[110][110];
    int vis[110][110];
    int T,n,m; 
    int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    int main()
    {
    //    freopen("in.txt","r",stdin);
        cin>>T;
        while(T--){
            int i,j;
            cin>>n>>m;
            memset(map,0,sizeof(map));
            memset(vis,0,sizeof(vis));
            for(i=0; i<n; ++i){
                cin>>map[i];
                for(j=0; j<m; ++j){
                    if(map[i][j] == 's')s.x=i,s.y=j;
                    if(map[i][j] == 'l')e.x=i,e.y=j;
                }
            }
            cout<<bfs()<<endl;
        }
        return 0;
    }
    int bfs(){
        priority_queue<node> q;
        q.push(s);
        vis[s.x][s.y] = 1;
        while(!q.empty()){
            node temp,a = temp = q.top();q.pop();
            if(a.x == e.x && a.y == e.y)return a.lev;
            for(int i=0; i<4; ++i){
                a.x = temp.x + dir[i][0];// a.x += dir[i][0] WA了 
                a.y = temp.y + dir[i][1];
                if(a.x<0||a.y<0||a.x>=n||a.y>=m||map[a.x][a.y]=='#'||vis[a.x][a.y])continue;
                if(a.x == e.x && a.y == e.y){
    //                cout<<"-----------1----"<<a.lev<<endl ;
                    a.lev = temp.lev;//WA无数次, 
    //                cout<<"-----------2----"<<a.lev<<endl ;前后a.lev的值没有改变 
                } 
                else a.lev = map[a.x][a.y] -'A' + 1 + temp.lev;
                q.push(a);
                vis[a.x][a.y] = 1;
            }
        }
        return -1;
    }
  • 相关阅读:
    JavaScript事件详解
    JavaScript事件
    十六进制转十进制原理
    java:数组复制
    java:数组扩容
    MySQL---Day2
    Pyhton学习——Day47
    MySQL---Day1
    Pyhton学习——Day46
    Someing-About-Work
  • 原文地址:https://www.cnblogs.com/slothrbk/p/7251881.html
Copyright © 2011-2022 走看看