1562 poj bfs

#include<iostream>
using namespace std;
#include<cstring>
#include<queue>
int used[105][105];
int a[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
char s[105][105];
int n,m;
struct node
{ int x,y;};
void bfs(node k)
{ node t1,t2;
queue<node>p;
memset(used,0,sizeof(used));
p.push(k);
used[k.x][k.y]=1;
s[k.x][k.y]=‘*‘;
while(!p.empty())
{ t1=p.front();
p.pop();
for(int i=0;i<8;i++)
{ t2.x=t1.x+a[i][0]; t2.y=t1.y+a[i][1];
if(t2.x>=0&&t2.x<n&&t2.y>=0&&t2.y<m&&s[t2.x][t2.y]==‘@‘&&0==used[t2.x][t2.y])
{ p.push(t2);
used[t2.x][t2.y]=1;
s[t2.x][t2.y]=‘*‘;
}
}
}
}
int main()
{ int i,j,sum;
node k;
while(cin>>n>>m)
{ if(n==0&&m==0) break;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
cin>>s[i][j];
sum=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(s[i][j]==‘@‘)
{ sum++;
k.x=i; k.y=j;
bfs(k);
}
cout<<sum<<endl;
}
return 0;
}

1562 poj bfs

时间: 2024-10-12 19:34:42

1562 poj bfs的相关文章

poj 1562 简单 bfs

// 简单 bfs #include <iostream>#include<fstream>using namespace std; char map[110][110];int flag[110][110];int qu[11000][2],qe,qs,m,n;int add[8][2]={-1,-1,  -1,0, -1,1,   0,-1,  0,1,  1,-1,    1,0,   1,1 }; void bfs(int r,int c){    int i,tr,tc;

1562 poj dfs

#include<stdio.h> char grid[101][101];int n, m;int dir[8][2] = {    {-1, -1}, {-1,  0},    {-1,  1}, { 0,  1},    { 1,  1}, { 1,  0},    { 1, -1}, { 0, -1}}; void dfs(int x, int y){    int a, b;    grid[x][y] = '*';    for(int i = 0; i < 8; i++)

(简单) POJ 1562 Oil Deposits,BFS。

Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

poj 1324 状态压缩+bfs

http://poj.org/problem?id=1324 Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17042   Accepted: 4065 Description During winter, the most hungry and severe time, Holedox sleeps in its lair. When spring comes, Holedox wakes

POJ - 3414 Pots (BFS+路径记录)

题目链接:http://poj.org/problem?id=3414 题意:两个空杯子倒水,使得任意一个杯子中的水量达到题目要求,有6种操作:装满a,装满b,倒掉a,倒掉b,a->b,b->a. 题解:模拟一下操作,就好了. 1 #include <queue> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7

POJ - 2251 Dungeon Master(三维BFS)

题目链接:http://poj.org/problem?id=2251 题意:三维BFS. 题解:大水题,只不过多加了两个方向 1 //poj2251 2 #include <queue> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 int sx,sy,sz,ex,ey,ez,L,R,C; 8 const int INF=

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

poj 1324 Holedox Moving A*算法对bfs的优化

题意: 迷宫里有一条贪食蛇,求它的蛇头到迷宫左上角最少要多少步. 分析: 关键是将蛇的状态压缩编码,然后bfs,超时就改A*,这题有类似最短路径的性质,A*发现节点重复后不需要更新直接舍弃即可. 代码: //poj 1324 //sep9 #include <iostream> #include <algorithm> #include <queue> using namespace std; struct state { int x[10],y[10]; }; str

POJ 2312 Battle City(优先队列+BFS)

题目链接:http://poj.org/problem?id=2312 Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7085   Accepted: 2390 Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often pl