POJ 2386 Lake Counting
来源:互联网 发布:win8.1无法连接windows 编辑:程序博客网 时间:2024/06/14 09:43
这个感觉用不到动态规划
自己一次就想出来了
做完后看了看网上大神的解法,发现直接用深度遍历更快。
先给出自己404k 94ms的代码,在粘上大神192k 0ms的代码
代码:
#include <iostream>#include <string>using namespace std;struct element{bool water;int row;int col;int id;};void update_pondid(int row, int col, element ** arr,int arrrow, int arrcol,int oldid,int newid){int i;int j;for ( i = 0; i <= row; i++ ){for ( j = 0; j < arrcol; j++ ){if(arr[i][j].id == oldid )arr[i][j].id = newid;}}}void Around_Judge(int row, int col, element ** arr,int arrrow, int arrcol,int *pondcount,int * ponddelete){if(arr[row][col].water){int l = -1, rt = -1;int id = -1;bool re = false;if( (col - 1) >= 0 && arr[row][col-1].water ){id = l = arr[row][col-1].id;re = true;}if( (col - 1) >= 0 && (row - 1) >= 0 && arr[row-1][col-1].water ){id = arr[row-1][col-1].id;re = true;}if( (row - 1) >= 0 && arr[row-1][col].water ){id = arr[row-1][col].id;re = true;}if( (col + 1) < arrcol && (row - 1) >= 0 && arr[row-1][col+1].water){rt = arr[row-1][col+1].id;re = true;}if(!re){*pondcount +=1;arr[row][col].id = *pondcount;}else if( id != -1 && rt != -1 && id != rt ){arr[row][col].id = id;update_pondid(row, col, arr,arrrow,arrcol,rt,id);*ponddelete +=1;}else{id > rt ? arr[row][col].id = id: arr[row][col].id = rt;}}}int main(){int r = 0 , c = 0 ;cin>>r>>c;element **arr = new element*[r];for(int i = 0; i < r; i++ )arr[i] = new element[c];//初始化int i;int j;for ( i = 0; i < r; i++ ){for ( j = 0; j < c; j++ ){char t;cin>>t;element ele = {false,i,j,0};if( t == 'W' )ele.water = true;arr[i][j] = ele;}}int * count = new int;*count = 1;int * dele = new int;*dele = 1;for ( i = 0; i < r; i++ ){for ( j = 0; j < c; j++ ){Around_Judge(i,j,arr,r,c,count,dele);}}cout<<(*count) - (*dele) <<endl;return 0;}
大神的代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>using namespace std;#define maxn 106struct XPoint{ int x; int y; XPoint(int xx, int yy) : x(xx), y(yy) { } XPoint() { }};int n, m;bool map[maxn][maxn];bool vis[maxn][maxn];int ans;int dir[8][2] ={{ -1, -1 },{ -1, 0 },{ -1, 1 },{ 0, -1 },{ 0, 1 },{ 1, -1 },{ 1, 0 },{ 1, 1 } };bool ok(int x, int y){ if (x < 0 || y < 0 || x >= n || y >= m) return false; if (vis[x][y]) return false; return map[x][y];}void work(int x, int y){ ans++; queue<XPoint> q; q.push(XPoint(x, y)); vis[x][y] = true; while (!q.empty()) { XPoint temp = q.front(); q.pop(); for (int i = 0; i < 8; i++) if (ok(temp.x + dir[i][0], temp.y + dir[i][1])) { vis[temp.x + dir[i][0]][temp.y + dir[i][1]] = true; q.push(XPoint(temp.x + dir[i][0], temp.y + dir[i][1])); } }}int main(){ //freopen("t.txt", "r", stdin); scanf("%d%d", &n, &m); memset(map, 0, sizeof(map)); memset(vis, 0, sizeof(vis)); ans = 0; getchar(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char ch; scanf("%c", &ch); if (ch == 'W') map[i][j] = true; } getchar(); } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (!vis[i][j] && map[i][j]) work(i, j); printf("%d\n", ans); return 0;}
0 0
- 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
- poj 2386 Lake Counting
- 桂阳
- 一键安装Android开发环境
- 插入式注解处理器
- cobol学习之一开发环境及简单程序
- Linux系统编程:如何写Daemon进程(守护进程)
- POJ 2386 Lake Counting
- NYOJ 44
- 第12周项目2刑警的射击成绩
- iphone ios 屏幕,statusbar,状态栏,标签栏尺寸,高度
- 学习使用Delphi for android 调用Java类库
- 最长上升子序列(LIS)问题
- Radar Installation(poj1328)(贪心)
- 题目【2002】
- BZOJ 1968 AHOI2005 COMMON 约数研究 线性筛