Mr. Frog’s Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 312 Accepted Submission(s): 209
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 * 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.
), 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).
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”.
3 3
1 2 1
2 1 2
1 2 1
3 3
1 2 3
2 1 2
3 2 1
Case #2: No
first sample can be explained as below.
/*
这个题可以搜索,但是30*30完全可以暴力
*/
#include<bits/stdc++.h>
using namespace std;
int pa[][];
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
int T,nl = ,N,M,i,j,pl;
scanf("%d",&T);
while(T--)
{
pl = ;
scanf("%d%d",&N,&M);
for(i = ; i <= N; i++)
for(j = ; j <= M; j++)
scanf("%d",&pa[i][j]);
for(i = ; i < M ; i++)///判断边上是不是有相同的
{
for(j = i + ; j <= M ; j++)
if((pa[][i] == pa[][j]) || (pa[N][i] == pa[N][j]))
pl = ;
if(pl) break;
}
if(!pl)
{
for(i = ; i < N ; i++)///判断是不是有相连的有相同的
{
for(j = i + ; j <= N ; j++)
if((pa[i][] == pa[j][]) || (pa[i][M] == pa[j][M]))
pl = ;
if(pl) break;
}
if(!pl)
{
for(i = ; i <= N ; i++)///判断是不是有相连的有相同的
{
for(j = ; j <= M ; j++)
{
if(i!= N && pa[i][j] == pa[i + ][j]) pl = ;
if(j!= M && pa[i][j] == pa[i][j + ]) pl = ;
}
if(pl) break;
}
}
}
if(pl)
printf("Case #%d: Yes\n",++nl);
else
printf("Case #%d: No\n",++nl);
}
return ;
}