poj 1164 The Castle (dfs)

本文介绍了一种使用深度优先搜索(DFS)算法解决迷宫问题的方法,通过标记访问过的房间并递归地探索相邻的未访问房间,最终计算出迷宫中房间的最大区域面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 http://poj.org/problem?id=1164

提交的时候正遇oj挂了,害我白白错了几次,却不知情。。。

 

#include "iostream" #include "stdio.h" #include "string.h" using namespace std; struct room { bool vis; int val; }castle[51][51]; int area; void dfs(int x, int y) { if(!castle[x][y].vis) { area++; castle[x][y].vis = true; if(castle[x][y].val - 8 < 0) //若向南无墙 dfs(x + 1, y); else castle[x][y].val -= 8; if(castle[x][y].val - 4 < 0) //若向东无墙 dfs(x, y + 1); else castle[x][y].val -= 4; if(castle[x][y].val - 2 < 0) //若向北无墙 dfs(x - 1, y); else castle[x][y].val -= 2; if(castle[x][y].val - 1 < 0) //若向西无墙 dfs(x, y - 1); else castle[x][y].val -= 1; } } int main() { int R, C, i, j; while(scanf("%d%d", &R, &C) != EOF) { for(i = 1; i <= R; i++) for(j = 1; j <= C; j++) { scanf("%d", &castle[i][j].val); castle[i][j].vis = false; } int nRooms = 0, maxArea =0; for(i = 1; i <= R; i++) for(j = 1; j <= C; j++) { area = 0; dfs(i, j); if(area > 0) nRooms++; if(area > maxArea) maxArea = area; } cout << nRooms << endl << maxArea << endl; } return 0; }

转载于:https://www.cnblogs.com/firecoder/archive/2011/08/02/2512221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值