当前位置: 首页 > news >正文

CH2906 武士风度的牛(算竞进阶习题)

水。。。。。

直接bfs。。。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
int n, m, f[155][155];
char g[155][155];
pair<int, int> st, ed;
const int dx[] = {-2, -2, 2, 2, -1, -1, 1, 1};
const int dy[] = {-1, 1, -1, 1, -2, 2, -2, 2};

void init(){
    for(int i = 0; i < m; i ++){
        for(int j = 0; j < n; j ++){
            if(g[i][j] == 'K')
                st.first = i, st.second = j, g[i][j] = '.';
            if(g[i][j] == 'H')
                ed.first= i, ed.second = j, g[i][j] = '.';
        }
    }
}

bool inArea(int x, int y){
    return x >= 0 && x < m && y >= 0 && y < n && g[x][y] == '.';
}

int bfs(){
    memset(f, -1, sizeof f);
    queue<pair<int, int>> q;
    f[st.first][st.second] = 0;
    q.push(st);
    while(!q.empty()){
        int x = q.front().first, y = q.front().second; q.pop();
        for(int i = 0; i < 8; i ++){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(inArea(nx, ny) && f[nx][ny] == -1){
                f[nx][ny] = f[x][y] + 1;
                if(nx == ed.first && ny == ed.second)
                    return f[nx][ny];
                q.push(make_pair(nx, ny));
            }
        }
    }
    return -1;
}

int main(){

    n = read(), m = read();
    for(int i = 0; i < m; i ++) scanf("%s", g[i]);
    init();
    printf("%d\n", bfs());
    return 0;
}

转载于:https://www.cnblogs.com/onionQAQ/p/10542251.html

相关文章:

  • 2014年蓝桥杯部分题目与解答
  • 重拾 ObjC 自动释放池
  • 监听JS对象属性变化 Object.defineProperty Proxy 记录
  • 读ios开发有感——建立APP开发体系
  • 回归
  • Kubernetes — 重新认识Docker容器
  • 专业术语------扫盲
  • 实验1
  • nunjucks模版引擎入门
  • git flow常用命令
  • PHP实现多维数组按指定值排序
  • 高分笔记_括号匹配
  • 2018-2019-2 《网络对抗技术》Exp2 后门原理与应用 20165211
  • 每日 30 秒 ⏱ 谁敢与我一战
  • 用Python爬取王者农药英雄皮肤
  • Asm.js的简单介绍
  • CSS盒模型深入
  • in typeof instanceof ===这些运算符有什么作用
  • js正则,这点儿就够用了
  • leetcode讲解--894. All Possible Full Binary Trees
  • react 代码优化(一) ——事件处理
  • Spark RDD学习: aggregate函数
  • Spring Cloud中负载均衡器概览
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • Sublime text 3 3103 注册码
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 经典排序算法及其 Java 实现
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 力扣(LeetCode)22
  • 日剧·日综资源集合(建议收藏)
  • 微服务框架lagom
  • 一个完整Java Web项目背后的密码
  • 容器镜像
  • 我们雇佣了一只大猴子...
  • ​Linux·i2c驱动架构​
  • #if #elif #endif
  • $L^p$ 调和函数恒为零
  • (02)Hive SQL编译成MapReduce任务的过程
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (十七)Flask之大型项目目录结构示例【二扣蓝图】
  • (转载)虚函数剖析
  • *** 2003
  • ... 是什么 ?... 有什么用处?
  • .bashrc在哪里,alias妙用
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】
  • @DateTimeFormat 和 @JsonFormat 注解详解
  • @RequestMapping-占位符映射
  • [1525]字符统计2 (哈希)SDUT
  • [20140403]查询是否产生日志
  • [Angular] 笔记 7:模块