UVa 657 - The die is cast
来源:互联网 发布:琅琊榜细节 知乎 编辑:程序博客网 时间:2024/06/05 10:01
题目:给你一个地图,分成很多个区域'*',每个区域中还有一些'X',求每个区域中'X'区域的个数。
分析:搜索,bfs+dfs。
首先,利用bfs寻找每个'*'的区域;
然后,在每个区域中,遇到'X'用floodfill处理,将'X'统计,并转化成'*';
最后,统计输出即可。
说明:区域的边界不一定只有'*',也可能是'X';每组数据后有一个空行。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;typedef struct nodeq{int x,y;}queue;queue Q[2500],Now,New;int ans[2500];char maps[51][51];int dxy[4][2] = {1,0,0,1,-1,0,0,-1};void dfs( int x, int y, int n, int m ) {if ( x < 0 || y < 0 || x >= n || y >= m || maps[x][y] != 'X' )return;maps[x][y] = '*';for ( int i = 0 ; i < 4 ; ++ i )dfs( x+dxy[i][0], y+dxy[i][1], n, m );}int bfs( int x, int y, int n, int m ) {int count = 0;if ( maps[x][y] == 'X' ) {dfs( x, y, n, m );count ++;}int move = 0,save = 1;Q[0].x = x;Q[0].y = y;while ( move < save ) {Now = Q[move ++];for ( int i = 0 ; i < 4 ; ++ i ) {New.x = Now.x + dxy[i][0];New.y = Now.y + dxy[i][1];if ( New.x >= 0 && New.x < n && New.y >= 0 && New.y < m ) {if ( maps[New.x][New.y] == 'X' ) {dfs( New.x, New.y, n, m );count ++;}if ( maps[New.x][New.y] == '*' ) {maps[New.x][New.y] = '.';Q[save ++] = New;}}}}return count;}int main(){int n,m,T = 1;while ( ~scanf("%d%d",&m,&n) && n+m ) {for ( int i = 0 ; i < n ; ++ i )scanf("%s",maps[i]);int count = 0;for ( int i = 0 ; i < n ; ++ i )for ( int j = 0 ; j < m ; ++ j )if ( maps[i][j] == '*' || maps[i][j] == 'X' )ans[count ++] = bfs( i, j, n, m );sort( ans, ans+count );printf("Throw %d\n%d",T ++,ans[0]);for ( int i = 1 ; i < count ; ++ i )printf(" %d",ans[i]);printf("\n\n");}return 0;}
0 0
- UVa 657 The die is cast
- uva 657 - The die is cast
- uva 657 - The die is cast
- uva 657 - The die is cast
- UVa 657 - The die is cast
- UVA 657 - The die is cast
- UVA 657 The die is cast
- UVa 657 - The die is cast
- uva 657 - The die is cast
- UVA 657 The die is cast
- uva 657 The die is cast
- The die is cast UVA 657
- uva 657 The die is cast
- UVa 657 - The die is cast
- UVA 657 The die is cast
- The die is cast - UVa 657 搜索
- [657] The die is cast UVA
- UVA - 657 The die is cast
- vc中release和debug的区别
- JAVA 通过HtmlParser获取指定的tag
- 使用迭代法对二叉树进行后序遍历——Leetcode系列(六)
- 网站建设10个最好的响应的HTML5滑块插件
- 互联网思维:高大上难接地气
- UVa 657 - The die is cast
- U-boot引导流程分析二
- C++的基本应用
- linux内核源代码的C语言代码
- caperjs操作cookie
- 写驱动时交叉编译之makefile编写模板(tiny6410)
- 遗传算法
- Python实现求两个字符串的最短编辑距离
- J2EE学习笔记之JSP常用三个指令元素