HDU 5335——Walk Out——————【贪心】
Walk Out
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1292 Accepted Submission(s): 239
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.
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.
#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
const int INF=0x3f3f3f3f;
int vis[maxn][maxn];
int Map[maxn][maxn];
int way[2*maxn];
int f[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int n,m,k;
struct NODE{
int x,y;
};
queue<NODE>Q;
stack<NODE>S;
int BFS(){
NODE st,tmp;
while(!Q.empty()){
st=Q.front();
Q.pop();
int xx,yy;
for(int i=0;i<4;i++){
xx=st.x+f[i][0];
yy=st.y+f[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(!vis[xx][yy])&&Map[xx][yy]==0){
if(xx==n&&yy==m)
return -1;
tmp.x=xx,tmp.y=yy;
vis[xx][yy]=1;
Q.push(tmp);
}
}
}
}
bool check(int x,int y){
int xx,yy;
for(int i=0;i<4;i++){
xx=x+f[i][0];
yy=y+f[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy]==1){
return true;
}
}
return false;
}
void Find1(int typ){
while(!S.empty())
S.pop();
while(!Q.empty())
Q.pop();
int maxs=-INF;
NODE st,tmp; if(typ==0){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(Map[i][j]==1){
if(check(i,j)){ if(i+j>maxs){
maxs=i+j;
}
st.x=i,st.y=j;
vis[i][j]=2;
S.push(st);
}
}
}
}
}else{
st.x=1,st.y=1;
vis[1][1]=2;
maxs=2;
S.push(st);
}
if(S.empty()==0)
way[k++]=1;
while(!S.empty()){
st=S.top();
S.pop();
if(st.x+st.y==maxs){
Q.push(st);
}
}
while(1){
int flag=0;
while(!Q.empty()){
st=Q.front();
Q.pop();
if(st.x==n&&st.y==m){
return ;
}
int xx,yy;
for(int i=0;i<2;i++){
xx=st.x+f[i][0];
yy=st.y+f[i][1];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(!vis[xx][yy])){
if(Map[xx][yy]==0){
flag=1;
}
tmp.x=xx,tmp.y=yy;
vis[xx][yy]=2;
S.push(tmp);
}
}
}
if(flag==1){
way[k++]=0;
while(!S.empty()){
st=S.top();
S.pop();
if(Map[st.x][st.y]==0){
Q.push(st);
}
}
}else{
way[k++]=1;
while(!S.empty()){
st=S.top();
S.pop();
Q.push(st);
}
}
}
}
int main(){
int t;
char str[1020];
scanf("%d",&t);
while(t--){
k=0;
memset(vis,0,sizeof(vis));
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",str+1);
for(int j=1;j<=m;j++){
Map[i][j]=str[j]-'0';
}
}
if(n==m&&n==1&&Map[1][1]==0){
printf("0\n");continue;
}
if(Map[1][1]==0){
NODE st;
st.x=1,st.y=1;
vis[1][1]=1;
while(!Q.empty())
Q.pop();
Q.push(st);
if(BFS()==-1) {
printf("0\n");
continue;
}
Find1(0);
}
else{
Find1(1);
}
for(int i=0;i<k;i++){
printf("%d",way[i]);
}printf("\n");
}
return 0;
}
/*
50
3 3
001
111
101 55
1 2
01 */
HDU 5335——Walk Out——————【贪心】的更多相关文章
- hdu 5335 Walk Out (搜索)
题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...
- HDU 5335 Walk Out BFS 比较坑
H - H Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 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 (2015 Multi-University Training Contest 4)
Walk Out Time Limit: 2000/10 ...
- HDU 5335 Walk Out
题意:在一个只有0和1的矩阵里,从左上角走到右下角, 每次可以向四个方向走,每个路径都是一个二进制数,求所有路径中最小的二进制数. 解法:先bfs求从起点能走到离终点最近的0,那么从这个点起只向下或向 ...
- HDU 5335 Walk Out(多校)
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- 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 (BFS,技巧)
题意:有一个n*m的矩阵,每个格子中有一个数字,或为0,或为1.有个人要从(1,1)到达(n,m),要求所走过的格子中的数字按先后顺序串起来后,用二进制的判断大小方法,让这个数字最小.前缀0不需要输出 ...
- hdu 5335 Walk Out(bfs+斜行递推) 2015 Multi-University Training Contest 4
题意—— 一个n*m的地图,从左上角走到右下角. 这个地图是一个01串,要求我们行走的路径形成的01串最小. 注意,串中最左端的0全部可以忽略,除非是一个0串,此时输出0. 例: 3 3 001 11 ...
随机推荐
- c#对文件的读写
最近需要对一个文件进行数量的分割,因为数据量庞大,所以就想到了通过写程序来处理.将代码贴出来以备以后使用. //读取文件的内容 放置于StringBuilder 中 StreamReader sr = ...
- css css3新特性
css css3新特性 一.css3是什么? 我不喜欢把已有的概念从一个地方抄到另一个地方,还是喜欢如下方式. 参考百度百科: http://baike.baidu.com/link?url=z2V ...
- POJO 与 JavaBean 的区别 !
$说明: POJO :全称(Plain Old Java Object)翻译为“普通旧Java对象” 通俗理解为“一个简单的java对象”. JavaBean: 是一种JAVA语言写成的可重用组件,是 ...
- Github加载慢,显示不完整问题解决
问题: 在访问Github网站的时候,可能会遇到网站响应超时,图片加载不出,排版错误等情况(大部分情况下是可以正常打开的). 解决方法: 修改 C:\Windows\System32\drivers\ ...
- 机器学习基石笔记:11 Linear Models for Classification、LC vs LinReg vs LogReg、OVA、OVO
原文地址:https://www.jianshu.com/p/6f86290e70f9 一.二元分类的线性模型 线性回归后的参数值常用于PLA/PA/Logistic Regression的参数初始化 ...
- 洛谷P4462 [CQOI2018]异或序列(莫队)
打广告->[这里](https://www.cnblogs.com/bztMinamoto/p/9538115.html) 我蠢了…… 如果$a_{l} xor ...a_{r}=k$,那么只要 ...
- JBOSS在win7环境下启动run.bat无反应
今天从隔壁机器拷贝了一份Jboss,却发现启动无任何反应. 仔细对比了jdk jboss的各项参数发现都是相同,无奈之下,检查run.bat文件 发现时在此句出现前后 无反应: "%JAVA ...
- CI框架源码学习笔记3——Log.php
上一节说完了Common.php,然而跟代码打交道总是免不了日志记录,所以这一节我们说说Log.php文件. 先看看类里面的几个属性, protected $_log_path; 日志路径 prot ...
- Ubuntu安装SHH服务
1.打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码"--> ...
- 简单理解php的socket连接
socket建立套接的过程图: 首先了解socket 几个主要函数: socket的关键函数1: socket_create($net参数1,$stream参数2,$protocol参数3) 作用:创 ...