http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1412
第一次知道bfs可以用的这么活,本来打算直接用3*3数组将每一种情况构建新表,但是将表都建好后发现了一个很麻烦的问题,就是单独输出3*3的图是对的,但是构建成多种情况的图的时候,相邻的两个字母的情况无法连接起来(因为图中间有一条边是公用的
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char grid[52][52];
char map[52][52];
int m,n;
int dir[4][2] = {{-1,0},{0,1},{0,-1},{1,0}}; // ?,?,?,?
//????? 0,1,2,3
int figure[11][5] = //figure[i][0]??????
{
{2,0,2}, //A
{2,0,1}, //B
{2,3,2}, //C
{2,1,3}, //D
{2,0,3}, //E
{2,2,1}, //F
{3,0,2,1}, //G
{3,0,3,2}, //H
{3,1,2,3}, //I
{3,0,1,3}, // J
{4,0,1,2,3} //K
};
//?(x,y)???????????????
void dfs(int x,int y)
{
int t1,t2,xi,yi,i,j,temp;
grid[x][y] = '*'; //????????
t1 = map[x][y] - 'A'; //???????
for(i = 1; i <= figure[t1][0]; i++)
{
temp= figure[t1][i];
xi = x+dir[temp][0];
yi = y+ dir[temp][1];
if(xi >= m || yi >= n || xi < 0 || yi < 0) continue; //??
if(grid[xi][yi] == '*') continue; //????
t2 = map[xi][yi] - 'A';
for(j = 1; j <= figure[t2][0]; j++) //?????????
{
if(temp + figure[t2][j] == 3 )
break;
}
if( j <= figure[t2][0]) //?break??,???????
{
dfs(xi,yi);
}
}
}
//??????????????,????????????????,??????????
//?????????????????,?map[i][j] ?????????,?grid[i][j]????????
//????,?????*,????????@
int main()
{
// freopen("in.cpp","r",stdin);
// freopen("out.cpp","w",stdout);
while(true)
{
int i,j;
cin>>m>>n;
if(m<0 || n < 0)
break;
memset(grid,'@',sizeof(grid));
for(i=0; i< m; i++)
{
scanf("%s",map[i]);
}
int count = 0;
for(i = 0; i< m; i++)
{
for( j = 0; j < n; j++)
{
if(grid[i][j] == '@')
{
dfs(i,j);
count++;
}
}
}
cout<<count<<endl;
}
return 0;
}