Educational Codeforces Round 5 C The Labyrinth DFS
来源:互联网 发布:手机淘宝充值中心没有 编辑:程序博客网 时间:2024/06/02 23:22
题意:给一个N*M的地图,其中’.’表示空地,’*’表示障碍物。问,如果把障碍物拿掉,最多可以变成一个多大的全是空地的联通块?
思路:很容易想到把障碍物拿掉之后联通块的大小,就是上下左右的联通块大小加起来再加上障碍物本身一个联通块。有一点要注意,如果上和左属于同一个联通块的时候,不要重复加。求联通块的大小可以用DFS,先DFS一遍找联通块的大小,再DFS一遍把联通块中每个块的大小都赋成前面DFS找到的结果。
坑点:结果要% 10,当时没看到这点,WA了一发。
http://codeforces.com/contest/616/problem/C
/********************************************* Problem : Codeforces Author : NMfloat InkTime (c) NM . All Rights Reserved .********************************************/#include <map>#include <set>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define rep(i,a,b) for(int i = (a) ; i <= (b) ; i ++) //遍历#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --) //反向遍历#define repS(it,p) for(auto it = p.begin() ; it != p.end() ; it ++) //遍历一个STL容器#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next) //遍历u所连接的点#define cls(a,x) memset(a,x,sizeof(a))#define eps 1e-8using namespace std;const int MOD = 1e9+7;const int INF = 0x3f3f3f3f;const int MAXN = 1e5+5;const int MAXE = 2e5+5;typedef long long LL;typedef unsigned long long ULL;int T,n,m,k;int fx[] = {0,1,-1,0,0};int fy[] = {0,0,0,-1,1};char Map[1005][1005];int num[1005][1005];//每个点最多连接的点数.bool vis[1005][1005];bool vis1[1005][1005];int belong[1005][1005];int tot ;int idx ;void input() { rep(i,1,n) scanf("%s",&Map[i][1]);}void dfs(int x,int y) { belong[x][y] = idx; tot ++; rep(i,1,4) { int tmpx = x + fx[i]; int tmpy = y + fy[i]; if(tmpx >= 1 && tmpx <= n && tmpy >= 1 && tmpy <= m && !vis[tmpx][tmpy]) { vis[tmpx][tmpy] = true; if(Map[tmpx][tmpy] == '.') { dfs(tmpx,tmpy); } } }}void dfs1(int x,int y) { num[x][y] = tot; rep(i,1,4) { int tmpx = x + fx[i]; int tmpy = y + fy[i]; if(tmpx >= 1 && tmpx <= n && tmpy >= 1 && tmpy <= m && !vis1[tmpx][tmpy]) { vis1[tmpx][tmpy] = true; if(Map[tmpx][tmpy] == '.') { dfs1(tmpx,tmpy); } } }}void solve() { cls(num,0); cls(vis,0); cls(vis1,0); cls(belong,0); idx = 0; rep(i,1,n) { rep(j,1,m) { if(!vis[i][j] && Map[i][j] == '.') { idx ++; tot = 0; vis[i][j] = true; vis1[i][j] = true; dfs(i,j); dfs1(i,j); } } } int x[6],y[6]; int ans ; rep(i,1,n) { rep(j,1,m) { if(Map[i][j] == '.') printf("."); else { ans = 0; rep(ia,1,4) { x[ia] = i + fx[ia]; y[ia] = j + fy[ia]; } rep(ia,1,4) { int ok1 = 0; rep(ib,1,ia-1) { if(belong[x[ia]][y[ia]] == belong[x[ib]][y[ib]]) ok1 = 1; } if(!ok1) ans += num[x[ia]][y[ia]]; } printf("%d",(ans+1)%10); } } puts(""); }}int main(void) { //freopen("a.in","r",stdin); while(~scanf("%d %d",&n,&m)) { input(); solve(); } return 0;}
0 0
- Educational Codeforces Round 5-C. The Labyrinth(简单dfs)
- Educational Codeforces Round 5 C The Labyrinth DFS
- Educational Codeforces Round 5 C. The Labyrinth (DFS **)
- Educational Codeforces Round 5 C. The Labyrinth
- Educational Codeforces Round 5 C. The Labyrinth
- Educational Codeforces Round 5 C. The Labyrinth
- Educational Codeforces Round 5 C.The Labyrinth(dfs+并查集)
- Educational Codeforces Round 5 C. The Labyrinth BFS,并查集
- Educational Codeforces Round 22 C. The Tag Game dfs
- CodeForces 616 C. The Labyrinth(dfs)
- codeforces 616C The Labyrinth (dfs)
- CodeForces 616C The Labyrinth(DFS)
- Educational Codeforces Round 5(C)(DFS+hash)
- Educational Codeforces Round 5 C
- Educational Codeforces Round 1 D.Igor In the Museum(DFS)
- Educational Codeforces Round 31 C. Bertown Subway dfs
- Codeforces 616C The Labyrinth
- codeforces 616C The Labyrinth
- Bash shell编程<六>:处理用户输入
- 列出android 声卡
- Codeforces 612C Replace To Make Regular Bracket Sequence 【stack】
- Android Studio安装
- Java中ArrayList源码解析
- Educational Codeforces Round 5 C The Labyrinth DFS
- iOS 3D touch
- Git安装及配置
- 基于Dubbo框架构建分布式服务
- 都在说EventBus,我也来一波EventBus
- ios app图片角标
- Codeforces 612D The Union of k-Segments 【思维】
- eclipse 的一些小知识
- java多线程--生产者和消费者模型