Lake Counting(思维漏洞)(记忆化搜索)
来源:互联网 发布:led走字屏改字软件 编辑:程序博客网 时间:2024/06/06 02:43
http://poj.org/problem?id=2386
1、在AOJ上,我提交的是如下代码,且AC了,但是在POJ却一直WA:
其实这是我思维上的漏洞,假设图示如下:
..W...W..
...WWW...
如果按我开始的代码,则结果是2,正确是1。
#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <map>#include <iostream>#include <stdexcept>#include <cstddef>#include <algorithm>#include <vector>#include <numeric>#include <cctype>#define LL long long#define Endl endl#define INF 0x7fffffff//#define WJconst int maxn=110;char str[maxn][maxn];bool vis[maxn][maxn];using namespace std;int main(int argc, char *argv[]){#ifdef WJ//freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);#endifint n,m;memset(vis,0,sizeof(vis));memset(str,'.',sizeof(str));scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin>>str[i][j];int cnt=0;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){if(str[i][j]=='W'){if(!vis[i][j]) cnt++;vis[i][j]=1; vis[i][j+1]=1; vis[i][j-1]=1;vis[i-1][j]=1; vis[i-1][j+1]=1; vis[i-1][j-1]=1;vis[i+1][j]=1; vis[i+1][j+1]=1; vis[i+1][j-1]=1;}}printf("%d\n",cnt);return 0;}
2、正确代码如下:
#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <map>#include <iostream>#include <stdexcept>#include <cstddef>#include <algorithm>#include <vector>#include <numeric>#include <cctype>#define LL long long#define Endl endl#define INF 0x7fffffff//#define WJusing namespace std;const int maxn=105;char str[maxn][maxn];bool vis[maxn][maxn];void dfs(int x,int y){if(vis[x][y]||str[x][y]=='.'||str[x][y]=='*') return ;vis[x][y]=1;dfs(x+1,y); dfs(x+1,y+1); dfs(x+1,y-1); dfs(x,y+1); dfs(x,y-1); dfs(x-1,y-1); dfs(x-1,y); dfs(x-1,y+1);}int main(int argc, char *argv[]){#ifdef WJ//freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);#endifint n,m;memset(vis,0,sizeof(vis));memset(str,'*',sizeof(str));scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin>>str[i][j];int cnt=0;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(!vis[i][j]&&str[i][j]=='W'){cnt++; dfs(i,j);}}}cout<<cnt<<endl;return 0;}
0 0
- Lake Counting(思维漏洞)(记忆化搜索)
- 搜索(A - Lake Counting)
- poj2386——Lake Counting(搜索)
- Lake Counting(搜索之DFS)
- lake counting---dfs(深度搜索算法)
- POJ-2386 Lake Counting (思维+简单dfs)
- Lake Counting(DFS)
- Lake Counting(dfs)
- lake counting(dfs)
- 搜索入门(DFS)-Lake Counting(积水连通问题)
- poj 2386 Lake Counting(搜索,种子填充法)
- poj 2386 Lake counting(深度优先搜索)
- POJ 2386 Lake Counting(深度优先搜索)
- 深度搜索DFS-Lake Counting(POJ NO.2386)
- 深度搜索DFS-Lake Counting(POJ NO.2386)
- Lake Counting -- DFS(深搜)
- poj2386 Lake Counting (深搜)
- poj 2386 (Lake Counting)
- 服务器server.c、搜索serach.c
- 通过获取的一个值和本地的一个值比较来获取一个boolean值
- 考研日记---9.06---加油
- 如何在JSP页面显示mysql数据库内容 (二)
- 通过本地存储的一个值和当前获取值得比较得到一个boolean值
- Lake Counting(思维漏洞)(记忆化搜索)
- itoa的实现
- mac 安装opencv homebrew安装
- c++头文件
- hihocoder: 后序遍历
- Robotruck - ZOJ 3031 dp
- Ubuntu 12.10 下MySQL 5.1.65 解压安装
- LeetCode: Word Ladder
- 覆盖技术规范噶的舒服的萨芬