FZU 2124 bfs+vis记录

第一次团队训练赛的题 自己看完题没看到不能用舌头吃道具..以为是什么贪心混合bfs..果断放弃..悄悄的背锅了

然后其实比较简单 只是利用vis记录的时候要分两种状态记录 有没有道具

每到一个地方 就朝四个方向都尝试一下能不能吃到 如果吃到了就更新ans

需要注意的是刚开始就要尝试四个方向

vis[2][25][25]的第一维记录道具状态

一开始认为图太小 不用vis 然而不用vis就会出现跳不出循环的状况

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<queue>
using namespace std;
char ma[25][25];
bool vis[2][25][25];///0 没有 1 有了
int n,m;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
bool check(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m)
    return true;
    return false;
}
double ans;
struct node
{
    int x;
    int y;
    double time;
    bool daoju;
};
bool ok;
double find(int x,int y)
{
    ///1
    double a=0.0;
    int xx,yy;
    xx=x;
    yy=y;
    while(xx>0)
    {
        xx--;
        a+=0.2;
        if(ma[xx][yy]==‘X‘)
        break;
        if(ma[xx][yy]==‘B‘)
        {
            ok=true;
            return a;
        }
    }
    a=0.0;
    xx=x;
    yy=y;
    while(xx<n-1)
    {
        xx++;
        a+=0.2;
        if(ma[xx][yy]==‘X‘)
        break;
        if(ma[xx][yy]==‘B‘)
        {
            ok=true;
            return a;
        }
    }
    a=0.0;
    xx=x;
    yy=y;
    while(yy>0)
    {
        yy--;
        a+=0.2;
        if(ma[xx][yy]==‘X‘)
        break;
        if(ma[xx][yy]==‘B‘)
        {
            ok=true;
            return a;
        }
    }
    a=0.0;
    xx=x;
    yy=y;
    while(yy<m-1)
    {
        yy++;
        a+=0.2;
        if(ma[xx][yy]==‘X‘)
        break;
        if(ma[xx][yy]==‘B‘)
        {
            ok=true;
            return a;
        }
    }
    return 200.0;
}
void bfs(int x,int y)
{
    vis[0][x][y]=false;
    node a;
    a.x=x;
    a.y=y;
    a.time=0.0;
    a.daoju=false;
    double aa=find(a.x,a.y);
    if(aa<150.0)
    {
        double b=aa+a.time;
        if(b<ans)
        {
            ans=b;
            ok=true;
        }
    }
    queue<node >q;
    q.push(a);
    while(!q.empty())
    {
        node b;
        b=q.front();q.pop();
        node c;
        for(int i=0;i<4;i++)
        {
            c=b;
            c.x+=dx[i];
            c.y+=dy[i];
            int v;
            if(c.daoju==false)
            v=0;
            else v=1;
            if(check(c.x,c.y)&&vis[v][c.x][c.y]==true)
            {
                if(ma[c.x][c.y]!=‘X‘)
                {
                    vis[v][c.x][c.y]=false;
                    if(c.daoju==false)
                    c.time+=1.0;
                    else c.time+=0.5;
                    if(ma[c.x][c.y]==‘B‘)
                    {
                        if(c.time<ans)
                        ans=c.time;
                    }
                    else if(ma[c.x][c.y]==‘S‘)
                    {
                        c.daoju=true;
                        double a=find(c.x,c.y);
                        if(a<150.0)
                        {
                            ok=true;
                            double b=a+c.time;
                            if(b<ans)
                            ans=b;
                        }
                        q.push(c);
                    }
                    else
                    {
                        double a=find(c.x,c.y);
                        if(a<150.0)
                        {
                            ok=true;
                            double b=a+c.time;
                            if(b<ans)
                            ans=b;
                        }
                        q.push(c);
                    }
                }
            }
        }
    }
}
int main(){
while(~scanf("%d%d",&n,&m))
{
    memset(vis,true,sizeof(vis));
    for(int i=0;i<n;i++)
    {
        scanf("%s",ma[i]);
    }
    ok=false;
    ans=9999.0;
    for(int i=0;i<n;i++)
    {
        for(int k=0;k<n;k++)
        {
            if(ma[i][k]==‘P‘)
            {
                bfs(i,k);
                if(ok)
                printf("%.1f\n",ans);
                else printf("-1\n");
            }
        }
    }
}
}
时间: 2024-08-01 10:44:35

FZU 2124 bfs+vis记录的相关文章

迷宫bfs+路径记录

给定5*5的地图,1表示墙,0表示空地,从左上角走到左下角 是把所有的可走路径都记录下来了,但是 搜索有递归与非递归形式 本代码使用非递归形式 bfs+路径记录 对于num[i].pre=head的理解是他存在很多条路,每个点是从上一个点走过来的,但总存在一条路是到达终点的,所以,只需要得到到终点的一个head就可以顺着这个路径标记回去了 #include <iostream> #include <cstdio> using namespace std; char a[10][10

FZU 2196(bfs)

G - Escape Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 2196 Description 小明进入地下迷宫寻找宝藏,找到宝藏后却发生地震,迷宫各处产生岩浆,小明急忙向出口处逃跑.如果丢下宝藏,小明就能迅速离开迷宫,但小明并不想轻易放弃自己的辛苦所得.所以他急忙联系当程序员的朋友你(当然是用手机联系),并告诉你他所面临的情况,

FZU 2092 bfs+记忆化搜索

晚上团队训练赛的题 和普通bfs不同的是 这是同时操纵人与影子两个单位进行的bfs 由于可能发生人和影子同时接触水晶 所以不可以分开操作 当时使用node记录人和影子的位置 然后进行两重for循环来分别改变位置 结果超内存 分析了一下应该是队列超了内存 毕竟如果每个点都存入的话一个点最多可以衍生出25个node 然后t最大为200s 一定会超 之间还发生了一些并不能理解的bug 被逼到最后重构才拿到了一个超内存 名次也不好 急需一个ac来赶上去 简直要烧起来了 侧面反映心理素质还是差一些 当时未

HDU 1104 Remainder(BFS路径记录+数论)

Remainder Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4337    Accepted Submission(s): 1095 Problem Description Coco is a clever boy, who is good at mathematics. However, he is puzzled by a d

POJ - 3414 Pots (BFS+路径记录)

题目链接:http://poj.org/problem?id=3414 题意:两个空杯子倒水,使得任意一个杯子中的水量达到题目要求,有6种操作:装满a,装满b,倒掉a,倒掉b,a->b,b->a. 题解:模拟一下操作,就好了. 1 #include <queue> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7

FZU 2028 BFS+vector

一个普通的bfs 如果不看样例和input的解释... 四个0真是神样例 又被input误导 以为每个点都按顺序有标号 传送门的终点给的是一个点的标号 然后结果是什么呢?无尽的runtime error...持续了半个训练赛的runtime error.. 然后其实传送门的终点给的是坐标 莫忘-1 然后vector莫忘清空 然后当bfs开始扫传送门的时候莫忘 b是不停在变的(至少在我写的程序中是) 所以for循环中v[][]括号中的应该是从队列中取出来的node的坐标 #include<stdi

POJ 3984:迷宫问题(BFS+路径记录)

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7560   Accepted: 4426 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要

[bfs,深度记录] East Central North America Regional Contest 2016 (ECNA 2016) D Lost in Translation

Problem D Lost in Translation The word is out that you’ve just finished writing a book entitled How to Ensure Victory at a Programming Contest and requests are flying in. Not surprisingly, many of these requests are from foreign countries, and while

poj 3414 pots (bfs+路径记录)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Accepted: 4955   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f