一、本周ACM学习相关内容
-
十分认真,仔细,严谨的学习了DFS和BFS的内容(《挑战程序设计竞赛》) —— 4小时
-
十分认真,仔细,严谨的学习了列如vector和queue等及其函数(也算是上课学的吧) —— 3小时
二、题数与耗时
下面是解题报告:
POJ - 2386 Lake Counting
很简单的dfs,就是问出现一个w之后和它相连的w可以凑成一个水洼,问一共可以组成几个水洼;
按正常的套路做即可
代码:
//去吧马里奥!把AC公主救回来! // ******** // ************ // ####....#. // #..###.....##.... // ###.......###### // ........... // ##*####### // ####*******###### // ...#***.****.*###.... // ....**********##..... // ....**** *****.... // #### #### // ###### ###### #include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<string> #include<map> #include<sstream> #include<cstring> #define LL long long #define _64 __int64 using namespace std; char table[150][150]; int N,M; void dfs(int x,int y){ table[x][y] = '.'; int nx,ny; for(int dx = -1;dx <= 1;dx++){ for(int dy = -1;dy <= 1;dy++){ nx = x+dx; ny = y+dy; if(nx >= 0 && nx < N && ny >= 0 && ny < M && table[nx][ny] == 'W'){ dfs(nx,ny); } } } } int main(){ cin >> N >> M; for(int i = 0;i < N;i++){ for(int j = 0;j < M;j++){ cin >> table[i][j]; } } int ans = 0; for(int i = 0;i < N;i++){ for(int j = 0;j < M;j++){ if(table[i][j] == 'W'){ dfs(i,j); ans++; } } } cout << ans << endl; return 0; }
POJ - 1979 Red and Black
和上一题差不多,不一样就不一样在这个计数是在dfs里面计数的
代码:
扫描二维码关注公众号,回复:
9864176 查看本文章
//去吧马里奥!把AC公主救回来! // ******** // ************ // ####....#. // #..###.....##.... // ###.......###### // ........... // ##*####### // ####*******###### // ...#***.****.*###.... // ....**********##..... // ....**** *****.... // #### #### // ###### ###### #include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<string> #include<map> #include<sstream> #include<cstring> #define LL long long #define _64 __int64 using namespace std; int W,H; char table[30][30]; int a[4] = {0,0,1,-1}; int b[4] = {1,-1,0,0}; int vis[30][30]; int ans; void dfs(int x,int y){ vis[x][y] = 1; for(int i = 0;i < 4;i++){ int nx,ny; nx = x + b[i]; ny = y + a[i]; if(nx >= 0 && nx < H && ny >= 0 && ny < W && table[nx][ny] == '.' && !vis[nx][ny]){ dfs(nx,ny); ans++; } } } int main(){ while(cin >> W >> H,W != 0,H != 0){ ans = 0; memset(vis,0,sizeof(vis)); for(int i = 0;i < H;i++){ for(int j = 0;j < W;j++){ cin >> table[i][j]; } } for(int i = 0;i < H;i++){ for(int j = 0;j < W;j++){ if(table[i][j] == '@' && !vis[i][j]){ dfs(i,j); break; } } } cout << ans+1 << endl; } return 0; }