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
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
题意:相同的格子只有相连或者在同一台边上的才能消掉,问图中是否有能消掉的格子。
就判断边上和内部就行了;
<span style="font-family:Arial;font-size:12px;">#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n,m,g[50][50];
int f[4][2]= {{0,1},{1,0},{-1,0},{0,-1}};
bool dfs()
{
for(int i=1; i<=n; i++) //shu
for(int j=i+1; j<=n; j++)
if(g[i][1]==g[j][1])
return true;
for(int i=1; i<=n; i++) //shu
for(int j=i+1; j<=n; j++)
if(g[i][m]==g[j][m])
return true;
for(int i=1; i<=m; i++) //heng
for(int j=i+1; j<=m; j++)
if(g[1][i]==g[1][j])
return true;
for(int i=1; i<=m; i++) //heng
for(int j=i+1; j<=m; j++)
if(g[n][i]==g[n][j])
return true;
for(int i=2; i<n; i++)
{
for(int j=2; j<m; j++)
{
for(int k=0; k<4; k++)
{
int x=f[k][0]+i,y=j+f[k][1];
if(g[i][j]==g[x][y])
return true;
}
}
}
return false;
}
int main()
{
int T;
cin>>T;
for(int t=1; t<=T; t++)
{
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d",&g[i][j]);
printf("Case #%d: ",t);
if(dfs())
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
</span>