题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1412
题目大意:翻译看的《图论算法理论,实现及应用》王桂平著,练习2.1
思路:dfs,简单麻烦。。。
///2014.7.25
///zoj2412
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m;
int num;
char maze[60][60];
bool used[60][60];
const int dir[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };
void init(){
num = 0;
memset(used,false,sizeof(used));
for(int i=0 ; i<n ; i++)
scanf("%s",maze[i]);
}
const bool link[11][4] = {
{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},
{1,0,1,0},{0,1,0,1},{1,1,0,1},{1,0,1,1},
{0,1,1,1},{1,1,1,0},{1,1,1,1}
};
bool judge(int x,int y,int i){
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if( used[xx][yy] )
return false;
if( !link[ maze[x][y]-'A' ][i] )
return false;
if( !link[ maze[xx][yy]-'A' ][ (i+2)%4 ] )
return false;
return true;
}
void dfs(int x,int y){
used[x][y] = true;
int xx,yy;
for(int i=0 ; i<4 ; i++){
xx = x + dir[i][0];
yy = y + dir[i][1];
if( xx>=0 && xx<n && yy>=0 && yy<m && judge(x,y,i) )
dfs(xx,yy);
}
}
int main(){
while( scanf("%d %d",&n,&m) && n!=-1 ){
init();
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<m ; j++){
if( !used[i][j] ){
num ++;
dfs(i,j);
}
}
}
printf("%d\n",num);
}
return 0;
}