团体程序设计天梯赛(L3-018 森森美图 (30 分))

题目:

思路分析:

一个bfs搜索的题目:看代码就行! 

代码实现:

/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */

const int MAX=10001;
struct node{
    int x,y;
    double dis;
};
bool operator!=(node a,node b){
    return a.x!=b.x||a.y!=b.y;
}
bool operator==(node a,node b){
    return a.x==b.x&&a.y==b.y;
}
int n,m;
int a[MAX][MAX];
double f[MAX][MAX];
double ans=0;
int flag;
node st;
node en;
int cross(node a,node b,node p){//三角形行列式公式,判断三点是否在一个直线上
    return (b.x-a.x)*(p.y-a.y)-(p.x-a.x)*(b.y-a.y);
}
int check(node x){
    if(x.x<1||x.x>n||x.y>m||x.y<1){
        return 0;
    }
    if(flag&&x!=st&&x!=en&&cross(st,en,x)<=0) return 0;
    if(!flag&&x!=st&&x!=en&&cross(st,en,x)>=0) return 0;
    if(x.dis>=f[x.x][x.y]) return 0;
    return 1;
}
void bfs(){
    queue<node>q;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)
            f[i][j]=INF;
    }
    if(check(st)){
        q.push(st);
        f[st.x][st.y]=st.dis;
    }
    while (!q.empty()) {
        node now=q.front();
        q.pop();
        node ne;
        for(int i=0;i<8;i++){
            ne.x=now.x+xd[i];
            ne.y=now.y+yd[i];
            if(i<4){
                ne.dis=f[now.x][now.y]+a[ne.x][ne.y];
            }
            else {
                ne.dis=f[now.x][now.y]+a[ne.x][ne.y]+(a[ne.x][ne.y]+a[now.x][now.y])*(sqrt(2)-1);
            }
            if(check(ne)){
                f[ne.x][ne.y]=ne.dis;
                q.push(ne);
            }
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    cin>>st.y>>st.x>>en.y>>en.x;
    st.y++;
    st.x++;
    en.x++;
    en.y++;
    st.dis=a[st.x][st.y];
    flag=1;
    bfs();
    ans+=f[en.x][en.y];
    flag=0;
    bfs();
    ans+=f[en.x][en.y];
    ans-=a[st.x][st.y]+a[en.x][en.y];
    printf("%.2f",ans);
}

猜你喜欢

转载自blog.csdn.net/m0_57006708/article/details/121306212