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