POJ1979 Red and Black DFS搜索
来源:互联网 发布:1688怎么传淘宝 编辑:程序博客网 时间:2024/05/21 14:42
题目链接:http://poj.org/problem?id=1979
题目意思:给你一个N*M的迷宫图,@表示人起始位置,#便是墙无法通过," . "表示路可以走。让你求出从@位置能走过的 ‘ . ’有多少格。
分析:这个题目可以跟POJ2386 一起做,将那个题目的代码修改一下就可以用了。统计的是走过的格子数所以DFS BFS都可以用。
#include<stdio.h>#include<cmath>#include<iostream>#include<algorithm>#include<string.h>#include<queue>using namespace std;char map[105][105];int dis[4][2]={-1,0, 1,0, 0,-1, 0,1};int n,m,ans;int isin(int x,int y){if(x >= 0 && x < n && y >= 0 && y < m)return 1;return 0;}void dfs(int x,int y){ for(int i = 0; i < 4 ; i++) { int x1 = x + dis[i][0]; int y1 = y + dis[i][1]; if(isin(x1,y1) && map[x1][y1] == '.') { map[x1][y1]='#'; //走过的格子变成墙,ANS统计下变墙的格子数 ans++; dfs(x1,y1); } else continue; }}int main(){ int i , j , k ; while(scanf("%d%d",&m,&n)&&n!=0&&m!=0) { for(i=0;i<n;i++) scanf("%s",map[i]); memset(vis,0,sizeof(vis)); ans=1; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(map[i][j]=='@') { dfs(i,j); break; } } } printf("%d\n",ans); }}
0 0
- POJ1979 Red and Black DFS搜索
- poj1979 Red and Black dfs
- poj1979 Red and Black(dfs)
- [dfs]poj1979 Red and Black
- POJ1979 Red and Black (DFS)
- poj1979 Red and Black DFS
- POJ1979 Red and Black (DFS)
- POJ1979 Red and Black【DFS】
- poj1979 Red and Black【搜索】
- poj1979 Red and Black DFS深度优先搜索+剪枝
- 深度优先搜索(DFS):POJ1979--Red and Black
- POJ1979(DFS深度优先搜索)之 Red and Black
- POJ1979 Red and Black dfs基础题
- hdu1312 poj1979 Red and Black(DFS)
- poj1979 Red and Black(DFS)
- POJ1979 Red and Black(深搜DFS)
- POJ1979-Red and Black-DFS染色
- POJ1979 Red and Black(DFS)
- FPGA机器学习之龙星计划机器学习第七堂
- 黑马程序员 java中的String类
- 【hdu】【P2157】【How many ways??】【题解】【矩阵乘法】
- 解读IEEE标准754:浮点数表示
- 法规和发个很反感
- POJ1979 Red and Black DFS搜索
- OC---特有语法1
- 使用spring AOP获得session的思路
- NSSet和NSMutableSet
- ARM平台上Linux异常处理代码简要分析
- Debug与Release版本的区别
- Android系统架构
- C语言编译原理
- 在WPF程序中使用摄像头兼谈如何使用AForge.NET控件