hdu5336 Walk Out
题意是:入口:地图的左上角,出口,地图的右上角,求所经过的路径的二进制数最小
照着题解敲了一遍
思路是:首先 二进制 的 位数 越小 越好,其次 二进制的前缀零越多 表示 值越小
首先 在前缀是0的情况下走到最远(最远的定义是 当前坐标是 i , j 则 在保证 位数越小的情况下 则 还剩下 n-i + m-j 的路要走)
剩下的路 必须 向 右走 或者向 下 走,然后 在 其中 选取 0 尽量 多的 路
代码:非原创!!!(from 多校题解)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue> using namespace std;
const int MAXN = +;
bool vis[MAXN][MAXN];
char graph[MAXN][MAXN];
int n,m;
int dr[] = {,,,-};
int dc[] = {,-,,};
struct Node{
int x,y;
}; void bfs(){
memset(vis,,sizeof(vis));
vis[][] = ;
queue<Node>q;
Node tmp;
tmp.x = ,tmp.y = ;
q.push(tmp);
int mx = ;
while(!q.empty()){
tmp = q.front();
q.pop();
if(graph[tmp.x][tmp.y] > ''){
continue;
}
for(int i = ;i < ;i++){
int xx = tmp.x + dr[i];
int yy = tmp.y + dc[i];
if(xx<||yy<||xx>n||yy>m || vis[xx][yy]){
continue;
}
// cout << "x = " << tmp.x << " y = " << tmp.y << endl;
Node tt;
tt.x = xx;
tt.y = yy;
vis[xx][yy] = ;
mx = max(mx,xx+yy);
// cout << "x = " << xx << " y = " << yy << endl << endl;;
q.push(tt);
}
}
if(vis[n][m] && graph[n][m] == ''){
printf("0\n");
return;
}
printf("");
for(int i = mx;i < m+n;i++){
char mi = '';
for(int j = ;j <= n;j++){
if(i-j > && i-j <= m && vis[j][i-j]){
mi = min(mi,graph[j+][i-j]); //向右走
mi = min(mi,graph[j][i-j+]);//向下走
}
}
printf("%c",mi);
for(int j = ;j <= n;j++){
if(i-j > && i-j <= m && vis[j][i-j]){
if(mi == graph[j+][i-j]){
vis[j+][i-j] = ;
}
if(mi == graph[j][i-j+]){
vis[j][i-j+] = ;
}
}
}
}
printf("\n");
}
int main(){
// freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i = ;i <= n;i++){
scanf("%s",graph[i]+);
}
for(int i = ;i <= n+;i++){
graph[i][] = '';
graph[i][m+] = '';
}
for(int i = ;i < m+;i++){
graph[][i] = '';
graph[n+][i] = '';
}
bfs();
}
return ;
}
hdu5336 Walk Out的更多相关文章
- python os.walk()
os.walk()返回三个参数:os.walk(dirpath,dirnames,filenames) for dirpath,dirnames,filenames in os.walk(): 返回d ...
- LYDSY模拟赛day1 Walk
/* 依旧考虑新增 2^20 个点. i 只需要向 i 去掉某一位的 1 的点连边. 这样一来图的边数就被压缩到了 20 · 2^20 + 2n + m,然后 BFS 求出 1 到每个点的最短路即可. ...
- How Google TestsSoftware - Crawl, walk, run.
One of the key ways Google achievesgood results with fewer testers than many companies is that we ra ...
- poj[3093]Margaritas On River Walk
Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...
- os.walk()
os.walk() 方法用于通过在目录树种游走输出在目录中的文件名,向上或者向下. walk()方法语法格式如下: os.walk(top[, topdown=True[, onerror=None[ ...
- 精品素材:WALK & RIDE 单页网站模板下载
今天,很高兴能向大家分享一个响应式的,简约风格的 HTML5 单页网站模板.Walk & Ride 这款单页网站模板是现代风格的网页模板,简洁干净,像素完美,特别适合用于推广移动 APP 应用 ...
- 股票投资组合-前进优化方法(Walk forward optimization)
code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...
- Go Walk教程 - 流程控制( switch)
Go的 switch 非常灵活,表达式不必是常量或整数,执行的过程从上至下,直到找到匹配项,不要break: var score =98 var result string switch score/ ...
- [py]os.walk爬目录&sys.argv灵活获取参数
1, 遍历目录 os.walk('/tmp') os.next() 2,sys.argv ######################################## py@lanny:~/t ...
随机推荐
- haproxy 访问www.zjdev.com 自动跳转到appserver_8001 对应的nginx
# # acl zjdev_7_req hdr_beg(host) -i www.zjdev.com # use_backend appserver_8001 if zjdev_7_req
- kinect for windows - 初认识
kinect是微软开发的一种计算机输入设备,原来只是用于xbox,kinect负责捕捉用户的动作,让xbox游戏做出相应的反应.很快大家对此非常有兴趣,因此有些geek和组织为kinect开发了驱动和 ...
- 11997 - K Smallest Sums(优先队列)
11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...
- ARP地址解析协议
ARP地址解析协议:为IP地址到相应的硬件地址之间提供动态映射. 下面图为例说明地址解析协议的详细工作流程. 结合图中的序号进行说明. client通过FTP协议连接server时,首先解析器会把主机 ...
- iOS MBProgressHUD 之带底板的加载提示
文章来自:http://blog.csdn.net/ryantang03/article/details/7877120 MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单 ...
- ZOJ2971 Give Me the Number 【模拟】
这道题目使用Map. 然后一次性遍历下来即可. QAQ 注意初始化的时候小心点不要错.. Source Code: //#pragma comment(linker, "/STACK:167 ...
- Ural 1313 - Some Words about Sport
Ural doctors worry about the health of their youth very much. Special investigations showed that a l ...
- 分享,iOS国家手机区号代码.plist
APP注册需要手机号码的时候,如果有在其他国家的时候需要填写手机区号 一份有国家名字和区号的plist 参照微信注册的时候 格式是 <Array> <Array> <Ar ...
- 模拟红外协议C程序——接收模块
目的:方便程序的调试,提供效率,减少工作累,可以不在线调试编程时显示实时数据,特别产品不带显示的或者MCU是OPT的,有很大的帮助. 过程:将要看的数据发送出来,另一个板(一个带有显示的就OK了,显示 ...
- Eclipse下执行main函数报java.lang.NoClassDefFoundError的解决
今天执行eclipse下的一个java类,无论run还是debug,都报java.lang.NoClassDefFoundError.而且把main中函数都注释掉,执行还是报一样的错. 检查了一下这个 ...