2015 多校赛 第四场 1009 (hdu 5335)
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.
题意:给出一幅图,起点在左上角终点在左下角。沿途走过的数字连起来为最后所得的二进制数,设法令该数最小,输出之。
思路:
起点为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)的更多相关文章
- 2015 多校赛 第四场 1010 (hdu 5336)
Problem Description XYZ is playing an interesting game called "drops". It is played on a r ...
- 2015 多校赛 第五场 1010 (hdu 5352)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5352 看题看得心好累. 题目大意: 给出 n 个点,依次执行 m 次操作:输入“1 x”时,表示将与 ...
- 2015 多校赛 第七场 1011 (hdu 5379)
题意:给定一棵树,树上有 n 个节点.问有多少种方案,使得在每个节点上依次放置数 1~n 后,每个节点的儿子节点上的数连续(比如 1 为根,有1-2,1-3,1-4,则令2,3,4上的数连续),每个子 ...
- 2015 多校赛 第五场 1006 (hdu 5348)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题目大意:给出一幅无向图,问是否存在一种方案,使得给每条边赋予方向后,每个点的入度与出度之差小于 ...
- 2015 多校赛 第三场 1002 (hdu 5317)
Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more i ...
- hdu 5328 Problem Killer(杭电多校赛第四场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5328 题目大意:找到连续的最长的等差数列or等比数列. 解题思路:1.等差等比的性质有很多.其中比较重 ...
- 【杂题总汇】HDU多校赛第十场 Videos
[HDU2018多校赛第十场]Videos 最后一场比赛也结束了…… +HDU传送门+ ◇ 题目 <简要翻译> 有n个人以及m部电影,每个人都有一个快乐值.每场电影都有它的开始.结束时间和 ...
- NOI.AC NOIP模拟赛 第四场 补记
NOI.AC NOIP模拟赛 第四场 补记 子图 题目大意: 一张\(n(n\le5\times10^5)\)个点,\(m(m\le5\times10^5)\)条边的无向图.删去第\(i\)条边需要\ ...
- hdu5379||2015多校联合第7场1011 树形统计
pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is ...
随机推荐
- inline-block兼容IE7
{ display:inline-block; *display:inblock; *zoom:1 }
- mybatis 简单的入门实例
第一步:添加mybaties的架包 第二步:配置mybaties的文件 <?xml version="1.0" encoding="UTF-8" ?> ...
- final关键字用法
Java关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量. final类不能被继承,没有子类,final类中的方法默认是final的. final方法 ...
- 刽子手游戏(Hangman Judge, UVa 489)
刽子手游戏其实是一款猜单词游戏,游戏规则是这样的:计算机想一个单词让你猜,你每次可以猜一个字母.如果单词里有那个字母,所有该字母会显示出来:如果没有那个字母,则计算机会在一幅“刽子手”画上填一笔.这幅 ...
- java面试题(转)
1.面向对象的特征有哪些方面?答:面向对象的特征主要有以下几个方面:- 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注这些 ...
- Python基础-判断闰年
输入一个年份,判断该年份是否是闰年并输出结果.求它是否是闰年?要求:能被4整除不能被100整除或者能被400整除. y=input('请输入年份:') if(y%4==0 and y%100 != 0 ...
- 2.1 Java开发工具包
Java专业术语 术语名 缩写 ...
- SCI 论文金句
SCI 不会写?其实英语基础好一点,文献多看一点,多写写自然就能自己写出来了.当然,你肯定会说英语真的好难,好吧,就知道你们懒得学英语了.我给你们整理了一套万能模板,涵盖了论文不同部分的常用句型. 摘 ...
- 洛谷 P1348 Couple number
题目描述 任何一个整数N都能表示成另外两个整数a和b的平方差吗?如果能,那么这个数N就叫做Couple number.你的工作就是判断一个数N是不是Couple number. 输入输出格式 输入格式 ...
- TCP学习(3)--TCP释放连接的过程(四次挥手)
一.TCP释放连接的过程(四次挥手) TCP释放连接的步骤例如以下图所看到的. 如今如果clientA和server端B都处于数据传送状态. TCP连接断开的过程例如以下: 1.clien ...