poj 1979 简单的搜索
2011-04-28 15:57
267 查看
与迷宫问题类似 用BFS或者DFS都可以
BFS和DFS的简单区别在于 当产生新的结点时DFS先从新的结点开始拓展,而BFS从先产生该结点的结点开始拓展
题目大意:给定一组数据,其中'.'代表黑色瓦片,'#'代表红色瓦片,'@'为当前位置,从当前位置出发只能够经过黑色瓦片,求能经过的不重复的瓦片的最大数量
广度优先搜索:
#include<iostream>
#include<queue>
#define MAX 25
using namespace std;
int dirx[4] = {0,0,1,-1},diry[4] = {1,-1,0,0};
char maze[MAX][MAX];
int w,h;
int visited[MAX][MAX];
struct node{
int x,y;
int nextx,nexty;
};
node dir;
void init(){
int i,j;
for(i = 1;i <= h;i++)
for(j = 1;j <= w;j++){
cin>>maze[i][j];
if(maze[i][j] == '@'){
dir.x = i;
dir.y = j;
}
}
}
void BFS(){
node t,s;
int i,count = 1;
queue<node> que;
memset(visited,0,sizeof(visited));
que.push(dir);
visited[dir.x][dir.y] = 1;
while(!que.empty()){
t = que.front();
que.pop();
for(i = 0;i < 4;i++){
if(t.x+dirx[i] >= 1&&t.x+dirx[i] <= h&&t.y+diry[i] >= 1&&t.y+diry[i] <= w&&
maze[t.x+dirx[i]][t.y+diry[i]] == '.'&&visited[t.x+dirx[i]][t.y+diry[i]] == 0){
count++;
t.nextx = t.x+dirx[i];
t.nexty = t.y+diry[i];
s.x = t.nextx;
s.y = t.nexty;
visited[s.x][s.y] = 1;
que.push(s);
}
}
}
cout<<count<<endl;
}
int main(){
while(cin>>w>>h&&w&&h){
init();
BFS();
}
return 0;
}
深度优先搜索:
#include<iostream>
#define MAX 25
using namespace std;
int count,visited[MAX][MAX],h,w;
char maze[MAX][MAX];
int dirx[4] = {1,-1,0,0},diry[4] = {0,0,1,-1};
void DFS(int i,int j){
count++;
visited[i][j] = 1;
for(int k = 0;k < 4;k++){
int nextx = i+dirx[k];
int nexty = j+diry[k];
if(nextx >= 1&&nextx <= h&&nexty >= 1&&nexty <= w&&maze[nextx][nexty] == '.'&&visited[nextx][nexty] == 0)
DFS(nextx,nexty);
}
}
int main(){
while(cin>>w>>h&&w&&h){
int i,j,begin_i,begin_j;
memset(visited,0,sizeof(visited));
for(i = 1;i <= h;i++)
for(j = 1;j <= w;j++){
cin>>maze[i][j];
if(maze[i][j] == '@'){
begin_i = i;
begin_j = j;
}
}
count = 0;
DFS(begin_i,begin_j);
cout<<count<<endl;
}
return 0;
}
BFS和DFS的简单区别在于 当产生新的结点时DFS先从新的结点开始拓展,而BFS从先产生该结点的结点开始拓展
题目大意:给定一组数据,其中'.'代表黑色瓦片,'#'代表红色瓦片,'@'为当前位置,从当前位置出发只能够经过黑色瓦片,求能经过的不重复的瓦片的最大数量
广度优先搜索:
#include<iostream>
#include<queue>
#define MAX 25
using namespace std;
int dirx[4] = {0,0,1,-1},diry[4] = {1,-1,0,0};
char maze[MAX][MAX];
int w,h;
int visited[MAX][MAX];
struct node{
int x,y;
int nextx,nexty;
};
node dir;
void init(){
int i,j;
for(i = 1;i <= h;i++)
for(j = 1;j <= w;j++){
cin>>maze[i][j];
if(maze[i][j] == '@'){
dir.x = i;
dir.y = j;
}
}
}
void BFS(){
node t,s;
int i,count = 1;
queue<node> que;
memset(visited,0,sizeof(visited));
que.push(dir);
visited[dir.x][dir.y] = 1;
while(!que.empty()){
t = que.front();
que.pop();
for(i = 0;i < 4;i++){
if(t.x+dirx[i] >= 1&&t.x+dirx[i] <= h&&t.y+diry[i] >= 1&&t.y+diry[i] <= w&&
maze[t.x+dirx[i]][t.y+diry[i]] == '.'&&visited[t.x+dirx[i]][t.y+diry[i]] == 0){
count++;
t.nextx = t.x+dirx[i];
t.nexty = t.y+diry[i];
s.x = t.nextx;
s.y = t.nexty;
visited[s.x][s.y] = 1;
que.push(s);
}
}
}
cout<<count<<endl;
}
int main(){
while(cin>>w>>h&&w&&h){
init();
BFS();
}
return 0;
}
深度优先搜索:
#include<iostream>
#define MAX 25
using namespace std;
int count,visited[MAX][MAX],h,w;
char maze[MAX][MAX];
int dirx[4] = {1,-1,0,0},diry[4] = {0,0,1,-1};
void DFS(int i,int j){
count++;
visited[i][j] = 1;
for(int k = 0;k < 4;k++){
int nextx = i+dirx[k];
int nexty = j+diry[k];
if(nextx >= 1&&nextx <= h&&nexty >= 1&&nexty <= w&&maze[nextx][nexty] == '.'&&visited[nextx][nexty] == 0)
DFS(nextx,nexty);
}
}
int main(){
while(cin>>w>>h&&w&&h){
int i,j,begin_i,begin_j;
memset(visited,0,sizeof(visited));
for(i = 1;i <= h;i++)
for(j = 1;j <= w;j++){
cin>>maze[i][j];
if(maze[i][j] == '@'){
begin_i = i;
begin_j = j;
}
}
count = 0;
DFS(begin_i,begin_j);
cout<<count<<endl;
}
return 0;
}
相关文章推荐
- poj 1979 Red and Black (简单裸搜索)
- 简单搜索1979@POJ
- POJ 1426 Find The Multiple(kuangbin带你飞 专题一:简单搜索)
- POJ-1111 Image Perimeters 简单搜索
- [kuangbin带你飞]专题一 简单搜索F - Prime Path(POJ 3126)
- [kuangbin带你飞]专题一 简单搜索 C - Catch That Cow poj 3278
- poj 1426 Find The Multiple 简单搜索
- POJ-1426(Find The Multiple)--简单搜索
- POJ 1321棋盘问题(简单搜索)
- [kuangbin带你飞]专题1 简单搜索 C - Catch That Cow POJ - 3278
- poj 几道简单的搜索题(三)
- POJ 3278 Catch That Cow(kuangbin带你飞 专题一:简单搜索)
- poj1562 Oil Deposits 简单搜索,dfs和bfs都行,我用的bfs
- [kuangbin带你飞]专题一 简单搜索-D - Fliptile POJ - 3279
- POJ-1979 深度优先搜索DFS
- POJ 1979 简单dfs||bfs
- POJ 1321 棋盘问题(简单搜索)
- POJ 3984 迷宫问题(kuangbin带你飞 专题一:简单搜索)
- pku1979 简单搜索
- POJ 3279 Fliptile(kuangbin带你飞 专题一:简单搜索)