【BFS】 计蒜客T1214

 计蒜客T1214

  • 就是一个带状态的简单搜索。
  • 我真的吐了,比赛的时候MLE了n发,结果就差一个inque数组我就过了……因为没有判断相同点的相同状态有没有在队列中,所以爆了。我吐了我吐了我吐了我真的吐了……qaq
  • 总而言之我太菜了。这个题疯狂MLE。想到是队列出了问题。但是愣死就是没想到再加一个数组。太久没做搜索了。
  • 队长说的没错,连大一的都不如。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)

#define MID (l + r ) >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid + 1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define eps  1e-6

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 200;
inline int read()
{
    int x = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -f; c = getchar(); }
    while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}
int m, n, t;
char mp[maxN][maxN];
int sx, sy;
const int dir[4][2] = {
        1, 0,
        0, 1,
        -1, 0,
        0, -1
};
struct node{
    int x, y, dis, state;
    node() {}
    node(int a, int b, int c, int d): x(a), y(b), dis(c), state(d) {}
};
bool vis[maxN][maxN][10], inque[maxN][maxN][10];
bool InMap(int x, int y)
{
    return x >= 0 && x < m && y >= 0 && y < n;
}
int bfs(int x, int y)
{
    memset(vis, 0, sizeof(vis));
    queue<node>q;
    q.push(node(x, y, 0,  t)); inque[x][y][t] = 1;
    while(!q.empty())
    {
        node tmp = q.front(); q.pop();
        for(int i = 0; i < 4; i ++ )
        {
            int xx = tmp.x + dir[i][0];
            int yy = tmp.y + dir[i][1];
            if(!InMap(xx, yy) || (xx == sx && yy == sy))
                continue;
            else if(mp[xx][yy] == '#' && tmp.state == 0)
                continue;
            else if(mp[xx][yy] == '#' && tmp.state && !vis[xx][yy][tmp.state - 1] && !inque[xx][yy][tmp.state - 1])
            {
                q.push(node(xx, yy, tmp.dis + 1, tmp.state - 1));
                inque[xx][yy][tmp.state - 1] = 1;
            }
            else if(mp[xx][yy] == '*' && !vis[xx][yy][tmp.state] && !inque[xx][yy][tmp.state])
            {
                q.push(node(xx, yy, tmp.dis + 1, tmp.state));
                inque[xx][yy][tmp.state] = 1;
            }
            else if(mp[xx][yy] == '+')
                return tmp.dis + 1;
        }
        vis[tmp.x][tmp.y][tmp.state] = 1;
    }
    return -1;
}
int main()
{
    m = read(); n = read(); t = read();
    for(int i = 0; i < m; i ++ )
    {
        for(int j = 0; j < n; j ++ )
        {
            scanf("%c", &mp[i][j]);
            if(mp[i][j] == '@')
            {
                sx = i;
                sy = j;
            }
        }
        getchar();
    }
    printf("%d\n", bfs(sx, sy));
    return 0;
}

我也真的有毒,纠缠烂打?没有自知之明,自找没趣,自作多情……原谅我词穷了。反正就是一个很不堪的人。

我从小就对自己说:xlq,一定要活成自己想要的模样。

一年一年过去,只有在tear中才想起自己说的话,有的时候就算想到也还是对某些人某些事某些东西有一种执念,然后放任自己浪荡,放任自己颓废。我希望自己happy,也希望大家happy。希望大家心想事成。至于我,我心想成不了。不喜欢随遇而安,不相信命中注定,不屈服于命运,那个在梦里si掉的xlq就真的si了。最强悍的第三者不是别人,而是命运。JJ说的,但是你有你的XiaoZhang,我也有我的Arrogant.  至于命运,等命运真的快要毁掉世界的时候再说。推荐一首歌——嚣张。挺好听的,起码我觉得是这样。

2020年加油。没有tear,只有laughter. 这是我唯一的愿望。

再加一个:成为 可 爱 的人儿。【并非字面意思】

发布了190 篇原创文章 · 获赞 57 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44049850/article/details/104060968
BFS