AOJ 0118: Property Distribution (简单DFS)
来源:互联网 发布:圆周率网络 编辑:程序博客网 时间:2024/06/05 10:11
题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0118
题意:给定一个矩阵,同类字符相连的为一个块,问总共有几个块。
输入:h,w(行和列)0 <= h <= 100,0 <= w <= 100
矩阵
输入包含多组用例,以0,0结束。
输出:块数。
代码:
#include <iostream>using namespace std;typedef long long ll;#define INF 2147483647int w,h;char a[102][102];int dir[4][2] = {-1,0,1,0,0,-1,0,1};int ans = 0;void dfs(int x,int y,char s){ if(x < 0 || x >= h || y < 0 || y >= w || a[x][y] != s) return; char t = a[x][y]; a[x][y] = 'o'; for(int i = 0;i < 4; i++){ dfs(x+dir[i][0], y+dir[i][1], t); }}int main(){ while(cin >> h >> w){ if(w == 0 && h == 0) break; ans = 0; int sx,sy; for(int i = 0;i < h; i++){ for(int j = 0;j < w; j++){ cin >> a[i][j]; } } for(int i = 0;i < h; i++){ for(int j = 0;j < w; j++){ if(a[i][j] != 'o'){ ans++; dfs(i,j,a[i][j]); } } } cout << ans << endl; } return 0;}
阅读全文
0 0
- AOJ 0118: Property Distribution (简单DFS)
- AOJ 0118 Property Distribution (dfs)
- AOJ 0118 Property Distribution (DFS)
- [DFS] AOJ 0118 Property Distribution
- Aoj 0118 Property Distribution【dfs】
- AOJ 0118 Property Distribution 【DFS】
- AOJ 0118 Property Distribution(dfs 求连通块)
- AOJ--0118 Property Distribution
- AOJ 0118 Property Distribution (果园分类)
- AOJ 0033 ball+AOH 0118Property Distribution (DFS)
- AOJ 0118 Property Distribution (BFS)
- AOJ 0118 Property Distribution 题解
- aizu/aoj-0118-Property Distribution
- AOJ 0118: Property Disribution(dfs)
- [挑战程序设计竞赛] AOJ 0118 - Property Distribution
- AOJ 0118 Property Distribution {深度优先搜索}
- Property Distribution(AOJ 0118)深度优先搜索
- aoj0118 Property Distribution(dfs)
- TCP和UDP
- 东莞市速网五金制品有限公司1*4光纤笼子规格书
- Android根据包名启动或者销毁activity
- java编码、解码URLEncoder、URLDecoder测试
- 开源TTS(Text To Speah)的选择和使用
- AOJ 0118: Property Distribution (简单DFS)
- 爬虫Scrapy-避免被禁止
- ECharts属性设置
- jsp的include动作和forward动作遇到的问题
- 每天一道LeetCode-----找到给定数组的连续子数组,使这个子数组的和最大,要求复杂度为O(n)
- XGBoost学习笔记
- matlab中关于函数文件和脚本文件使用实例
- 关于linux下的定时
- centos 7 双网卡绑定配置