题干:

After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements:

  • There is at least one digit in the string,
  • There is at least one lowercase (small) letter of the Latin alphabet in the string,
  • There is at least one of three listed symbols in the string: '#', '*', '&'.

Considering that these are programming classes it is not easy to write the password.

For each character of the password we have a fixed string of length m, on each of these n strings there is a pointer on some character. The i-th character displayed on the screen is the pointed character in the i-th string. Initially, all pointers are on characters with indexes 1 in the corresponding strings (all positions are numbered starting from one).

During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index m, and when we move it to the right from the position m it moves to the position 1.

You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password.

Input

The first line contains two integers nm (3 ≤ n ≤ 50, 1 ≤ m ≤ 50) — the length of the password and the length of strings which are assigned to password symbols.

Each of the next n lines contains the string which is assigned to the i-th symbol of the password string. Its length is m, it consists of digits, lowercase English letters, and characters '#', '*' or '&'.

You have such input data that you can always get a valid password.

Output

Print one integer — the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.

Examples

Input

3 4
1**2
a3*0
c4**

Output

1

Input

5 5
#*&#*
*a1c&
&q2w*
#a3c#
*&#*&

Output

3

Note

In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.

In the second test one of possible algorithms will be:

  • to move the pointer of the second symbol once to the right.
  • to move the pointer of the third symbol twice to the right.

 

题目大意:

    有 n 个字符串,每个字符串的长度是m,如果认定一个字符串是一个密码,则必须满足:

1:至少有1个数字。2:至少有一个小写字母。3:至少有一个 #、*或&

现在有n个光标,每个光标指向这 n 个字符串。现在可以移动光标,最后使得所有光标指向的字符能组成一个密码。(字符串是循环的)

题目保证一定有解。

解题报告:

    这题可以直接暴力啊。看了下数据范围,o(n^3)不超,想法造一个三次方的循环暴力出来?正好题目三个条件,所以每一层循环就是一个条件不就好了?

 

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 1000000;
char s[55][55];
int shu[55],zimu[55],cc[55];
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i = 1; i<=n; i++) {
		scanf("%s",s[i]+1);
		shu[i] = zimu[i] = cc[i] = INF;
	}
//	memset(shu,INF,sizeof shu);
//	memset(zimu,INF,sizeof zimu);
//	memset(cc,INF,sizeof cc);
	for(int i = 1; i<=n; i++) {
		for(int j = 1; j<=m; j++) {
			if(s[i][j] >='0' && s[i][j] <= '9') {
				shu[i] = min(shu[i],min(m-j+1,j-1));
			}
		}
//		printf("%d\n",shu[i]);
	}
	for(int i = 1; i<=n; i++) {
		for(int j = 1; j<=m; j++) {
			if(s[i][j] >='a' && s[i][j] <= 'z') {
				zimu[i] = min(zimu[i],min(m-j+1,j-1));
			}
		}
//		printf("%d\n",zimu[i]);
	}	
	for(int i = 1; i<=n; i++) {
		for(int j = 1; j<=m; j++) {
			if(s[i][j] == '#' || s[i][j] == '*' || s[i][j] == '&' ) {
				cc[i] = min(cc[i],min(m-j+1,j-1));
			}
		}
//		printf("%d\n",cc[i]);
	}		
	int ans = INF;
	for(int i = 1; i<=n; i++) {
		for(int j = 1; j<=n; j++) {
			for(int k = 1; k<=n; k++) {
				if(i == j || i == k || j == k) continue;
//				if(ans == 1) printf("%d  %d  %d\n",i,j,k);
				ans = min(ans,shu[i]+zimu[j]+cc[k]);
			}
		}
	}
	printf("%d\n",ans);
	return 0 ;
}
//3 5
//*****
//1***a
//**a**

总结:

   1WA在没有判断三者处在同一行的情况。

   2WA在INF设置的值过大了。。因为这里是三个值相加啊,所以可以考虑一下1<<29之类的数。

  题目保证一定有解的类型,考虑一下预处理?

这题标解貌似是记忆化搜索?(链接)不过其实复杂度貌似也差不多、、

#include<bits/stdc++.h>
using namespace::std;
 
const int N=55;
const int INF=0x3f3f3f3f;
 
int n, m, dp[N][2][2][2];
char s[N][N];
 
bool isdigit(char ch)
{
    if(ch>='0' && ch<='9')
        return true;
    return false;
}
 
bool isletter(char ch)
{
    if(ch>='a' && ch<='z')
        return true;
    return false;
}
 
bool issymbols(char ch)
{
    if(ch=='*' || ch == '&' || ch == '#')
        return true;
    return false;
}
 
int solve(int pos, bool digit, bool letter, bool symbols)
{
    if(pos==n)
    {
        if(digit && letter && symbols)
            return 0;
        return INF;
    }
    if(dp[pos][digit][letter][symbols] != -1)
        return dp[pos][digit][letter][symbols];
    int ans=INF;
    for(int j=0; j<m; j++)
    {
        int cost = min(j, m-j);
        if(isdigit(s[pos][j]))
            ans = min(ans, cost+ solve(pos+1, 1, letter, symbols));
        else if(isletter(s[pos][j]))
            ans = min(ans, cost+ solve(pos+1, digit, 1, symbols));
        else if(issymbols(s[pos][j]))
            ans = min(ans, cost+ solve(pos+1, digit, letter, 1));
    }
    return dp[pos][digit][letter][symbols]=ans;
}
 
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
        scanf("%s",s[i]);
    memset(dp, -1, sizeof(dp));
    printf("%d\n",solve(0,0,0,0));
    return 0;
}