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  nm  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 (1n,m1000) . 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
 

Author
XJZX

题意:

给你一个迷宫,迷宫由0 1串组成,让你从  1 1 点走到 n,m点 ,路径上的所以点连起来就是个二进制数,求这个二进制数的最小值

思路:

一个二进制数想要小,首先位数一定要小,其次每一位让0尽肯能的出现在前面,也会小,所以第一步找到距离终点最近的一个1,这个1满足起点到这个1直接只由0组成

找到这个1之后就可以贪心走又下方(不向左上的原因是会使位数变大),这里需要用生命来优化,走过的点不能走,不能让地图每个点都要个string 来标记这个点的值,这样都会爆内存。标记用一个string 来记录当前结果,用另一个变量标记这个数是搜索的第几层,总之用生命来优化,下面贴代码

/*********************************
** 2015 Multi-University Training Contest 4 1009
** 2015-7-31
** by calamity_coming
** hdu 5335
**********************************/

#include <bits/stdc++.h>
using namespace std;
const int inf=0x7f7f7f7f;
typedef unsigned int intt;
typedef long long ll;
const int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};

char maze[1010][1010];
int n,m;
struct Point
{
    int x,y;
    int fair()//到终点的距离
    {
        return m + n - x - y - 2;
    }
    Point (int _x = 0,int _y = 0) : x(_x) , y(_y) {}
};

class Solution
{
    bool end_zero;
    queue<Point> jb;
    int min_long;
    string final_ans;

public:
    Solution() : end_zero (false) , final_ans("1")
    {
        cin>>n>>m;
        for(int i=0; i<n; ++i)
        {
            cin>>maze[i];
        }
        if(maze[n-1][m-1] == '0')//让他为0好判断
        {
            end_zero = true;
            maze[n-1][m-1] = '1';
        }
        min_long = n+m-2;
    }

    /*********************************************************
    ** 寻找第一个1出现的位置
    ** 前提,已经把结尾强制设为1
    ************************************************************/
    void find_one()
    {
        if(maze[0][0] == '1')
        {
            jb.push(Point(0,0));
            return;
        }
        queue<Point> ac;
        ac.push(Point(0,0));
        while(!ac.empty())
        {
            Point adc = ac.front();
            ac.pop();
            for(int i=0; i<4; ++i)
            {
                int x = dir[i][0] + adc.x;
                int y = dir[i][1] + adc.y;
                if(x<0 || x>=n || y<0 || y>=m)
                    continue;
                if(maze[x][y] == '0')
                {
                    ac.push(Point(x,y));
                    maze[x][y] = '#';
                    continue;
                }
                if(maze[x][y] == '1')
                {
                    Point ap(x,y);
                    jb.push(ap);
                    maze[x][y] = '*';
                    if(ap.fair() < min_long)
                    {
                        min_long = ap.fair();
                        update_queue();//一定不为空
                    }
                }
            }
        }
    }

    void update_queue()//更新队列值,始终是满足最小的(用时间换空间)
    {
        for(int i=0,len = jb.size(); i<len; ++i)
        {
            Point adc = jb.front();
            jb.pop();
            if(min_long == adc.fair())
            {
                jb.push(adc);
            }
        }
    }

    /**********************************************************
    ** 广搜找到结果,只往右下方走
    ** 用生命在标记,防止爆内存
    ** 把走过的0变成#,1变成*,可以防止重复走
    *************************************************************/
    void bfs()
    {
        while(!jb.empty())
        {
            Point adc = jb.front();
            jb.pop();
            if(min_long == 0)
            {
                return ;
            }

            if(min_long == adc.fair() && maze[adc.x][adc.y] == '*' &&
                    final_ans[final_ans.size()-1] == '0') //被抛弃的点
            {
                continue;
            }

            if(adc.y + 1 < m)//向右
            {
                if(maze[adc.x][adc.y+1] == '0')
                {
                    Point ap = adc;
                    ap.y++;
                    if(min_long == ap.fair() + 1)//1 -> 10
                    {
                        min_long = ap.fair();
                        final_ans += '0';
                    }
                    else if(final_ans[final_ans.size()-1] == '1')//11 -> 10
                    {
                        final_ans[final_ans.size()-1] = '0';

                    }
                    jb.push(ap);
                    maze[adc.x][adc.y+1] = '#';
                }
                else if(maze[adc.x][adc.y + 1] == '1')
                {
                    if(adc.x + 1 >= n || (maze[adc.x+1][adc.y] != '0') && maze[adc.x+1][adc.y] != '#')
                    {
                        Point ap = adc;
                        ap.y++;
                        if(min_long == ap.fair() + 1)//111 -> 1111
                        {
                            min_long --;
                            final_ans += '1';
                            jb.push(ap);
                            maze[adc.x][adc.y+1] = '*';
                        }
                        else if(final_ans[final_ans.size()-1] == '1')// 11-> 11
                        {
                            jb.push(ap);
                            maze[adc.x][adc.y+1] = '*';
                        }
                    }
                }
            }

            if(adc.x + 1 < n)//向下
            {
                if(maze[adc.x+1][adc.y] == '0')
                {
                    Point ap = adc;
                    ap.x++;
                    if(min_long == ap.fair() + 1)//111 -> 1110
                    {
                        min_long = ap.fair();
                        final_ans += '0';
                    }
                    else if(final_ans[final_ans.size()-1] == '1')//1111 -> 1110
                    {
                        final_ans[final_ans.size()-1] = '0';
                    }
                    jb.push(ap);
                    maze[adc.x+1][adc.y] = '#';
                }
                
                else if(maze[adc.x+1][adc.y] == '1')
                {
                    if(adc.y + 1 >= m || (maze[adc.x][adc.y+1] != '0' && maze[adc.x][adc.y+1] != '#'))
                    {
                        Point ap = adc;
                        ap.x++;
                        if(min_long == ap.fair() +1)//111 -> 1111
                        {
                            min_long = ap.fair();
                            final_ans += '1';
                            jb.push(ap);
                            maze[adc.x+1][adc.y] = '*';
                        }
                        
                        else if(final_ans[final_ans.size()-1] == '1')// 1111 -> 1111
                        {
                            jb.push(ap);
                            maze[adc.x+1][adc.y] = '*';
                        }
                    }
                }
            }
        }
    }

    void Win_S5()
    {
        find_one();
        update_queue();//最后一定要再次更新一下,删除不该有的东西
        bfs();
        if(end_zero)
        {
            final_ans[final_ans.size()-1] = '0';
        }
        cout<<final_ans<<endl;
    }
};
int main()
{
    ios::sync_with_stdio(false);
    int t;
    int cot = 0;
    cin>>t;
    while(t--)
    {
        Solution OMG;
        OMG.Win_S5();
    }
    return 0;
}


挺虐心的,200多行,WA了一天,最后发现是队列少更新一次,还是不够细心啊


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值