题意:给你一个迷宫地图,让你走。问最多可以走多少个“."

思路:dfs

  1. 找到起点,然后对起点进行dfs操作。
  2. dfs操作时,要把当前的位置标志成"#"表示已经走过,然后进行四个方向的遍历。如果当前可以满足不超过范围,且是"."的就继续dfs

代码上的注意:四个方向上的遍历,用一个二维数组比较方便

const int dir[][]
{
{ ,- },{ , },{ , },{ -, }
};

解决问题的代码:

#include <iostream>
#include <cstdio>
using namespace std;
int w, h;
int ans = ;
char map[][];
const int dir[][]
{
{ ,- },{ , },{ , },{ -, }
};
int bfs(const int x, const int y)
{
map[x][y] = '#';
++ans;
for (int i = ; i < ; i++)
{
int cur_x = x + dir[i][];
int cur_y = y + dir[i][];
if (cur_x >= && cur_x < h&&cur_y >= && cur_y < w&&map[cur_x][cur_y] == '.')
bfs(cur_x, cur_y);
}
return ans;
}
int main()
{
while (scanf("%d%d", &w, &h) != EOF)
{
if (w == && h == ) break;
for (int i = ; i < h; i++)
for (int j = ; j < w; j++)
cin >> map[i][j];
bool flag = true;
if (flag)
{
for (int i = ; i<h; i++)
for (int j = ; j<w; j++)
if (map[i][j] == '@')
{
cout << bfs(i, j) << endl;
flag = false;
}
}
if (!flag)
{
ans = ;
continue;
}
}
}

poj 1979 走多少个‘ . '问题 dfs算法的更多相关文章

  1. POJ 3620 Avoid The Lakes(dfs算法)

    题意:给出一个农田的图,n行m列,再给出k个被淹没的坐标( i , j ).求出其中相连的被淹没的农田的最大范围. 思路:dfs算法 代码: #include<iostream> #inc ...

  2. POJ 1979 Red and Black【DFS】

    标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include< ...

  3. poj 1979 Red and Black(dfs水题)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  4. poj 3050 地图5位数问题 dfs算法

    题意:一个5*5地图上面,从任意位置上下左右跳五次,组成一个数.问:不重复的数有多少个? 思路:dfs 从任意位置跳5次,说明每个位置都需要遍历. 组成一个数:number*10+map[dx][dy ...

  5. poj 1321 排兵布阵问题 dfs算法

    题意:有不规则地图,在上面放n个相同的棋子,要求摆放的时候不同行不同列.问:有多少种摆法? 思路:dfs+回溯 用一个book[]数组来表示当前列是否有放棋子 一行一行的遍历,对一行来说遍历它的列,如 ...

  6. POJ 1979 Red and Black (简单dfs)

    题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...

  7. POJ 1979 Red and Black (DFS)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  8. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  9. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

随机推荐

  1. 如何在windows下是用mysqldumpslow命令

    1. 再一次点击mysql安装文件(默认是没安装mysqldumpslow这些脚本的),如图: 点击next如下图 点击Developer Components 旁边的选择this feature , ...

  2. SLF4J user manual 专题

    System Out and Err Redirected to SLF4J The sysout-over-slf4j module allows a user to redirect all ca ...

  3. Dubbo 使用rest协议发布http服务

    演示用GitHub地址:https://github.com/suyin58/dubbo-rest-example 1       Dubbo_rest介绍 Dubbo自2.6.0版本后,合并了dub ...

  4. 如何移除网站Response Headers中的X-Powered-By信息?

    X-Powered-By是网站响应头信息其中的一个,出于安全的考虑,一般会修改或删除掉这个信息. 如果你用的node.js express框架,那么X-Powered-By就会显示Express.如果 ...

  5. AES加密示例

    最近用到对文本内容进行加密,于是查了一下常用的加密算法: DES(Data Encryption Standard):对称算法,数据加密标准,速度较快,适用于加密大量数据的场合:3DES(Triple ...

  6. 系统有问题基本出在数据库上,web层无状态

    系统有问题基本出在数据库上,web层无状态.

  7. LeetCode House Robber 家庭劫犯(dp)

    题意:有一个整数序列,从中挑出一些数字,使得总和是最大,前提是,相邻的两个数字中只能挑其一.比如1 2 3 就只能挑2或者1和3. 思路:很直观的题,dp思想.降低规模,从小规模开始考虑.如果只有两个 ...

  8. Codeforces 666E Forensic Examination SAM or SA+线段树合并

    E. Forensic Examination http://codeforces.com/problemset/problem/666/E 题目大意:给模式串S以及m个特殊串,q个询问,询问S的子串 ...

  9. MovieReview—Despicable Me 3(神偷奶爸3)

    Minions&Unicorn         The film focuses on the story of Grew and the bastard Bled. A variety of ...

  10. 10款免费的MySQL数据库图形化管理工具

    绝大多数的关系数据库都明显不同于MS Access,它们都有两个截然不同的部分:后端作为数据仓库,前端作为用于数据组件通信的用户界面.这种设计非常巧妙,它并行处理两层编程模型,将数据 层从用户界面中分 ...