《算法竞赛从入门到进阶》第四章 搜索技术 hdu1312 "Red and Black" BFS

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
char room[25][25];
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
int Wx,Hy,num;
//Wx,Hy:长宽的边界 ,num:从初始瓷砖到能到达的瓷砖的总数
#define CHECK(x,y) (x<Wx&&x>=0&&y>=0&&y<Hy)
//检查是否越界
struct node
{
    int x,y;
};
void BFS(int dx,int dy)
{
    queue<node> q;
    node start,next;
    start.x = dx;
    start.y = dy;
    q.push(start);//把当前节点入队
    while(!q.empty())
    {
        start = q.front();
        q.pop();
        for(int i=0;i<4;++i)
        {
            next.x=start.x+dir[i][0];
            next.y=start.y+dir[i][1];
            if(CHECK(next.x,next.y)&&room[next.x][next.y]==‘.‘)
            {
                room[next.x][next.y]=‘#‘;//标记一下当前节点已经走过了
                num++;//步数自增
                q.push(next);//将子节点入队
            }
        }
    }
}
int main()
{
    int x,y,dx,dy;
    //dx,dy: 人的站位
    while(cin>>Wx>>Hy)
    {
        if(Wx==0&&Hy==0)
        {
            break;
        }
        for(y=0;y<Hy;++y)
        {
            for(x=0;x<Wx;++x)
            {
                cin>>room[x][y];
                if(room[x][y]==‘@‘)
                {
                    dx=x;
                    dy=y;
                }
            }
        }

        num=1;
        BFS(dx,dy);
        cout<<num<<endl;
    }
    return 0;
 } 

原文地址:https://www.cnblogs.com/chrysanthemum/p/11823037.html

时间: 2024-08-01 02:45:17

《算法竞赛从入门到进阶》第四章 搜索技术 hdu1312 "Red and Black" BFS的相关文章

《算法竞赛从入门到进阶》第四章 搜索技术

递归打印全排列 #include<iostream> #include<algorithm> #include<ctime> #define Swap(a,b) {int temp=a;a=b;b=temp;} using namespace std; int data[]={1,2,3,4,5,6,7,8,9,10,32,15,18,33}; int num = 0; int Perm(int begin,int end) { int i; if(begin==end

算法竞赛_入门经典_刘汝佳__(2)

1,有几位数字 #include<stdio.h> int main_2_1_digit(){ int n; while(scanf("%d",&n)){ int count = 0; if(n==0) count = 1; while(n){ count++; n/=10; } printf("%d\n",count); } return 0; } 2,三位数的三个数字 #include<stdio.h> int main_2_2_

SpringCloud从入门到进阶(四)——使用SpringBoot搭建微服务

内容 SpringBoot整合SpringCloud的Eureka.Zuul等组件,快速实现简单易懂且具有服务熔断.负载均衡的分布式架构1.0,体验微服务的魅力. 版本 IDE:IDEA 2017.2.2 x64 JDK:1.8.0_171 manve:3.3.3 SpringBoot:1.5.9.RELEASE SpringCloud:Dalston.SR1 适合人群 ?Java开发人员 说明 转载请说明出处:SpringCloud从入门到进阶(四)--使用SpringBoot搭建微服务 参考

SpringCloud从入门到进阶(四)——生产环境下Eureka的完全分布式部署

内容 由于前两节的内容我们知道,开启了preferIpAddress后,Eureka的伪分布式部署会提示replica不可用.这一节我们讲解如何在生产环境下部署完全分布式的Eureka集群,确保开启了preferIpAddress后replica的可用性. 版本 IDE:IDEA 2017.2.2 x64 JDK:1.8.0_171 manve:3.3.3 SpringBoot:1.5.9.RELEASE SpringCloud:Dalston.SR1 适合人群 Java开发人员 节点信息: 节

ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区

原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一个全面的了解,接下来在本章中,将通过一个案例来熟悉ArcGIS for Desktop的使用,从解决问题的过程中,逐渐适应ArcGIS桌面的界面和操作方式. 本章的练习数据是一个住宅小区的简单平面示意图,需要在已有的基础上把楼房的轮廓补充完整,并加以整饰,完成一幅地图. 1.1 打开地图文档并浏览

算法导论笔记——第十二~十四章 数据结构(二)树

第十二章 二叉搜索树 >=左子树的所有key,<=右子树的所有key 在一棵高度为h的二叉搜索树上,动态集合上的操作SEARCH,MINIMUM,MAXIMUM,SUCCESSOR,PREDECESSOR,INSERT和DELETE可以在O(h)时间内完成. h>=(lgn向下取整) 和快速排序算法一样,其平均性能更接近于最好情形. 随机构建二叉搜索树期望高度为O(lgn). 各种操作请自行查阅. 第十三章 红黑树 是一种(近似)平衡的二叉搜索树.可以保证在最坏情况下基本动态集合操作的时

SpringMVC从入门到精通之第四章

第一个知识点:@Controller注解,用于标识这个类是一个后端控制器(类似struts中的action),主要作用就是接受页面的参数,转发页面.中间的业务逻辑是调用业务类处理的这个就是MVC设计模式的思路.我们来看下这个注解的源码: package org.springframework.stereotype; import java.lang.annotation.Annotation; import java.lang.annotation.Documented; import java

《算法竞赛经典入门完整版》习题

习题2-3 韩信点兵 #include<stdio.h>#include<time.h>int main(){    int a,b,c,S = 10;    scanf("%d",&a);    scanf("%d",&b);    scanf("%d",&c);    while(!(S%3 == a && S%5 == b && S%7 == c &&a

算法竞赛与入门经典---P66 [UVA 10635] Prince and Princess

Prince and PrincessInput: Standard Input Output: Standard Output Time Limit: 3 Seconds In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below: Prince stands in square 1, ma