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. img、a标签的使用

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  2. 小白年薪26万,为什么Python岗位薪资越来越高?

    人工智能和大数据概念的兴起,带动了Python的快速增长——Python语言逻辑简洁.入门简单.生态丰富,几乎成为几个新兴领域的不二选择.而除了这两个领域,Python还有更多的适用领域:爬虫.web ...

  3. MVC Ajax调用Action时-OnActionExecuting RedirectResult 无法跳转的处理办法

    public class BaseController : Controller { protected override void OnActionExecuting(ActionExecuting ...

  4. 30.3 FCL中的混合构造

     30.3.2 Monitor类和同步块 internal sealed class Transaction { private readonly object _lock = new object( ...

  5. Qt Creator 中文乱码问题

    一. Qt 4 乱码问题 解决方案 1. 在Qt 中 快捷菜单选项功能中 Edit(编辑)  --> Select Encoding...(选择编码) 选择载入(显示)编码和储存编码,要解决中文 ...

  6. Git 基础教程 之 标签

    所谓标签:就是一个让人容易记住的有意义的名字,与某个commit绑在一起. 创建标签:①切回需要打标签的分支上                  ② git tag <name>  默认标 ...

  7. JavaSE 学习笔记之StringBuilder(十六)

    < java.lang >-- StringBuilder字符串缓冲区:★★★☆ JDK1.5出现StringBuiler:构造一个其中不带字符的字符串生成器,初始容量为 16 个字符.该 ...

  8. ubuntu添加开机自启和sysv-rc-conf

    此文ubuntu使用sysvinit,而非upstart UBUNTU添加开机自动启动程序方法 1. 开机启动时自动运行程序    Linux加载后, 它将初始化硬件和设备驱动, 然后运行第一个进程i ...

  9. fzu 2129

    第i个元素a未出现过:dp[i] = (2 * dp[i-1] + 1) % mod; visit[a]代表a最后出现的位置 第i个元素a出现过:dp[i] = (2 * dp[i-1] - dp[v ...

  10. 【ACM】hdu_2007_平方和与立方和_201307261533

    平方和与立方和Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...