# Description

Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes, which is marked from A to K, as Figure 1 shows.

Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map

FJK
IHE
then the water pipes are distributed like

Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.

Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him?

Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.

# Input

There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of ‘A’ to ‘K’, denoting the type of water pipe over the corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= M, N <= 50.

# Output

For each test case, output in one line the least number of wellsprings needed.

# 坑

1、方向数组和枚举之间的关系一定要清楚，卡了好久
2、数组下标总是写错，不仔细

# 代码

#include <iostream>
#include <cstdio>
#include <cstring>

//#define DEBUG

using namespace std;

enum Direction
{
DOWN = 0,
UP,
RIGHT,
LEFT
};

const int dirx[] = {1, -1, 0, 0};//x方向的数组
const int diry[] = {0, 0, 1, -1};//y方向的数组

//用4个点来表示每种水田的四个边是否有口
const int squares[12][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}
};

int ans;
int m, n;
int fram[55][55];
int vis[55][55];

bool isOk();//判断是否联通的函数
void Solve();
void Input();//输入函数
void Dfs(int, int);
void Print(int a, int b)//Debug用
{
printf("   A : %c, B : %c\n", a+'A', b+'A');
}

int main()
{
while(cin >> m >> n)
{
if((m > 0)&&(n > 0))
{
Input();
Solve();
}
else break;
}
return 0;
}

bool isOk(int x, int y, int dir)
{
if(dir == UP)
{
if(x == 0) return false;
int a = fram[x][y];
int b= fram[x-1][y];

#ifdef DEBUG
cout << "DIR : " << dir << "  X : " << x << "  Y : " << y;
Print(a, b);
#endif // DEBUG

if(squares[a][0]&squares[b][2])
return true;
else
return false;

}
else if(dir == DOWN)
{
if(x == m-1) return false;

int a = fram[x][y];
int b= fram[x+1][y];
#ifdef DEBUG

cout << "DIR : " << dir <<  "  X : " << x << "  Y : " << y;
Print(a, b);
#endif // DEBUG

if(squares[a][2]&squares[b][0])
return true;
else
return false;
}
else if(dir == RIGHT)
{
if(y == n-1) return false;

int a = fram[x][y];
int b = fram[x][y+1];

#ifdef DEBUG
cout << "DIR : " << dir << "  X : " << x << "  Y : " << y;
Print(a, b);
#endif
if(squares[a][1]&squares[b][3])
return true;
else
return false;
}
else if(dir == LEFT)
{
if(y == 0) return false;

int a = fram[x][y];
int b = fram[x][y-1];

#ifdef DEBUG
cout << "DIR : " << dir << "  X : " << x << "  Y : " << y;
Print(a, b);
#endif // DEBUG

if(squares[a][3]&squares[b][1])
return true;
else
return false;
}
return 1;
}

void Solve()
{
memset(vis, 0, sizeof(vis));
ans = 0;
for(int i = 0;i<m;i++)
for(int j = 0;j<n;j++)
if(!vis[i][j])
{
Dfs(i, j);
#ifdef DEBUG
cout << "======" << endl;
#endif // DEBUG
ans++;
}
printf("%d\n", ans);
}

void Input()
{
char t;
for(int i = 0;i<m;i++)
{
for(int  j= 0;j<n;j++)
{
cin >> t;
fram[i][j] = t-'A';
}
}
}

void Dfs(int x, int y)
{

if(x < 0 || x >= m || y < 0 || y >= n) return;
if(vis[x][y]) return;

vis[x][y] = 1;
for(int i = 0;i<4;i++)
{
if(isOk(x, y, i))
Dfs(x+dirx[i], y+diry[i]);
else
continue;
}
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120