HDU 5335 Walk Out BFS + 贪心 2015 Multi-University Training Contest 4 1009

8 篇文章 0 订阅
这题的题意是给你一个矩阵,起点在00点,终点在右下角,问你跑过去的二进制的值最小是多少。这题用的是BFS加贪心做的,如果(0,0)点是0,便用dfs找到连成一片的距离最远的0,然后附近的1就是起点,然后跑BFS,在BFS中,如果这一层中有0,那么这一层状态为1的都不跑。这里的层是BFS深度的意思。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#define maxn 1000000
#define inf (1e9 + 5)
#define mem(a, b) memset(a, b, sizeof(a))
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;

int n, m, nn, ox[4] = {0, 1, 0, -1}, oy[4] = {1, 0, -1, 0}, prex[1005][1005], prey[1005][1005], maxs;
char maps[1005][1005];
bool vis[1005][1005], vis2[2005];

struct node
{
    int x, y, dis;
}nd[1000 * 1000 + 5];

void dfs(int x, int y)//寻找连一片的最远的起始点
{
    for(int i = 0;i < 4;i++)
    {
        if(x + ox[i] >= 0&&x + ox[i] < n&&y + oy[i] >= 0&&y + oy[i] < m&&!vis[x + ox[i]][y + oy[i]]&&maps[x + ox[i]][y + oy[i]] == '0')
        {
            vis[x + ox[i]][y + oy[i]] = 1;
            dfs(x + ox[i], y + oy[i]);
        }
        else if(x + ox[i] >= 0&&x + ox[i] < n&&y + oy[i] >= 0&&y + oy[i] < m&&!vis[x + ox[i]][y + oy[i]]&&maps[x + ox[i]][y + oy[i]] == '1')
        {
            if(maxs < (x + ox[i] + y + oy[i]))
                maxs = x + ox[i] + y + oy[i];
            vis[x + ox[i]][y + oy[i]] = 1;
            nd[nn].x = x + ox[i];
            nd[nn].y = y + oy[i];
            nd[nn++].dis = 1;
        }
    }
}

void prt(int a, int b)//打印
{
    int x = prex[a][b];
    int y = prey[a][b];
    if(prex[x][y] == -1)
    {
        printf("%c", maps[x][y]);
        printf("%c", maps[a][b]);
        return;
    }
    prt(x, y);
    printf("%c", maps[a][b]);
}

void bfs()
{
    queue<struct node> q;
    for(int i = 0;i < nn;i++)
        if(maxs == nd[i].x + nd[i].y)//maxs保证最远
            q.push(nd[i]);
    int dd = 1;//层数
    vis2[1] = 0;//表示一层状态没有0
    while(!q.empty())
    {
        struct node tmp = q.front();
        q.pop();
        if(tmp.x == n - 1&&tmp.y == m - 1)
        {
            if(prex[tmp.x][tmp.y] != -1)
                prt(tmp.x, tmp.y);
            return;
        }
        if(dd < tmp.dis)//更新层
            dd = tmp.dis;
        if(vis2[dd]&&maps[tmp.x][tmp.y] == '1')//如果此层有0,那么1不跑。
            continue;
        for(int i = 0;i < 2;i++)
        {
            if(tmp.x + ox[i] >= 0&&tmp.x + ox[i] < n&&tmp.y + oy[i] >= 0&&tmp.y + oy[i] < m&&!vis[tmp.x + ox[i]][tmp.y + oy[i]])
            {
                vis[tmp.x + ox[i]][tmp.y + oy[i]] = 1;
                prex[tmp.x + ox[i]][tmp.y + oy[i]] = tmp.x;
                prey[tmp.x + ox[i]][tmp.y + oy[i]] = tmp.y;
                if(tmp.dis == dd&&maps[tmp.x + ox[i]][tmp.y + oy[i]] == '0')
                    vis2[dd + 1] = 1;
                struct node jj;
                jj.x = tmp.x + ox[i];
                jj.y = tmp.y + oy[i];
                jj.dis = tmp.dis + 1;
                q.push(jj);
            }
        }
    }
}

int main()
{
    int cas;
    scanf("%d", &cas);
    while(cas--)
    {
        nn = 0;
        mem(vis, 0);
        mem(vis2, 0);
        mem(prex, -1);
        mem(prey, -1);
        mem(maps, 0);
        maxs = 0;
        scanf("%d%d%*c", &n, &m);
        for(int i = 0;i < n;i++)
            scanf("%s", maps[i]);
        if(n == 1&&m == 1)
        {
            if(maps[0][0] == '0')
                printf("0\n");
            else
                printf("1\n");
            continue;
        }
        vis[0][0] = 1;
        if(maps[0][0] - '0' > 0)
            nd[nn].x = 0, nd[nn].y = 0, nd[nn++].dis = 1;
        else
            dfs(0, 0);
        bfs();
        if(prex[n - 1][m - 1] == -1&&maps[n - 1][m - 1] == '0')
            printf("0");
        else if(prex[n - 1][m - 1] == -1&&maps[n - 1][m - 1] == '1')
            printf("1");
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值