hdu 5335 Walk Out (2015 Multi-University Training Contest 4)
Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit:
65536/65536 K (Java/Others)
Total Submission(s): 194 Accepted Submission(s): 32
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.
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.
the answer itself is 0 (in
this case, print 0 instead).
2
2 2
11
11
3 3
001
111
101
111
101
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=1000+100;
char s[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
struct node
{
int x;
int y;
};
int ans;
int n,m;
queue<node> q;
vector<node> son[maxn*2];
void BFS1()
{
memset(vis,false,sizeof(vis));
node p;
p.x=1;
p.y=1;
ans=0;
vis[1][1]=true;
while(!q.empty())
q.pop();
if(s[1][1]=='0')
{
ans=2;
q.push(p);
}
while(!q.empty())
{
node p2;
p=q.front();
q.pop();
if(p.x==n&&p.y==m)
{
if(s[n][m]=='0')
{
ans=n+m;
vis[n][m]=true;
}
break;
}
for(int i=0; i<4; i++)
{
p2.x=p.x+dir[i][0];
p2.y=p.y+dir[i][1];
if(p2.x>0&&p2.x<=n&&p2.y>0&&p2.y<=m&&!vis[p2.x][p2.y]&&s[p2.x][p2.y]=='0')
{
vis[p2.x][p2.y]=true;
if(p2.x+p2.y>ans)
{
ans=p2.x+p2.y;
}
q.push(p2);
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%s",s[i]+1);
}
BFS1();
if(ans==n+m)
printf("0\n");
else
{
for(int i=0; i<=n+m; i++)
son[i].clear();
int cur=0;//记录前一位0出现的位置
if(ans==0)//起点为1
ans=1;
else
{
for(int i=1; i<=n; i++)//找到全部的离(n,m)近期的点。
{
int j=ans-i;
if(j>=1&&j<=m&&vis[i][j]&&s[i][j]=='0')
{
node v;
v.x=i;
v.y=j;
son[ans].push_back(v);
}
}
cur=ans;
}
for(int i=ans+1; i<=n+m; i++)//枚举每一步
{
if(cur==0)//前面不存在0
{
for(int j=1; j<=n; j++)
{
int k=i-j;
node v;
if(k>=1&&k<=m&&s[j][k]=='0')
{
v.x=j;
v.y=k;
son[i].push_back(v);
cur=i;
}
}
}
else
{
for(int j=1; j<=n; j++)
{
int k=i-j;
node v;
if(k>=1&&k<=m&&s[j][k]=='0')
{
for(int l=0; l<son[cur].size(); l++)
{
v=son[cur][l];
if(v.x<=j&&v.y<=k&&i-cur>=j+k-v.x-v.y)//推断前面的0是否可达
{
node w;
w.x=j;
w.y=k;
son[i].push_back(w);
break;
}
}
}
}
if(son[i].size()>0)
cur=i;
} }
for(int i=ans+1; i<=n+m; i++)
{
if(son[i].size()>0)
printf("0");
else
printf("1");
}
printf("\n");
}
}
return 0;
}
hdu 5335 Walk Out (2015 Multi-University Training Contest 4)的更多相关文章
- hdu 5335 Walk Out (搜索)
题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...
- 2015 Multi-University Training Contest 4 hdu 5335 Walk Out
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5335 Walk Out(多校)
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- hdu 5335 Walk Out 搜索+贪心
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total S ...
- hdu 5335 Walk Out(bfs+寻找路径)
Problem Description In an n∗m maze, the right-bottom corner or a written on it. An explorer gets los ...
- HDU 5335 Walk Out BFS 比较坑
H - H Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- HDU 6091 - Rikka with Match | 2017 Multi-University Training Contest 5
思路来自 某FXXL 不过复杂度咋算的.. /* HDU 6091 - Rikka with Match [ 树形DP ] | 2017 Multi-University Training Conte ...
- HDU 6125 - Free from square | 2017 Multi-University Training Contest 7
思路来自这里 - - /* HDU 6125 - Free from square [ 分组,状压,DP ] | 2017 Multi-University Training Contest 7 题意 ...
- HDU 6129 - Just do it | 2017 Multi-University Training Contest 7
比赛时脑子一直想着按位卷积... 按题解的思路: /* HDU 6129 - Just do it [ 规律,组合数 ] | 2017 Multi-University Training Contes ...
随机推荐
- Node.js:NPM 使用介绍
ylbtech-Node.js:NPM 使用介绍 1.返回顶部 1. NPM 使用介绍 NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: ...
- [源码管理] ubuntu中svn简明用法:服务器搭建+客户端使用
本文是对网络上前人的优秀文章加以实践验证后所整理(修正或补充) 第一部分:svn服务器搭建(主要是四步走) 参考:http://www.son1c.cn/show/920.html 一,安装Subve ...
- shp系列(二)——利用C++进行shp文件的读(打开)
1.各数据类型及其字节数 BYTE 1; char 1; short 2; int 4; double 8; 2.位序big和little及其转换 对于位序是big的 ...
- IO编程 - 转载自廖雪峰的博文
IO在计算机中指Input/Output,也就是输入和输出.由于程序和运行时数据是在内存中驻留,由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口. 比如你打开 ...
- VPU硬编码
平台是RK3066(福州瑞芯微公司),android 4.2.0,其实时VP8硬编码,与软件编码是ffpmeg,x264,xvid等软编码是有区别的.硬编码主要是依赖于硬件. 硬编码:通过调用Andr ...
- Django[pronounced dʒ] installation on windows
1.Install python, download python windows installer from http://www.python.org/download/ and do inst ...
- 【Oracle】RAC集群中的命令
数据库名称:racdb 节点名称:rac3.rac4 注:以下命令均在grid用户中执行 1.查看集群节点的状态: [grid@rac3 ~]$ crsctl check cluster [grid@ ...
- 使用光盘作为yum源安装ifconfig等网络命令
# mkdir -p /mnt/cdrom# 如果是光驱:mount -t iso9660 /dev/cdrom /mnt/cdrom/# 如果是ISO:mount -o loop /usr/loca ...
- Windows2012R2 时间同步设置
Windows2012R2里没有了internet时间,或者Internet时间无法同步成功,都可以尝试使用如下方法. 1.打开命令提示符, 输入:gpedit.msc,打开组策略管理器 2.执行上述 ...
- **PCL:嵌入VTK/QT显示(Code^_^)
中国人真是太不知道分享了,看看这个老外的博客,启发性链接. http://www.pcl-users.org/ 1. 这个是可用的源代码: 原文:I saw a thread with links t ...