hdu 1312
题意:“@”为起点,“.”为路,求可以走的格子有多少个(包括起点)
水题 bfs搜一发
思路:只有可以走的节点才能进入队列,所以每次出队列时ans+1就可以了(没有退出条件,所有可进入的节点都要搜索)
#include "map"
#include "queue"
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "iostream"
#include "algorithm"
#define abs(x) x > 0 ? x : -x
#define max(a,b) a > b ? a : b
#define min(a,b) a < b ? a : b using namespace std; int d[][] = {{,},{,},{,-},{-,}};
bool Map[][],vis[][],f[][];
int n,m,ans; struct Node
{
int xx,yy;
}; void Bfs(int x,int y)
{
memset(vis,,sizeof(vis));
memset(f,,sizeof(f)); Node now,next;
queue<Node>Q; now.xx = x;
now.yy = y;
vis[x][y] = ;
f[x][y] = ;
ans = ; Q.push(now); while(!Q.empty())
{
now = Q.front();
Q.pop(); if(Map[now.xx][now.yy])
ans++; for(int i=; i<; i++)
{
next.xx = now.xx + d[i][];
next.yy = now.yy + d[i][]; if(Map[next.xx][next.yy] && !vis[next.xx][next.yy])
{
vis[next.xx][next.yy] = ;
Q.push(next);
}
}
}
printf("%d\n",ans);
} int main()
{
int i,j,x,y;
char c;
while(scanf("%d%d",&n,&m)&&(n||m))
{
memset(Map,,sizeof(Map));
for(i=; i<=m; i++)
{
getchar();
for(j=; j<=n; j++)
{
scanf("%c",&c);
if(c=='#')
Map[i][j] = ;
if(c=='.')
Map[i][j] = ;
if(c=='@')
{
x=i,y=j;
Map[i][j] = ;
}
}
} Bfs(x,y);
}
return ;
}
hdu 1312的更多相关文章
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312 Red and Black --- 入门搜索 DFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)
题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/100 ...
- HDU 1312 Red and Black (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU 1312 Red and Black(最简单也是最经典的搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
- HDU 1312 Red and Black(经典DFS)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...
- HDU 1312 Red and Black (DFS & BFS)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...
随机推荐
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- ios录音
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...
- util.js
轻量基础库.方法库 用时可直接拷贝 拆卸式使用 适用于mobile端简单页面 适用于PC简单页面 基于node.php等多种构建方法 (function(M){ /** * 初始化Ajax请求 * @ ...
- RMI的概念
RMI(Remote Method Invocation)远程方法调用是一种计算机之间利用远程对象互相调用实现双方通讯的一种通讯机制.使用这种机制,某一台计算机上的对象可以调用另外一台计算机上的对象来 ...
- json数据实际应用
JSON序列化输出 var xiaoming = { name: '小明', age: 14, gender: true, height: 1.65, grade: null, 'middle-sch ...
- js从字符串中提取身份证号,连续18位数字
<!DOCTYPE html> <html> <head> <title>提取身份证号</title> <meta charset=& ...
- 遍历table指定name的td
$("td[name='rates']").each(function () { var temp = $(this).text().substr(0,$(this).text() ...
- Storm TimeCacheMap RotatingMap源码分析
TimeCacheMap是Twitter Storm里面一个类, Storm使用它来保存那些最近活跃的对象,并且可以自动删除那些已经过期的对象. 不过在storm0.8之后TimeCacheMap被弃 ...
- js点击a链接弹出alert对话框
代码 <html> <head> <meta charset="utf-8"> <meta name="generator&qu ...
- Shell 编程基础之基本语法结构汇总
一.条件语句 简单条件 if [ condition ]; then # 当 condition 成立时,执行内容: fi # 将 if 反过来写,fi 结束 if 之意 复杂条件 if [ cond ...