zoukankan      html  css  js  c++  java
  • HDU 3345

    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('
    ') ;
            }
            putchar('
    ') ;
        }
        return 0 ;
    }
    View Code
  • 相关阅读:
    取消Git代理设置
    在Go语言中使用JSON(去掉空字段)
    go-- 用go-mssql驱动连接sqlserver数据库
    Go加密解密之DES
    Go语言interface详解
    Go--避免SQL注入
    golang: 把sql结果集以json格式输出
    Golang操作数据库
    Oracle ORA-01555(快照过旧)
    racle undo 解析
  • 原文地址:https://www.cnblogs.com/xiaohongmao/p/3481234.html
Copyright © 2011-2022 走看看