Walk Out

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2724 Accepted Submission(s): 548

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

经过一下午的努力,终于AC了,还是要靠特殊的数据测题啊

加组数据

1

5 5

00000

11110

00000

01111

00000

Sample Output

0

#include <iostream>
#include <cstdio>
#include <cmath>
#include <map>
#include <queue>
#include <stack>
#include <cstring>
#include <cstdlib>
#include <string>
#include <algorithm>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define eps 1e-9
#define WW freopen("output","w",stdout)
#define RR freopen("input","r","stdin") using namespace std; const int MAX = 1010; struct node
{
int x;
int y;
} q[2*MAX]; char str[MAX][MAX];
bool vis[MAX][MAX];
int n,m;
int top;
int sum;
int dir[][2]= {{0,1},{1,0},{0,-1},{-1,0}};
bool Judge(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m)
{
return true;
}
return false;
}
void bfs()//如果str[0][0]=='0',则找到一个通过一系列的零可以到达且离终点最近
{
memset(vis,false,sizeof(vis));
queue<node>Q;
node a,b;
a.x=0;
a.y=0;
Q.push(a);
vis[0][0]=true;
while(!Q.empty())
{
b=Q.front();
Q.pop();
for(int i=0; i<4; i++)
{
a.x=b.x+dir[i][0];
a.y=b.y+dir[i][1];
if(!Judge(a.x,a.y)||vis[a.x][a.y])
{
continue;
}
if(str[a.x][a.y]=='1')
{
if(a.x+a.y>sum)
{
top=0;
q[top].x=a.x;
q[top].y=a.y;
top++;
sum=a.x+a.y;
}
else if(a.x+a.y==sum)
{
q[top].x=a.x;
q[top].y=a.y;
top++;
}
}
else
{
if(a.x+a.y==n+m-2)//重要的一点,因为开始没有写WA了好几次。
{
top=0;
return ;
}
Q.push(a);
}
vis[a.x][a.y]=true;
}
}
} void DFS()
{
memset(vis,false,sizeof(vis));
queue<node>q0;
queue<node>q1;
node a,b;
for(int i=0; i<top; i++)
{
vis[q[i].x][q[i].y]=true;
q0.push(q[i]);
}
printf("1");
if(vis[n-1][m-1])
{
return ;
}
while(1)
{
int flag=1;
while(!q0.empty())
{
b=q0.front();
q0.pop();
for(int i=0; i<2; i++)
{
a.x=b.x+dir[i][0];
a.y=b.y+dir[i][1];
if(!Judge(a.x,a.y)||vis[a.x][a.y])
{
continue;
}
if(str[a.x][a.y]=='0')
{
flag=0;
}
q1.push(a);
vis[a.x][a.y]=true; }
} printf("%d",flag); if(vis[n-1][m-1])
{ return ;
}
while(!q1.empty())
{
b=q1.front();
q1.pop();
if(flag)
{
q0.push(b);
}
else
{
if(str[b.x][b.y]=='0')
{
q0.push(b);
}
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
top=0;
sum=0;
for(int i=0; i<n; i++)
{
scanf("%s",str[i]);
}
if(str[0][0]=='1')
{
q[0].x=0;
q[0].y=0;
top++;
}
else
{
bfs();
}
if(top==0)
{
printf("0");
}
else
{
DFS();
}
printf("\n");
}
return 0;
}

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

多校4-Walk Out 分类: 比赛 2015-08-02 17:15 21人阅读 评论(0) 收藏的更多相关文章

  1. 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏

    文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...

  2. PIGS 分类: POJ 图论 2015-08-10 09:15 3人阅读 评论(0) 收藏

    PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18209 Accepted: 8277 Description Mir ...

  3. bzoj 1041 圆上的整点 分类: Brush Mode 2014-11-11 20:15 80人阅读 评论(0) 收藏

    这里先只考虑x,y都大于0的情况 如果x^2+y^2=r^2,则(r-x)(r+x)=y*y 令d=gcd(r-x,r+x),r-x=d*u^2,r+x=d*v^2,显然有gcd(u,v)=1且u&l ...

  4. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  5. strace使用详解(转) 分类: shell ubuntu 2014-11-27 17:48 134人阅读 评论(0) 收藏

    (一) strace 命令    用途:打印 STREAMS 跟踪消息. 语法:strace [ mid sid level ] ... 描述:没有参数的 strace 命令将所有的驱动程序和模块中的 ...

  6. 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏

    N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...

  7. c++ 字符串流 sstream(常用于格式转换) 分类: C/C++ 2014-11-08 17:20 150人阅读 评论(0) 收藏

    使用stringstream对象简化类型转换 C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中 ...

  8. 合并k个已排序的链表 分类: leetcode 算法 2015-07-09 17:43 3人阅读 评论(0) 收藏

    最先想到的是把两个linked lists 合并成一个. 这样从第一个开始一个一个吞并,直到所有list都被合并. class ListNode:# Definition for singly-lin ...

  9. IOS之按钮控件--Button全解析及使用 分类: ios技术 2015-01-17 17:09 169人阅读 评论(0) 收藏

    IOS开发中伴随我们始终的 最常用的几个空间之一 -- UIButton 按钮,对于button今天在此做一些浅析,并介绍下主流用法以及常见问题解决办法. 首先是继承问题,UIButton继承于UIC ...

随机推荐

  1. Java基础之写文件——将素数写入文件中(PrimesToFile)

    控制台程序,计算素数.创建文件路径.写文件. import static java.lang.Math.ceil; import static java.lang.Math.sqrt; import ...

  2. wampserver环境下,apache本地下设置多个域名

    Apache在安装之后默认只是指向一个站点,即127.0.0.1,如果要进行多站点的配置,需要更改一些配置. 步骤.方法: 1,)让Apache在启动时能加载虚拟主机模块. 打开Apache安装目录下 ...

  3. 实验十四_访问CMOS RAM

    编程:以"年/月/日 时:分:秒"的格式,显示当前的日期,时间. 注意:CMOS RAM中存储着系统的配置信息,除了保存时间信息的单元外,不要向其他的单元写入内容,否则将引起一些系 ...

  4. 0428—Scrum团队成立及《构建之法》第六、七章读后感

    5.Scrum团队成立 5.1 团队名称:喳喳 团队目标:突破渣渣 团队口号:吱吱喳喳 团队照: 5.2 角色分配 产品负责人: 112冯婉莹 Scrum Master:109张鑫相 PM项目经理:1 ...

  5. Sublime 不自动打开上次未关闭的文件 设置方法

    { "font_size": 17, "hot_exit": false, "remember_open_files": false, &q ...

  6. 01分数规划POJ2976(简单模板题)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7276   Accepted: 2523 De ...

  7. 20145207 《Java程序设计》第5周学习总结

    前言:先聊两句,上午电路实习,刚开始没多久就让电烙铁烫了,倒霉催的~晚上来这里接着弄代码,透心凉心飞扬~ 教材学习内容总结 一.异常处理 1.语法与继承结构 使用try.catch: Java中所有错 ...

  8. 科学计算器的Java实现

    简易的科学计算器的实现 ---Java版 import javax.swing.*;//新的窗口组件包 import java.awt.*; import java.awt.event.*; publ ...

  9. const 关键字及作用

    1.const 修饰一般常量,可以把变量变成常量 例如: int  num=10; num=100; printf(“num=%d\n”,num); 输出的来得值为:100: 但是如果const in ...

  10. [转]Java多线程干货系列—(一)Java多线程基础

    Java多线程干货系列—(一)Java多线程基础 字数7618 阅读1875 评论21 喜欢86 前言 多线程并发编程是Java编程中重要的一块内容,也是面试重点覆盖区域,所以学好多线程并发编程对我们 ...