Mr. Frog’s Game

Problem Description
One day, Mr. Frog is playing Link Game (Lian Lian Kan in Chinese).
In this game, if you can draw at most three horizontal or vertical head-and-tail-connected lines over the empty grids(the lines can be out of the whole board) to connect two non-empty grids with the same symbol or the two non-empty grids with the same symbol are adjacent, then you can change these two grids into empty and get several more seconds to continue the game.
Now, Mr. Frog starts a new game (that means there is no empty grid in the board). If there are no pair of grids that can be removed together，Mr. Frog will say ”I’m angry” and criticize you.
Mr. Frog is battle-scarred and has seen many things, so he can check the board in a very short time, maybe one second. As a Hong Kong Journalist, what you should do is to check the board more quickly than him, and then you can get out of the room before Mr. Frog being angry.

Input
The first line contains only one integer T (T≤500), which indicates the number of test cases.
For each test case, the first line contains two integers n and m (1≤n,m≤30).
In the next n lines, each line contains m integers,  j-th number in the i-th line means the symbol on the grid(the same number means the same symbol on the grid).

Output
For each test case, there should be one line in the output.
You should output “Case #x: y”，where x is the case number(starting from 1), and y is a string representing the answer of the question. If there are at least one pair of grids that can be removed together, the y is “Yes”（without quote), else y is “No”.

Sample Input
2
3 3
1 2 1
2 1 2
1 2 1
3 3
1 2 3
2 1 2
3 2 1


Sample Output
Case #1: Yes
Case #2: No

Hint

first sample can be explained as below.



Solution

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define ms(a) memset(a,0,sizeof(a))
typedef long long LL;
const LL LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const int MAXN = 100000 + 10;
const int MOD = 100000;
const double eps = 1e-6;
int gcd(int a, int b)
{
if (!b)return a;
return gcd(b, a%b);
}
using namespace std;
//or2..or2..or2..or2..or2..//
int main()
{
int T, cas = 1; cin >> T;
while (T--)
{
int n, m; cin >> n >> m;
int mp[40][40];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &mp[i][j]);
bool flag = 0;
for (int i = 0; i < n - 1 && !flag; i++)
for (int ii = i + 1; ii < n && !flag; ii++)
if (mp[i][0] == mp[ii][0] || mp[i][m - 1] == mp[ii][m - 1])flag = 1;
for (int j = 0; j < m - 1 && !flag; j++)
for (int jj = j + 1; jj < m && !flag; jj++)
if (mp[0][j] == mp[0][jj] || mp[n - 1][j] == mp[n - 1][jj])flag = 1;
for (int i = 1; i < n - 1 && !flag; i++)
for (int j = 1; j < m - 1 && !flag; j++)
if (mp[i][j] == mp[i][j + 1] || mp[i][j] == mp[i + 1][j])
flag = 1;
if (flag)printf("Case #%d: Yes\n", cas++);
else printf("Case #%d: No\n", cas++);
}
return 0;
}