题意:给你一个迷宫地图,让你走.问最多可以走多少个“." 思路:dfs 找到起点,然后对起点进行dfs操作. dfs操作时,要把当前的位置标志成"#"表示已经走过,然后进行四个方向的遍历.如果当前可以满足不超过范围,且是"."的就继续dfs 代码上的注意:四个方向上的遍历,用一个二维数组比较方便 ][] { { ,- },{ , },{ , },{ -, } }; 解决问题的代码: #include <iostream> #include &l…
题意:给出一个农田的图,n行m列,再给出k个被淹没的坐标( i , j ).求出其中相连的被淹没的农田的最大范围. 思路:dfs算法 代码: #include<iostream> #include<stdio.h> using namespace std; int t[150][150]; int visit[150][150]; int n,m,k; int dfs(int i,int j){ if(i<1||j<1||i>n||j>m||t[i][j]=…
标准DFS,统计遍历过程中遇到的黑点个数 #include<cstdio> #include<vector> #include<queue> #include<string> #include<map> #include<iostream> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const…
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 one of four adjacent tiles. But he can't move on red tiles, he can move only…
题意:一个5*5地图上面,从任意位置上下左右跳五次,组成一个数.问:不重复的数有多少个? 思路:dfs 从任意位置跳5次,说明每个位置都需要遍历. 组成一个数:number*10+map[dx][dy] 不重复的数字,用set(集合)来存储 只需要每次跳的时候步数加1,并且可以跳的位置,只要不超过范围就可以,即一个位置可以重复跳 解决问题的代码: #include <iostream> #include <cstdio> #include <set> using nam…
题意:有不规则地图,在上面放n个相同的棋子,要求摆放的时候不同行不同列.问:有多少种摆法? 思路:dfs+回溯 用一个book[]数组来表示当前列是否有放棋子 一行一行的遍历,对一行来说遍历它的列,如果满足book[i] == 0 && map[cur][i] == '#'  则可以摆放,然后继续遍历下一行 代码上需要注意的地方: ; i<n; i++) && map[cur][i] == '#') { book[i] = ; m++; dfs(cur + ); bo…
题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define INF 2147483647 int w,h; ][]; ][] = {-,,,,,-,,}; ; void dfs(int x,int y){ || x >= h || y < || y >= w || a[x][y] == '#') return; ans++; a[x][y] = '#';…
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 one of four adjacent tiles. But he can't move on red tiles, he can move only…
题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往四个方向搜索,会重复搜索,所以使用vis表来标记访问过的点,避免重复搜索. 代码如下: #include <iostream> using namespace std; ; int vis[MAX_N][MAX_N]; char map[MAX_N][MAX_N]; int red_count,…
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…