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 ...
随机推荐
- PHP采用301跳转方式防CC拦截
PHP采用301跳转方式防CC拦截 降低CC攻击的效果 <?php empty($_SERVER['HTTP_VIA']) or exit('Access Denied'); $second ...
- Python学习【第7篇】:Python之常用模块2
hashlib,configparser,logging模块 一.常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希 ...
- 洛谷——P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
- Spring整合Junit框架进行单元测试Demo
一.开发环境 eclipse版本:4.6.1 maven版本:3.3.3 junit版本:4.12 spring版本:4.1.5.RELEASE JDK版本:1.8.0_111 二.项目结构 图 三. ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- Alliances
树国是一个有n个城市的国家,城市编号为1∼n.连接这些城市的道路网络形如一棵树, 即任意两个城市之间有恰好一条路径.城市中有k个帮派,编号为1∼k.每个帮派会占据一些城市,以进行非法交易.有时帮派之间 ...
- 2015 编程之美初赛第一场 AC题
题目1 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, 2, …, n.树中有n - 1条边,任意两个节点间恰好有一条路 ...
- keras与sklearn的结合使用
keras与sklearn的结合使用 新建 模板 Fly Time: 2017-4-14 引言 代码 引言 众所周知,keras目前没有提供交叉验证的功能,我们要向使用交叉验证,就需要与sklearn ...
- Android自己定义组件系列【1】——自己定义View及ViewGroup
View类是ViewGroup的父类,ViewGroup具有View的全部特性.ViewGroup主要用来充当View的容器.将当中的View作为自己孩子,并对其进行管理.当然孩子也能够是ViewGr ...
- iOS - 社会化分享-微信分享,朋友圈分享
我仅仅做了文字和图片分享功能 1. TARGETS - Info - URL Types identifier -> weixin URL Schemes -> 应用id 2.在AppD ...