Aizu - 0118(深搜)
2016-03-17 08:18
375 查看
题目链接:点击打开链接
解析:
深搜无疑,和水洼那题类似。以查找过的赋值1,然后每次找不是1的位置,一搜一片即可。最后记录多少有片区域。
完整代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
const int maxn = 101;
char g[maxn][maxn];
int d[4][2] = {0 , 1 , 1 , 0 , 0 , -1 , -1 ,0};
void dfs(int si , int sj , int n , int m , char ch)
{
g[si][sj] = '1';
for(int i = 0 ; i < 4 ; i ++){
int x = si + d[i][0];
int y = sj + d[i][1];
if(x < 0 || x >= n || y < 0 || y >= m || g[x][y] != ch) continue;
dfs(x , y , n , m , ch);
}
}
int main()
{
#ifdef DoubleQ
freopen("in.txt" , "r" , stdin);
#endif // DoubleQ
int n , m;
while(cin >> n >> m){
if(n == 0 && m == 0) break;
string str;
for(int i = 0 ; i < n ; i ++){
cin >> str;
for(int j = 0 ; j < m ; j ++){
g[i][j] = str[j];
}
}
int cnt = 0;
/*
for(int i = 0 ; i < n ;i ++){
for(int j = 0 ; j < m ; j ++){
cout << g[i][j] << " ";
}
cout << endl;
}
*/
for(int i = 0 ; i < n ; i ++){
for(int j = 0 ; j < m ; j ++){
if(g[i][j] != '1'){
dfs(i , j , n , m , g[i][j]);
cnt ++;
}
}
}
cout << cnt << endl;
}
}
解析:
深搜无疑,和水洼那题类似。以查找过的赋值1,然后每次找不是1的位置,一搜一片即可。最后记录多少有片区域。
完整代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
const int maxn = 101;
char g[maxn][maxn];
int d[4][2] = {0 , 1 , 1 , 0 , 0 , -1 , -1 ,0};
void dfs(int si , int sj , int n , int m , char ch)
{
g[si][sj] = '1';
for(int i = 0 ; i < 4 ; i ++){
int x = si + d[i][0];
int y = sj + d[i][1];
if(x < 0 || x >= n || y < 0 || y >= m || g[x][y] != ch) continue;
dfs(x , y , n , m , ch);
}
}
int main()
{
#ifdef DoubleQ
freopen("in.txt" , "r" , stdin);
#endif // DoubleQ
int n , m;
while(cin >> n >> m){
if(n == 0 && m == 0) break;
string str;
for(int i = 0 ; i < n ; i ++){
cin >> str;
for(int j = 0 ; j < m ; j ++){
g[i][j] = str[j];
}
}
int cnt = 0;
/*
for(int i = 0 ; i < n ;i ++){
for(int j = 0 ; j < m ; j ++){
cout << g[i][j] << " ";
}
cout << endl;
}
*/
for(int i = 0 ; i < n ; i ++){
for(int j = 0 ; j < m ; j ++){
if(g[i][j] != '1'){
dfs(i , j , n , m , g[i][j]);
cnt ++;
}
}
}
cout << cnt << endl;
}
}
相关文章推荐
- BZOJ 1260 paint
- 出现( linker command failed with exit code 1)错误总结
- server certificate verification failed. CAfile: /e
- PowerDesigner打开设计文件后提示failed to read the fileXXX的解决办法
- Mongodb 3.0+ explain输出参数解析
- 219. Contains Duplicate II
- 初涉IPC,了解AIDL的工作原理及使用方法
- 初涉IPC,了解AIDL的工作原理及使用方法
- Troubleshooting 'library cache: mutex X' Waits. (文档 ID 1357946.1)
- codeforces-148E-Porcelain【DP】
- 错误 1 error LNK2019: 无法解析的外部符号 _wWinMain@16,该符号在函数 ___tmainCRTStartup 中被引用 E:\elipse demo\LTG\LTG\
- Mac Air 优势
- django.db.utils.IntegrityError:(1215, canot add foreign key constraint)
- main函数的两个参数
- URAL1017Staircases DP
- maven install时报错Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
- 从WinMain开始
- Pairs Forming LCM(素因子分解)
- clang: error: linker command failed with exit code 1 (use -v to see invocation)解决方法
- maven install Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war