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

题意:给出一幅图,起点在左上角终点在左下角。沿途走过的数字连起来为最后所得的二进制数,设法令该数最小,输出之。

思路:

起点为1的情况,令二进制数长度最小,则每次只往右走或往下走,并且步数相同时走 0 优先。

起点为0的情况,则按着 0 搜到离终点最近的为 1 的点,再按上述的方式走即可。

广搜题,,但 T 到跪。看了标程打了一份。这种 bfs 方式第一次见,确实6,我还是太年轻。。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
char g[][];
int n,m,t,head,tail;
#define maxn 1000005
int vis[][],x[maxn],y[maxn];
int dx[]={,-,,},dy[]={,,,-};
string bfs(){
memset(vis,,sizeof(vis));
vis[head=][tail=]=;
x[head]=,y[head++]=;
while(head!=tail){
if(g[x[tail]][y[tail]]==''){
for(int i=;i<;i++){
int X=x[tail]+dx[i],Y=y[tail]+dy[i];
if(X>=&&X<n&&Y>=&&Y<m&&!vis[X][Y]){
x[head]=X,y[head++]=Y;
vis[X][Y]=;
}
}
}
tail++;
}
if(vis[n-][m-]&&g[n-][m-]=='') return "";
int sum=;
string ans="";
for(int i=;i<n;i++){
for(int j=;j<m;j++)
if(vis[i][j]) sum=max(sum,i+j);
}
for(int k=sum;k<n+m-;k++){
char c='';
for(int i=,j=k-i;i<n;i++,j--) if(j>=&&j<m&&vis[i][j]){
if(i+<n) c=min(c,g[i+][j]);
if(j+<m) c=min(c,g[i][j+]);
}
ans+=c;
for(int i=,j=k-i;i<n;i++,j--) if(j>=&&j<m&&vis[i][j]){
if(i+<n&&g[i+][j]==c) vis[i+][j]=;
if(j+<m&&g[i][j+]==c) vis[i][j+]=;
}
}
return ans;
}
int main(){
//freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
scanf("%s",g[i]);
cout<<bfs()<<endl;
}
return ;
}

2015 多校赛 第四场 1009 (hdu 5335)的更多相关文章

  1. 2015 多校赛 第四场 1010 (hdu 5336)

    Problem Description XYZ is playing an interesting game called "drops". It is played on a r ...

  2. 2015 多校赛 第五场 1010 (hdu 5352)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5352 看题看得心好累. 题目大意: 给出 n 个点,依次执行 m 次操作:输入“1 x”时,表示将与 ...

  3. 2015 多校赛 第七场 1011 (hdu 5379)

    题意:给定一棵树,树上有 n 个节点.问有多少种方案,使得在每个节点上依次放置数 1~n 后,每个节点的儿子节点上的数连续(比如 1 为根,有1-2,1-3,1-4,则令2,3,4上的数连续),每个子 ...

  4. 2015 多校赛 第五场 1006 (hdu 5348)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题目大意:给出一幅无向图,问是否存在一种方案,使得给每条边赋予方向后,每个点的入度与出度之差小于 ...

  5. 2015 多校赛 第三场 1002 (hdu 5317)

    Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more i ...

  6. hdu 5328 Problem Killer(杭电多校赛第四场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5328 题目大意:找到连续的最长的等差数列or等比数列. 解题思路:1.等差等比的性质有很多.其中比较重 ...

  7. 【杂题总汇】HDU多校赛第十场 Videos

    [HDU2018多校赛第十场]Videos 最后一场比赛也结束了…… +HDU传送门+ ◇ 题目 <简要翻译> 有n个人以及m部电影,每个人都有一个快乐值.每场电影都有它的开始.结束时间和 ...

  8. NOI.AC NOIP模拟赛 第四场 补记

    NOI.AC NOIP模拟赛 第四场 补记 子图 题目大意: 一张\(n(n\le5\times10^5)\)个点,\(m(m\le5\times10^5)\)条边的无向图.删去第\(i\)条边需要\ ...

  9. hdu5379||2015多校联合第7场1011 树形统计

    pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is ...

随机推荐

  1. 熟悉RHEL7登录界面使用

    Linux操作系统提供了图像界面和字符界面两种操作环境. 图像界面: 1.开启RHEL7后进入到该界面,图中用户是我们创建的本地用户,如果我们要以管理员身份登录则点击Not listed(未列出). ...

  2. BZOJ 1724 USACO 2006 Nov. 切割木板

    倒过来的合并果子? 做法与合并果子一样 维护一个小根堆,每次取出最小的两个数进行合并 #include<cstdio> #include<algorithm> #include ...

  3. hdu2016 数据的交换输出【C++】

    数据的交换输出 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  4. scrapy——4 —反爬措施—logging—重要参数—POST请求发送实战

    scrapy——4 常用的反爬虫策略有哪些 怎样使用logging设置 Resquest/Response重要参数有哪些 Scrapy怎么发送POST请求 动态的设置User-Agent(随即切换Us ...

  5. hdu_hpu第八次周赛_1002 大菲波数_201310270958

    大菲波数 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  6. iOS:制作左右侧滑(抽屉式)菜单

    感谢控件作者:https://github.com/SocialObjects-Software/AMSlideMenu 首先上效果图: 这里我们使用AMSlideMenu来实现左右侧滑菜单的效果.控 ...

  7. CF #330 D2 E

    相当于给你一些点,要你最多删除不超过k,使得能使用一个边长为整数的长方形,与XY轴平行,使长方形的面积最小. 上课时拿笔来画画,然后忽然思路就开了,要是比赛也这样就好了~~先按X,Y分别排序,由于K较 ...

  8. Windows移动开发(五)——初始XAML

    关于详细的基本功就先说这么多.后面遇到再补充说明,前面说的都是一些代码和原理方面的东西.接下来说的会有界面和代码结合,会有成就感,由于能真正的做出东西来了. Windows移动开发包含Windows ...

  9. 【HDOJ 5419】 Victor and Toys (排列组合)

    [HDOJ 5419] Victor and Toys n个玩具 m个区间 每一个玩具有一个beauty值 问任选三个区间 三区间的MINleft~MAXright的和的期望值 预处理一个数组 存放每 ...

  10. Android4.4 wpa_supplicant深入分析之wpa_supplicant初始化流程续

    下面我们将接上一篇文章继续分析main中第二个关键函数wpa_supplicant_add_iface. wpa_supplicant_add_iface用于向wpa_supplicant添加接口设备 ...