zoukankan      html  css  js  c++  java
  • 小乐乐打游戏

    题目描述

            小乐乐觉得学习太简单了,剩下那么多的时间好无聊,于是便想打游戏。
            最近新出了一个特别火的游戏,叫吃猪,小乐乐准备玩一玩。
            吃猪游戏很简单,给定一个地图,大小为n*m,在地图中会随机出现一个火山口,只要小乐乐能逃离这个地图,他便能吃猪! 
            但吃鸡远没有那么简单:
            1.小乐乐每走一次只能上下左右四个方向中走一步。
            2.小乐乐每走一步,火山喷发的岩浆就会向四周蔓延一个格子,所有岩浆走过的地方都视为被岩浆覆盖。
            3.小乐乐碰到岩浆就会死。
            4.地图中还有很多障碍,使得小乐乐不能到达,但是岩浆却可以把障碍融化。
            5.小乐乐只有走到题目给定的终点才算游戏胜利,才能吃猪。
            小乐乐哪见过这场面,当场就蒙了,就想请帮帮他,告诉他是否能吃猪。

    输入描述:

    多组样例输入

    第一行给定n,m,(1 <= n, m <= 1000)代表地图的大小。

    接下来n行,每一行m个字符,代表地图,对于每一个字符,如果是'.',代表是平地,'S'代表小乐乐起始的位置,
    'E'代表终点,'#'代表障碍物,'F'代表火山口。

    输出描述:

    输出只有一行。如果小乐乐能吃猪,输出"PIG PIG PIG!"。否则输出"A! WO SI LA!"。
    示例1

    输入

    3 3
    F..
    #S#
    #.E

    输出

    PIG PIG PIG!

    题意

      中文题意不做解释

    分析

      典型BFS没啥好说的

    ///  author:Kissheart  ///
    #include<stdio.h>
    #include<algorithm>
    #include<iostream>
    #include<string.h>
    #include<vector>
    #include<stdlib.h>
    #include<math.h>
    #include<queue>
    #include<deque>
    #include<ctype.h>
    #include<map>
    #include<set>
    #include<stack>
    #include<string>
    #define INF 0x3f3f3f3f
    #define FAST_IO ios::sync_with_stdio(false)
    const double PI = acos(-1.0);
    const double eps = 1e-6;
    const int MAX=1e5+10;
    const int mod=1e9+7;
    typedef long long ll;
    using namespace std;
    #define gcd(a,b) __gcd(a,b)
    inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
    inline ll inv1(ll b){return qpow(b,mod-2);}
    inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
    inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
    //freopen( "in.txt" , "r" , stdin );
    //freopen( "data.txt" , "w" , stdout );
    int n,m,sx,sy,ex,ey;
    int hx,hy;
    char mp[1005][1005];
    int vis[1005][1005];
    int dir[4][2]={1,0,-1,0,0,1,0,-1};
    struct node
    {
        int x,y;
        int setp;
    };
    int BFS()
    {
        queue<node>q;
        while(!q.empty()) q.pop();
        memset(vis,0,sizeof(vis));
    
        node p;
        p.x=sx;
        p.y=sy;
        p.setp=0;
        vis[sx][sy]=1;
        q.push(p);
    
    
        while(!q.empty())
        {
            p=q.front();
            q.pop();
    
            if(p.x==ex && p.y==ey)
                return 1;
            for(int i=0;i<4;i++)
            {
                int fx=p.x+dir[i][0];
                int fy=p.y+dir[i][1];
                if(fx>=1 && fy>=1 && fx<=n && fy<=m && mp[fx][fy]!='#' && !vis[fx][fy] && abs(hx-fx)+abs(hy-fy)>p.setp)
                {
                    vis[fx][fy]=1;
                    node no;
                    no.x=fx;
                    no.y=fy;
                    no.setp=p.setp+1;
                    q.push(no);
                }
            }
        }
        return 0;
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            for(int i=1;i<=n;i++)
                scanf("%s",mp[i]+1);
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(mp[i][j]=='S')
                    {
                        sx=i;
                        sy=j;
                    }
                    else if(mp[i][j]=='E')
                    {
                        ex=i;
                        ey=j;
                    }
                    else if(mp[i][j]=='F')
                    {
                        hx=i;
                        hy=j;
                    }
                }
            }
            //printf("%d %d
    ",sx,sy);
            //printf("%d %d
    ",ex,ey);
            //printf("%d %d
    ",hx,hy);
    
            if(BFS())
                printf("PIG PIG PIG!
    ");
            else
                printf("A! WO SI LA!
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    第十二周工作总结
    第八周工作总结
    冲刺2
    冲刺1
    用户场景分析
    用户场景分析
    水王在哪
    课堂练习-4个数的和
    《大道至简》第一章读后感
    ELF Format 笔记(三)—— Section Types
  • 原文地址:https://www.cnblogs.com/Kissheart/p/10063196.html
Copyright © 2011-2022 走看看