hdu5335 多校联合第四场1009 搜索

http://acm.hdu.edu.cn/showproblem.php?pid=5335

Problem Description

In an n?m maze,
the right-bottom corner is the exit (position (n,m) is
the exit). In every position of this maze, there is either a 0 or
a 1 written
on it.

An explorer gets lost in this grid. His position now is (1,1),
and he wants to go to the exit. Since to arrive at the exit is easy for him, he wants to do something more difficult. At first, he‘ll write down the number on position (1,1).
Every time, he could make a move to one adjacent position (two positions are adjacent if and only if they share an edge). While walking, he will write down the number on the position he‘s on to the end of his number. When finished, he will get a binary number.
Please determine the minimum value of this number in binary system.

Input

The first line of the input is a single integer T (T=10),
indicating the number of testcases.

For each testcase, the first line contains two integers n and m (1≤n,m≤1000).
The i-th
line of the next n lines
contains one 01 string of length m,
which represents i-th
row of the maze.

Output

For each testcase, print the answer in binary system. Please eliminate all the preceding 0 unless
the answer itself is 0 (in
this case, print 0 instead).

Sample Input

2
2 2
11
11
3 3
001
111
101

Sample Output

111
101
/**
hdu5335 多校联合第四场1009 搜索
题目大意:给定一个由0和1组成的棋盘,从左上角走到右下角路径(上下左右四种行走方式)组成二进制数问最小的是什么
解题思路:(转)如果我们规定这个人只能向下走或者向右走的话,问题会变的简单,二进制长度为n-m+1,然后我们可以一步一步求它每一步走的情况。
          首先他的二进制第一位一定要为0。在第一位为0之后一定只会向下或者向右走到终点,因为中间如果向上走或者向左走的话,二进制的
          位数会增加,大小肯定增大,所以问题的关键是找出从原点一直走0的位置,中间经过的0的位置距离终点最短的点,在这之后便只能向
          下走或者向右走。
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=1003;
int n,m,ans;
bool flag[maxn][maxn];
char a[maxn][maxn];
int dx[][2]= {1,0,0,1,-1,0,0,-1};
void bfs()
{
    queue <pair<int,int> >q;
    memset(flag,0,sizeof(flag));
    q.push(make_pair(0,0));
    while(!q.empty())
    {
        int x=q.front().first;
        int y=q.front().second;
        q.pop();
        for(int i=0; i<4; i++)
        {
            int xx=x+dx[i][0];
            int yy=y+dx[i][1];
            if(flag[xx][yy]||xx<0||xx>=n||yy<0||yy>=m)continue;
            flag[xx][yy]=1;
            ans=max(xx+yy,ans);
            if(a[xx][yy]=='0') q.push(make_pair(xx,yy));
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%s",a[i]);
        }
        ans=0;
        flag[0][0]=1;
        if(a[0][0]=='0')bfs();
        if(ans==n+m-2)
        {
            printf("%c\n",a[n-1][m-1]);
            continue;
        }
        printf("1");
        bool ok=0;
        for(int i=ans; i<n-1+m-1; i++)
        {
            bool judge=0;
            for(int k=0; k<=i; k++)
            {
                int x=k;
                int y=i-k;
                if(x<0||x>=n||y<0||y>=m||flag[x][y]==0)continue;
                if(ok&&a[x][y]=='1')continue;
                for(int j=0; j<2; j++)
                {
                    int xx=x+dx[j][0];
                    int yy=y+dx[j][1];
                    if(xx<0||xx>=n||yy<0||yy>=m)continue;
                    flag[xx][yy]=1;
                    if(a[xx][yy]=='0')judge=1;
                }
            }
            ok=judge;
            if(judge)printf("0");
            else printf("1");
        }
        printf("\n");
    }
    return 0;
}

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

时间: 2024-07-30 05:51:19

hdu5335 多校联合第四场1009 搜索的相关文章

HDOJ多校联合第四场

B题: C题:仅由'A','G','C','T',4个字母组成,给定一个字符串S,|S|<=15,给定一个整数m,以m为长度且仅含4种字母的字符串T,求LCS(S,T)为0,1,2,3....|S|,时相应字符串T的数目. 分析:dp+状态压缩 反正我不会这题,也是看了羊神的代码之后才明白这题的思路 下面说说我的理解吧: 由于|S|长度最大为15,所以用一个二进制编码表示是哪些位置上的字母构成LCS,并求相应的数目. dp[0][st],dp[1][st]记录的是相应字母构成LCS时,T可能的数

hdu5336 多校联合第四场1010 模拟+bfs优先队列

http://acm.hdu.edu.cn/showproblem.php?pid=5336 Problem Description XYZ is playing an interesting game called "drops". It is played on a r?c grid. Each grid cell is either empty, or occupied by a waterdrop. Each waterdrop has a property "siz

2015 多校赛 第四场 1009 (hdu 5335)

Problem Description In an n∗m maze, the right-bottom corner is the exit (position (n,m) is the exit). In every position of this maze, there is either a 0 or a 1 written on it. An explorer gets lost in this grid. His position now is (1,1), and he want

hdu5334 Virtual Participation 多校联合第四场

题意:给你一个数k,让你输出一个长度为n的数列, 该数列满足 不相等的子序列的个数和为k 关于不相等的定义题中有给出 思路:规律题  当k小于十万时,直接输出k个1,如果题目不要求n的范围 这道题可以都是输出k个1..... 当k大于十万时首先对于一个1到n的数列 它对应的k值为n*(n+1)/2,然后打表 打到45000 就够了,然后再判断表中离k最近切大于k的数是多少,假设为n,我们想办法使其减去n即可办法如下:对于一个1到n的数列,如果我们将其某个数变为1(不与1相邻) 那么其k值就会减一

HDU 5335(2015 ACM多校训练第四场1009)

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

HDOJ多校联合第六场

先一道一道题慢慢补上, 1009.题意,一棵N(N<=50000)个节点的树,每个节点上有一个字母值,给定一个串S0(|S0| <=30),q个询问,(q<=50000),每次询问经过两个点u,v之间的路径上的字母构成字符串S,问S0在S中作为序列出现了多少次. 分析:对于每次询问需要知道其LCA,设w = LCA(u, v),可以用tarjan处理出来,或者倍增法也行.然后w会将S0分成两部分,记做S1,S2,然后分别考虑S1在u->w的路径出现次数,以及S2在v->w出现

2014多校联合-第五场

1001:Inversion 模版题,求逆序数对.有多少逆序数对,就可以剪掉多少. 1003:Least common multiple 对于每一个子集,lcm为2的a的最大值次方*3的b的最大值次方. 所以我们只需要求出以某个b为b的最大值的时候,a的最大值的分布情况即可. 我们先把b从小到大排序. 对于某一个b,我门只需要求出之前出现过的a比当前a小的数量为x: 那么就可知对于这些a的子集,为2^x个,并且,每个子集a的最大值都为当前a. 我么还需要求出对于大于当前a的a有多少个比a小的数,

HDOJ多校联合第五场

1001 题意:求逆序对,然后交换k次相邻的两个数,使得剩下的逆序对最少. 分析:题目用到的结论是:数组中存在一对逆序对,那么可以通过交换相邻两个数使得逆序对减少1,交换k次,可以最多减少k个. 嘉定ai>aj,i < j,如果ai,aj相邻的,那么显然可以通过交换减少1:不相邻的情况, 考虑ak,k = j-1; #11:ak > aj,那么ak,aj构成逆序对,交换后逆序对减少1: #12:ak<=aj,那么ai,ak构成逆序对,问题转化为更小规模,可以通过同样的方法进一步分析

2014多校联合-第六场

最近这两场好无奈啊... 今天这场最后30分钟敲1001,压力倍增,虽然思路比较明确,但是代码打起来不怎么容易. 但是还是好在25分钟左右debug结束.提交wa,再提交,依然WA.......最后5分钟,还是没有AC掉. 一开始以为是精度问题,后来才sb的发现原来数组开小了. 在压力环境下保证代码的效率和质量真不是一件容易的事情.不过数组开小了,真是不可原谅. 1001:Map 题目相当于几条链表.把链表排成几行. 然后枚举每一列的状态会被操作多少次. 然后把和累加起来,然后直接除以状态总数.