题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1312

题目大意:问迷宫中有多少个点被访问。

解题思路

DFS肯定能水过去的。这里就拍了一下BFS。

然后发现自己BFS访问标记有问题,导致某些点被重复访问了。

赶紧改了一下。

#include "cstdio"
#include "queue"
#include "string"
#include "cstring"
#include "iostream"
using namespace std;
int n,m,sx,sy,map[][],dir[][]={-,,,,,-,,},cnt;
bool vis[][];
struct status
{
int x,y;
status(int x,int y):x(x),y(y) {}
};
void bfs(int x,int y)
{
queue<status> Q;
Q.push(status(sx,sy));
vis[sx][sy]=true;
while(!Q.empty())
{
cnt++;
status t=Q.front();Q.pop();
for(int s=;s<;s++)
{
int X=t.x+dir[s][],Y=t.y+dir[s][];
if(vis[X][Y]||X<||X>n||Y<||Y>m||!map[X][Y]) continue;
vis[X][Y]=true;
Q.push(status(X,Y));
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
string tt;
while(cin>>m>>n&&n)
{
cnt=;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++)
{
if(tt[j]=='#') map[i][j+]=;
if(tt[j]=='.') map[i][j+]=;
if(tt[j]=='@') {map[i][j+]=;sx=i;sy=j+;}
}
}
bfs(sx,sy);
cout<<cnt<<endl;
} }
1869483 2014-10-14 14:49:59 Accepted 1312 15MS 292K 1246 B C++ Physcal

HDU 1312 (BFS搜索模板题)的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. HDU 1251 Trie树模板题

    1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...

  3. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  4. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  5. 【网络流#1】hdu 3549 - 最大流模板题

    因为坑了无数次队友 要开始学习网络流了,先从基础的开始,嗯~ 这道题是最大流的模板题,用来测试模板好啦~ Edmonds_Karp模板 with 前向星 时间复杂度o(V*E^2) #include& ...

  6. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  7. HDU 2544 最短路(模板题)

    求1到N的最短路径,模板题,以1为源点,用dijkstra算法(可以用优先级队列优化) #include <iostream> #include <algorithm> #in ...

  8. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

  9. HDU 4825 Xor Sum (模板题)【01字典树】

    <题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...

随机推荐

  1. 添加删除一个controller

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) rails generate controller Users rails destroy cont ...

  2. poj1789 Truck History

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20768   Accepted: 8045 De ...

  3. Android程序启动程序与页面的跳转

    package login; import com.example.login.R; import android.app.Activity; import android.content.Inten ...

  4. What is the difference between provider network and self-service network in OpenStack?

    "self-service networking" allows users to create their own virtual networks, subnets, rout ...

  5. iOS 获得指定文件夹下的指定格式文件

    这个容易忘记,然后只能用些自己写的长代码代替了....这里做个备忘 主要用到NSFileManager的 contentsOfDirectoryAtPath:error: 和 NSArray的 pat ...

  6. Java for LeetCode 042 Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. tcp连接管理

    [root@ok etc]# cat /proc/sys/net/core/netdev_max_backlog 每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目 ...

  8. mysql 主主复制(双主复制)+ 配置KEEPALIVED实现热备

    binlog-do-db和replicate-do-db表示需要同步的数据库 binlog-ignore-db和replicate-ignore-db表示不需要同步的数据库 云端服务器为master配 ...

  9. grep -n 显示行号

    [root@86 ~]# grep -n "StartDiscoverers" /usr/local/zabbix/etc/zabbix_server.conf 176:### O ...

  10. Linux运维操作

    http://www.it165.net/os/html/201204/1909.html https://i.cnblogs.com/EditPosts.aspx?opt=1 http://www. ...