啊这,怎么说好呢,给我练习map用法的题。。
最后一个数据QOO还卡一下,笑死我了
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 55 ;
unordered_map<string,int> mhash ;
char s[N][N] ;
// 4个方向找,可以接受MOO和OOM的形式
// 左下,下,右下,右
int dx[4] = {1, 1, 1, 0} ;
int dy[4] = {-1, 0, 1, 1} ;
int n , m ;
bool in(int x , int y )
{
if(x < 0 || y < 0 || x >= n || y >= m ) return false ;
return true ;
}
void check(int x , int y )
{
// 对 当前xy位置进行检测,看看能否组成MOO或者OOM
char p = s[x][y] ;
for(int i = 0 ; i < 4 ; i ++ )
{
bool flag = true ;
char pp[3] ;
for(int j = 1 ; j <= 2 ; j ++ ){
int nx = x + dx[i] * j , ny = y + dy[i] * j ;
if(!in(nx,ny) ){
flag = false ;
break ;
}
pp[j] = s[x + dx[i] * j ][y + dy[i] * j] ;
}
if(!flag ) continue ;
// 这个方向合法
string temp1 ,temp2;
temp1 = temp2 = p ;
temp1 += pp[1] ;
temp1 += pp[2] ;
temp2 = pp[1] + temp2 ;
temp2 = pp[2] + temp2 ;
if(pp[1] == pp[2] && p != pp[1])
mhash[temp1] ++ ;
if(p == pp[1] && pp[1] != pp[2])
mhash[temp2] ++ ;
}
}
int main()
{
cin >> n >> m ;
for(int i = 0 ; i < n ; i ++)
cin >> s[i] ;
for(int i = 0 ; i < n ; i ++)
for(int j = 0 ; j< m ; j ++ )
check(i,j) ;
int ans = 0 ;
for(auto it : mhash )
{
// 特殊的,映射到自身是不合法的
// 就是*OO和M**不合法
string temp = it.first ;
if(temp[0] == 'M' || temp[1] == 'O') continue ;
ans = max(ans , it.second ) ;
// cout << it.first << ' ' << it.second << endl ;
}
cout << ans << endl ;
return 0 ;
}
还好只有MOO,不然我的枚举大法就要失效了(逃)