POJ 1979--DFS or BFS(水)

题意:。表示能走,#表示不能走,@为起点,求从起点出发能走的点的数目

分析:简单的BFS或DFS

BFS代码:

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int n,m;
char a[100][100];
int d[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int cnt;
int sx,sy;
struct h{
	int x,y;
};
queue<h> q;
void bfs()
{
	h tmp;
	while(!q.empty()){
		tmp=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			h tmp2;
			tmp2.x=tmp.x+d[i][0];
			tmp2.y=tmp.y+d[i][1];
			if(tmp2.x>=0&&tmp2.x<m&&tmp2.y>=0&&tmp2.y<n&&a[tmp2.x][tmp2.y]!='#'){
				cnt++;
				a[tmp2.x][tmp2.y]='#';
				q.push(tmp2);
			}
		}
	}
}
int main()
{
	while(cin>>n>>m){
		if(!n&&!m) break;
		while(!q.empty()) q.pop();
		for(int i=0;i<m;i++)
		for(int j=0;j<n;j++){
			cin>>a[i][j];
			if(a[i][j]=='@') sx=i,sy=j;
		}
		a[sx][sy]='#';
		h tmp;
		tmp.x=sx,tmp.y=sy;
		cnt=1;
		q.push(tmp);
		bfs();
		cout<<cnt<<endl;
	}
}

DFS代码:

#include<iostream>
#include<cstring>
using namespace std;
int n,m;
char a[100][100];
int d[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int cnt;
int sx,sy;
void dfs(int x,int y)
{
	if(a[x][y]=='#') return;
	a[x][y]='#';
	cnt++;
	for(int i=0;i<4;i++){
		int dx=x+d[i][0];
		int dy=y+d[i][1];
		if(dx>=0&&dx<m&&dy>=0&&dy<n){
			dfs(dx,dy);
		}
}
}
int main()
{
	while(cin>>n>>m){
		if(!n&&!m) break;
		for(int i=0;i<m;i++)
		for(int j=0;j<n;j++){
			cin>>a[i][j];
			if(a[i][j]=='@') sx=i,sy=j;
		}
		cnt=0;
		dfs(sx,sy);
		cout<<cnt<<endl;
	}
}
时间: 2024-11-10 13:55:37

POJ 1979--DFS or BFS(水)的相关文章

poj 1979 dfs水题

// 练练水题,夯实基础吧 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib

poj 1979 dfs

水过,注意边界不能超出. #include <iostream> using namespace std; int n, m, sx, sy, dir[4][2] = {0, -1, 0, 1, 1, 0, -1, 0}, count; char diagram[23][23]; void get_diagram(void) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> diag

poj 3083 dfs,bfs

Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3083 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander throu

《挑战》2.1 POJ POJ 1979 Red and Black (简单的DFS)

B - Red and Black Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1979 Description There is a rectangular room, covered with square tiles. Each tile is col

POJ 1573 Robot Motion【是搜索,就不要纠结是DFS还是BFS】

Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12845   Accepted: 6234 Description A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in

POJ 1426 Find The Multiple(DFS,BFS)

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100

POJ 1979 POJ 3009 AOJ 0033 AOJ 0118 [搜索类题目][0033贪心模拟]

/** POJ 1979 BFS */ #include <stdio.h> #include <string.h> #include <iostream> #include <queue> using namespace std; const int N = 20 + 5; int mp[N][N]; int sx,sy; int n, m; int vis[3000]; int dirx[] = {0, 1, 0, -1}; int diry[] = {

POJ 1979 Red and Black【深度优先搜索】

题目链接:http://poj.org/problem?id=1979 题目大意:一个矩形的房间地板被分为w*h个小块,每一个小块不是红的就是黑的,你首先站在一个黑色小块上,你只能朝你的四个方向(上下左右)移动,且不能到达红色的小块上,问你最多能到达多少个小块. 很简单的dfs深度优先搜索 没搜索过一个格子,将该格子设置为红色,之后的搜索就不会再搜索到该格子,就不会造成重复,因为该题有很多数据,记得每次处理数据是初始化各数组及其他数据. 代码如下: #include <iostream> #i

POJ 1979 Red and Black (红与黑)

POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to

POJ 1979 Red and Black 深度优先搜索上手题

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21738   Accepted: 11656 Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a