codeforces 616C The Labyrinth
来源:互联网 发布:java http json接口 编辑:程序博客网 时间:2024/04/30 12:02
从‘ . ’出发,相邻点作为连通块整体考虑,周围的星星要加上这堆点的个数。
两次bfs,一次找连通块,一次找可达的星星‘ * ’
ps:1> 记得取模。
2>要用int 取然后赋给char,一起用%s输出。——为了偷懒直接用char型字符取模结果。。。╮(╯▽╰)╭
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int m,n;char a[1010][1010],b[1010][1010];int vis[1010][1010];int dx[4]={-1,0,0,1};int dy[4]={0,-1,1,0};int num[1010][1010];/// bfs 找连通块int findblock(int x,int y){ int ans=0; vis[x][y]=1; int xx,yy; for(int i=0;i<4;i++) { xx=x+dx[i]; yy=y+dy[i]; if(xx<0||yy<0||xx>=n||yy>=m) continue; if(vis[xx][yy]==1||a[xx][yy]=='*') continue; // cout<<xx<<" "<<yy<<endl; ans+=findblock(xx,yy); } //cout<<"ans=="<<ans<<endl; ans++; return ans;}/// bfs 找可达*void findstar(int x,int y,int ans,int k){ vis[x][y]=0; for(int i=0;i<4;i++) { int xx=x+dx[i]; int yy=y+dy[i]; if(xx<0||yy<0||xx>=n||yy>=m) continue; if(a[xx][yy]=='*'&&vis[xx][yy]!=k) { vis[xx][yy]=k; num[xx][yy]+=ans; //char x=num[xx][yy]+'0'; //cout<<xx<<" "<<yy<<" "<<x<<endl; } else if(a[xx][yy]=='.'&&vis[xx][yy]==1) { findstar(xx,yy,ans,k); } }}int main(){ scanf("%d%d",&n,&m);/// n行m列 for(int i=0;i<n;i++) scanf("%s",a[i]); memset(vis,-1,sizeof(vis)); memset(num,0,sizeof(num)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]=='.'&&vis[i][j]==-1) {//cout<<"-1"<<endl; int ans=findblock(i,j); // cout<<" "<<i<<" "<<j<<" "<<ans<<endl; findstar(i,j,ans,i*m+j); } } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]=='*') { num[i][j]+=1; num[i][j]=num[i][j]-(num[i][j]/10)*10;/// 不可用 char 取模 b[i][j]=num[i][j]+'0'; } else b[i][j]='.'; } printf("%s\n",b[i]); } return 0;}
0 0
- Codeforces 616C The Labyrinth
- codeforces 616C The Labyrinth
- Codeforces 616C The Labyrinth
- The Labyrinth CodeForces - 616C
- codeforces 616C The Labyrinth(bfs)
- CodeForces 616 C. The Labyrinth(dfs)
- codeforces 616C The Labyrinth (dfs)
- CodeForces 616C The Labyrinth(DFS)
- CodeForces 616C:The Labyrinth(BFS)
- CodeForces 616C The Labyrinth (二维并查集)
- 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 DFS
- Educational Codeforces Round 5 C. The Labyrinth (DFS **)
- CF 616C The Labyrinth(dfs)
- Educational Codeforces Round 5 C.The Labyrinth(dfs+并查集)
- 新的开始
- 集训队专题(1)1005 Ancient Printer
- 运用人性,七情六欲聊运营
- NOIP 模拟练习题 收费站
- Android中attr自定义属性详解
- codeforces 616C The Labyrinth
- Linux 设备驱动中的并发控制
- 苹果电脑Finder中用户选项显示
- php命名空间的详细说明
- Android注解式开发BufferKnife的使用
- struct/union内存对齐
- 解决Eclipse和myeclipse在进行 html,jsp等 页面编辑时,自动格式化问题
- Java - String, StringBuffer and StringBuilder
- UITableView的数据源设置和简单介绍