http://acm.hdu.edu.cn/showproblem.php?pid=3345

最近重写usaco压力好大,每天写的都想吐。。

水一道bfs

注意的是开始旁边有敌人可以随便走,但是一旦开始走,再与敌人相邻行动力就变为0

#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <queue> 
#include <cstdio>
#include <cstring>
using namespace std ;
char map[101][101] ;
char ans[101][101] ;
int vis[101][101] ;
int n,m,MV ;
typedef struct L{
    int x,y ;
    int mv ;
    friend bool operator <(L a,L b)
    {
        return a.mv<b.mv ;
    } 
}L ;
L s ;
int dir[4][2]={0,1,1,0,0,-1,-1,0} ;
int ok(int x,int y)
{
    for(int i=0 ;i<4 ;i++)
    {
        int xx=x+dir[i][0] ;
        int yy=y+dir[i][1] ;
        if(xx<0 || yy<0 || xx>=n || yy>=m)continue ;
        if(map[xx][yy]=='E')
            return 1 ;
    }
    return 0 ;
}
void bfs()
{
    priority_queue <L> q ;
    L now,next ;
    memset(vis,0,sizeof(vis)) ;
    q.push(s) ;
    vis[s.x][s.y]=1 ;
    while(!q.empty())
    {
        now=q.top() ;
        for(int i=0 ;i<4 ;i++)
        {
            int xx=now.x+dir[i][0] ;
            int yy=now.y+dir[i][1] ;
            if(xx<0 || yy<0 || xx>=n || yy>=m)continue ;
            if(vis[xx][yy])continue ;
            if(map[xx][yy]=='#')continue ;
            if(map[xx][yy]=='E')continue ;
            next.x=xx ;next.y=yy ;
            if(map[xx][yy]=='.' && now.mv>=1)
            {
                next.mv=now.mv-1 ;
                if(ok(xx,yy))
                    next.mv=0 ;
                vis[xx][yy]=1 ;
                ans[xx][yy]='*' ;
                q.push(next) ;
            }
            if(map[xx][yy]=='T' && now.mv>=2)
            {
                next.mv=now.mv-2 ;
                if(ok(xx,yy))
                    next.mv=0 ;
                vis[xx][yy]=1 ;
                ans[xx][yy]='*' ;
                q.push(next) ;
            }
            if(map[xx][yy]=='R' && now.mv>=3)
            {
                next.mv=now.mv-3 ;

                if(ok(xx,yy))
                    next.mv=0 ;
                vis[xx][yy]=1 ;
                ans[xx][yy]='*' ;
                q.push(next) ;
            }
            if(map[xx][yy]=='P' && now.mv>=1)
            {
                next.mv=now.mv-1 ;
                if(ok(xx,yy))
                    next.mv=0 ;
                vis[xx][yy]=1 ;
                q.push(next) ;
            }
        }
        q.pop() ;
    }
}
int main()
{
    int t ;
    scanf("%d",&t) ;
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&MV) ;
        for(int i=0 ;i<n ;i++)
            scanf("%s",map[i]) ;
        for(int i=0 ;i<n ;i++)
        {
            for(int j=0 ;j<m ;j++)
            {
                ans[i][j]=map[i][j] ;
                if(map[i][j]=='Y')
                    s.x=i,s.y=j,s.mv=MV ;
            }
        }
        bfs() ;
        for(int i=0 ;i<n ;i++)
        {
            for(int j=0 ;j<m ;j++)
            {
                printf("%c",ans[i][j]) ;
            }
            putchar('\n') ;
        }
        putchar('\n') ;
    }
    return 0 ;
}
View Code