POJ 2386 Lake Counting DPS解法
来源:互联网 发布:matlab绘制矩阵灰度图 编辑:程序博客网 时间:2024/05/21 10:01
因为总是害怕超时,束手束脚顾虑太多,其实很简单,直接无脑搜索就做出来了
思路是设置一个数组记录访问情况,然后先找W,找完了DFS标记周围的W,如果没有可以标记的就结束搜索。
#include<stdio.h>#include<string.h>int n, m, num = 0;char pond[105][105];int vis[105][105];void dfs( int row, int col);int main(void){int i, j;scanf("%d%d", &n, &m);getchar();for( i = 0; i < n; i++){gets(pond[i]);}memset( vis, 0, sizeof(vis));for( i = 0; i < n; i++){for( j = 0; j < m; j++){if( pond[i][j] == 'W' && !vis[i][j]){ dfs( i, j); num++;}}}printf("%d", num);return 0;}void dfs( int row, int col){if( row == n || col == m || row < 0 || col < 0) return;else if( pond[row][col] != 'W' || vis[row][col]) return; //这里一定要是八个方向,之前想着主函数是从上到下从左到右扫描的就可以省略上面的三个方向,其实漏想了很多种情况,POJ这道题后面的讨论里有很好的测试数据vis[row][col] = 1;dfs( row+1, col);dfs( row+1, col-1);dfs( row+1, col+1);dfs( row, col+1);dfs( row, col-1);dfs( row-1, col+1);dfs( row-1, col);dfs( row-1, col-1);return;}
0 0
- POJ 2386 Lake Counting DPS解法
- poj 2386 Lake Counting
- poj 2386 Lake Counting
- POJ 2386 Lake Counting
- poj 2386 Lake Counting
- poj 2386 Lake Counting
- poj 2386 Lake Counting
- POJ 2386 Lake Counting
- poj 2386 Lake Counting
- POJ 2386 Lake Counting
- POJ 2386 Lake Counting
- poj 2386 Lake Counting
- POJ 2386 Lake Counting
- POJ 2386 Lake Counting
- POJ-2386-Lake Counting
- POJ 2386 Lake Counting
- poj 2386 Lake Counting
- Poj 2386 Lake Counting
- 数据挖掘笔记:分类和预测,判定树
- UVA 10911 Forming Quiz Teams(状压DP)
- POJ 1151 Atlantis (线段树+扫描线 求矩形面积并)
- 大型网站架构要素之《性能》篇
- 线程池 和数据库连接池
- POJ 2386 Lake Counting DPS解法
- HDU -- 3844 Mining Your Own Business(点双连通)
- 《重构》读后感——第一章
- vs2010 vc++ 调用dll
- I am coming back
- Adnroid Watchdog实现详解
- 日拱一卒(五十八)
- 错误总结之——编译文件时出现不允许包含non-ASCII的文字和标识
- 回调Callback之Java