经典的BFS。

 #include <stdio.h>
#include <string.h> #define MAXNUM 105
#define MAXROW 105
#define MAXQUE 10005 char buf[MAXROW][MAXNUM];
char visit[MAXROW][MAXNUM]; typedef struct {
int x, y;
} pos_st; pos_st queue[MAXQUE];
int direct[][] = {{,-},{-,-},{-,},{-,},{,},{,},{,},{,-}};
int m, n;
int total; void bfs();
void search(); int main() {
int i; while (scanf("%d%d", &m, &n)!=EOF && (m||n)) {
getchar();
for (i=; i<m; ++i) {
gets(buf[i]);
}
bfs();
printf("%d\n", total);
} return ;
} void bfs() {
int i, j; total = ;
memset(visit, -, sizeof(visit)); for (i=; i<m; ++i)
for (j=; j<n; ++j)
if (buf[i][j]=='@' && visit[i][j]==-)
search(i, j);
} void search(int row, int col) {
int front, rear;
int x, y, newx, newy;
int i; front = rear = ;
total++;
visit[row][col] = total; if (visit[row][col] > ) {
queue[rear].x = row;
queue[rear].y = col;
rear++;
while (front != rear) {
x = queue[front].x;
y = queue[front].y;
front++;
for (i=; i<; ++i) {
newx = x+direct[i][];
newy = y+direct[i][];
if (newx>= && newx<m && newy>= && newy<n) {
if (buf[newx][newy] == '@' && visit[newx][newy]<) {
queue[rear].x = newx;
queue[rear].y = newy;
rear++;
visit[newx][newy] = total;
}
}
}
}
} }

最新文章

  1. 连接Mysql提示Can’t connect to local MySQL server through socket的解决方法
  2. 11、Linq的使用
  3. path入门 20141102-1405
  4. 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility
  5. ASP.NET MVC Global.cs - 应用程序事件
  6. ASCII字符对照表 不时之需
  7. paper 63 :函数比较:imfilter与fspecial
  8. URL重写的优缺点分析
  9. HDU5137 How Many Maos Does the Guanxi Worth(枚举+dijkstra)
  10. MySql每月增加一个分区以及查询所有分区
  11. C# IIS7.0+ Web.Config 配置Session过期时间
  12. Tournament ZOJ - 4063 (青岛区域赛 F 打表)
  13. python基础学习笔记(二)
  14. 卸载系统自动jdk
  15. php高并发,大流量
  16. 微信公众号支付-Common
  17. 清空mailq 队列里面的邮件
  18. 剑指offer五十九之按之字形顺序打印二叉树
  19. c#编写远程控制的核心被控端操作类
  20. ssh连接不上排查方法总结

热门文章

  1. wamp优化
  2. ios 经典错误
  3. Java实战之02Hibernate-03Session中的常用方法
  4. OpenJudge/Poj 1316 Self Numbers
  5. [转]select模型的一种技巧运用-libevent
  6. C# 读取快捷方式指向的文件
  7. 一台Ubuntu server上安装多实例MySQL
  8. 51nod1089最长回文子串V2
  9. PHP常见算法-面试篇(1)
  10. hdu 4300 Clairewd’s message KMP应用