hdoj 1312 Red and Black

20 篇文章 0 订阅

题目大意:一个人站在一块黑色瓷砖上,他只能向四个方向走,不能走到红色瓷板上,求能走过的黑色瓷板,走过了的黑色瓷板不算

解题思路:bfs, dfs均可,,,,水题

BFS:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <climits>
#include <algorithm>
using namespace std;

struct node
{
	int x, y;
};

const int maxn = 25;
char room[maxn][maxn];
int w, h;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int bfs(int x, int y);

int main()
{
	
	int sx, sy;
	while(true)
	{
		scanf("%d %d", &w, &h);
		if(0 == w && 0 == h)
			break;
		for(int i = 0; i < h; i++)
		{
			scanf("%s", room[i]);
			for(int j = 0; j < w; j++)
			{
				if(room[i][j] == '@')
				{
					sx = i, sy = j;
				}
			}
		}
		printf("%d\n", bfs(sx, sy));
	}
	
	return 0;
}

int bfs(int x, int y)
{
	int val = INT_MIN;
	queue<node> que;
	node t, tmp;
	t.x = x; t.y = y; 
	room[t.x][t.y] = '#';
	que.push(t);
	int nu = 1;
	while(!que.empty())
	{
		tmp = que.front();
		que.pop();
		for(int i = 0; i < 4; i++)
		{
			t.x = tmp.x + dir[i][0];
			t.y = tmp.y + dir[i][1];
			if(t.x >= 0 && t.x < h && t.y >= 0 && t.y < w && room[t.x][t.y] == '.')
			{
				room[t.x][t.y] = '#';
				que.push(t);
				nu++;
			}
		}
	}
	return nu;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值