#include <iostream>
#include <string>
using namespace std;
int R, C;
char **area;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int dfs(int x, int y,unsigned int sta){
int _max = 0, tmp = 0;
for (int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if (nx >= 0 && nx < R && ny >= 0 && ny < C &&
!((sta >> (area[nx][ny] - 'A')) & 1))
tmp = dfs(nx, ny, sta | (1 << (area[nx][ny] - 'A')));
if (_max < tmp)
_max = tmp;
}
return _max + 1;
}
int main()
{
while (cin >> R >> C){
area = new char* [R];
for (int i = 0; i < R; i++){
area[i] = new char[C + 1];
}
for (int i = 0; i < R; i++)
cin >> area[i];
int res = dfs(0, 0, 1 << (area[0][0] - 'A'));
cout << res << endl;
for (int i = 0; i < R; i++){
delete [] area[i];
}
delete [] area;
}
return 0;
}
poj1154 dfs 深度搜索 集合
最新推荐文章于 2019-07-29 16:53:10 发布