HDU 5335 贪心+BFS

求从0,0点到n-1,m-1点的最小二进制数

分两种情况:

1:若0,0点为‘1’,则为起点,进bfs贪心

2:若0,0点为‘0’,则找出起点所连接的所有‘0’点所能接触到的‘1’点,取其中离终点曼哈顿距离最小的点当做起点,保证01串最短

bfs贪心:每次只往右或下方走,对所有能到达的新点,若有0,则只进是0的点,否则进1

#include "stdio.h"
#include "string.h"
#include "queue"
using namespace std;

const int inf=0x3f3f3f3f;
const int dir[4][2]={ {1,0},{0,1},{0,-1},{-1,0} };
struct node
{
    int x,y;
}s[2010];

char str[1010][1010];
int n,m,cnt;
int mark[1010][1010];
void find_s()
{
    queue<node>q;
    node cur,next;
    int i,j,x,key;

    memset(mark,-1,sizeof(mark));
    if (str[0][0]=='1') // 若0,0点为‘1’,则起点唯一
    {
        cnt=1;
        s[0].x=0;
        s[0].y=0;
        return ;
    }
    cur.x=0;cur.y=0;
    q.push(cur);
    mark[0][0]=0;
    while (!q.empty())
    {
        cur=q.front();
        q.pop();
        for (i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0];
            next.y=cur.y+dir[i][1];
            if (next.x<0 || next.y<0 || next.x>=n || next.y>=m) continue;
            if (mark[next.x][next.y]!=-1) continue;
            if (str[next.x][next.y]=='1')
                mark[next.x][next.y]=1;
            if (str[next.x][next.y]=='0')
            {
                mark[next.x][next.y]=0;
                q.push(next);
            }
        }
    }
    if (mark[n-1][m-1]==0)
    { cnt=0; return ;}

    cnt=0;
    key=inf; // 找出离终点曼哈顿距离最小的且可由起点‘0’到达的‘1’点
    for (i=0;i<n;i++)
        for (j=0;j<m;j++)
        if (mark[i][j]==1)
        {
            x=n+m-2-i-j;
            if (x<key)
            {
                cnt=0;
                s[cnt].x=i;
                s[cnt].y=j;
                cnt++;
                key=x;
            }
            else if (x==key)
            {
                s[cnt].x=i;
                s[cnt].y=j;
                cnt++;
            }
        }
}
/*
1
5 6
011101
001111
010111
111110
110101
*/

void bfs()
{
    queue<node>q1,q2;
    node cur,next;
    int i,flag;

    memset(mark,0,sizeof(mark));
    for (i=0;i<cnt;i++)
    {
        mark[s[i].x][s[i].y]=1;
        q1.push(s[i]);
    }
    printf("1");
    if (mark[n-1][m-1]==1) return ;

    while (1)
    {
        flag=1;
        while (!q1.empty())
        {
            cur=q1.front();
            q1.pop();
            for (i=0;i<2;i++)
            {
                next.x=cur.x+dir[i][0];
                next.y=cur.y+dir[i][1];
                if (next.x<0 || next.y<0 || next.x>=n || next.y>=m) continue;
                if (mark[next.x][next.y]==0)
                {
                    if (str[next.x][next.y]=='0')
                        flag=0;
                    mark[next.x][next.y]=1;
                    q2.push(next);
                }
            }
        }

        printf("%d",flag); // 若下一步有‘0’可走,只走0
        if (mark[n-1][m-1]==1) break;
        while (!q2.empty())
        {
            cur=q2.front();
            q2.pop();
            if (flag==0)
            {
                if (str[cur.x][cur.y]=='0')
                    q1.push(cur);
            }
            else
                q1.push(cur);
        }

    }
}

int main()
{
    int t,i;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d%d",&n,&m);
        for (i=0;i<n;i++)
            scanf("%s",str[i]);

        find_s(); // 找出可行的起点
        if(cnt==0 ) //从起点可只通过0直接到达终点
            printf("0");
        else
            bfs();
        printf("\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 07:20:00

HDU 5335 贪心+BFS的相关文章

HDU 5335 多校第4场 1009 Walk Out

Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1794    Accepted Submission(s): 340 Problem Description In an n?m maze, the right-bottom corner is the exit (position (n,m) is the exit)

hdu 5335 Walk Out (搜索)

题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走的路线上的0/1组成的二进数最小,现在要为矫情无比的探险家找最优路径咯. 解题思路: 对于二进制数,前导零是对数字大小没有任何影响的.当到不得不走1的时候就只能向下,或者向右走了.所以先搜索出来一直走零,能走到的最靠近终点的位置,然后在类似搜索,找出最优路径. 1 #include <queue>

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

hdu 4105 贪心思想

淋漓尽致的贪心思想 波谷一定是一位数,波峰一位数不够大的时候添加到两位数就一定够大了的. 当在寻找波谷碰到零了就自然当成波谷. 当在寻找波峰时碰到零时,将前面的波谷加到前一个波峰上,让当前的零做波谷,使得波谷的值尽量小,这就是本题最关键的贪心思想,一直想不到. 代码中:a表示前一个值,b表示当前考虑的值,tag为偶数时表示正在寻找波谷,奇数时在寻找波峰. #include<iostream> #include<cstdio> #include<cstring> #inc

HDU 4923 (贪心+证明)

Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

hdu 2037 贪心

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27361    Accepted Submission(s): 14439 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" &quo

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

hdu 4856 Tunnels(bfs+状态压缩)

题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,现在有人想要体验下这M个管道,问最短需要移动的距离,起点未定. 解题思路:首先用bfs处理出两两管道之间移动的距离,然后后用状态压缩求出最短代价,dp[i][j],i表示的已经走过的管道,j是当前所在的管道. #include <cstdio> #include <cstring> #include <queue> #include <algorithm&g

hdu 1104 数论+bfs

题意:给n,m,k;输出n经过+-*%后(n%k+k)%k==((n+1)%k)%k  输出最小路径与运算副 zsd:% 只是求余数 有正负 mod 是求模 无正负. yhd:对m*k求余对 对k求余不对 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5