Codevs 1215 迷宫 裸BFS

上一次打是在寒假,现在代码风格都有了很大的改变。
当时还是跟着伟大的红太阳 DQS(%%)学的呢。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

int n;
char maps[233][233];
bool v[233][233];
int dx[] = {0,1,0,-1,0};
int dy[] = {0,0,1,0,-1};
int sx, sy, ex, ey;

struct loc{
    int x, y;
};

queue <loc> q;
void bfs(int sx, int sy)
{
    q.push((loc){sx,sy});
    v[sx][sy] = 1;
    while(q.size())
    {
        loc nw = q.front();
        q.pop();
        if(nw.x == ex && nw.y == ey){
            cout << "YES" << endl;
            return;
        }
        for(int i = 1; i <= 4; i ++)
        {
            int x = nw.x + dx[i];
            int y = nw.y + dy[i];
            if(x > 0 && x <= n && y > 0 && y <= n && maps[x][y] != '#' && !v[x][y])
            {
                q.push((loc){x,y});
                v[x][y] = 1;
            }
        }
    }
    cout << "NO" << endl;
}

int main()
{
    int t;
    cin >> t;
    cin >> n;
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= n; j ++){
            cin >> maps[i][j];
            if(maps[i][j] == 's')
                sx = i,sy = j;
            else if(maps[i][j] == 'e')
                ex = i; ey = j;
        }
    bfs(sx,sy);
    return 0;
}
以上:2016-06-11 21:33:37

2016-9-28 17:04:55
重打:

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN = 20;
const int dx[] = {0,0,-1,0,1};
const int dy[] = {0,-1,0,1,0};
int n, sx, sy;
char maps[MAXN][MAXN];

struct Point{
    int x, y;
};

queue <Point> q;
void bfs(int sx, int sy)
{
    q.push((Point){sx,sy});
    maps[sx][sy] = '#';
    while(q.size())
    {
        Point now = q.front();
        q.pop(); 
        for(int i = 1; i <= 4; i ++)
        {
            int nx = now.x + dx[i];
            int ny = now.y + dy[i];
            if(maps[nx][ny] == 'e')
            {
                puts("YES");
                return;
            }
            if(nx >= 0 && nx <= n && ny >= 0 && ny <= n && maps[nx][ny] == '.')
            {
                maps[nx][ny] = '#';
                q.push((Point){nx, ny});
            }
        }
    }
    puts("NO");
}

void init()
{
    while(q.size())
        q.pop();
}

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        init();
        cin >> n;
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
            {
                char c = getchar();
                while(c != 's' && c != 'e' && c != '.' && c != '#')
                    c = getchar();
                maps[i][j] = c;
                if(c == 's')
                    sx = i, sy = j;
            }
        bfs(sx,sy);
    }
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值