1. 程式人生 > >小樂樂打遊戲

小樂樂打遊戲

 

題目描述

        小樂樂覺得學習太簡單了,剩下那麼多的時間好無聊,於是便想打遊戲。
        最近新出了一個特別火的遊戲,叫吃豬,小樂樂準備玩一玩。
        吃豬遊戲很簡單,給定一個地圖,大小為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\n",sx,sy); //printf("%d %d\n",ex,ey); //printf("%d %d\n",hx,hy); if(BFS()) printf("PIG PIG PIG!\n"); else printf("A! WO SI LA!\n"); } return 0; }
View Code