hdu 5926:Mr. Frog’s Game

http://acm.hdu.edu.cn/showproblem.php?pid=5926

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.

题意分析:

判断有没有相同的数字能否经过小于等于两次转弯的直线连接。

解题思路:

给出的矩阵是满的,所以只有相邻或者在外围才能符合条件,判断一下是否有相邻的情况,再判断一下四周有没有相同的

#include <stdio.h>
#include <algorithm>
#define N 50
using namespace std;
int a[N][N];
int n, m;
int dfs(int x, int y)
{
	int nex[4][2]={0,1, 0,-1, 1,0, -1,0};
	for(int i=0; i<4; i++)
	{
		int tx=x+nex[i][0];
		int ty=y+nex[i][1];
		if(tx<=0 || ty<=0 || tx>n || ty>m)
			continue;
		if(a[x][y]==a[tx][ty])
			return 1;
	}
	return 0;
}
int dfs1()
{
	int b[N], i;
	for(i=1; i<=n; i++)
		b[i]=a[i][1];
	sort(b+1, b+n+1);
	for(i=2; i<=n; i++)
		if(b[i]==b[i-1])
			return 1;
		
	for(i=1; i<=n; i++)
		b[i]=a[i][m];
	sort(b+1, b+n+1);
	for(i=2; i<=n; i++)
		if(b[i]==b[i-1])
			return 1;
			
	for(i=1; i<=m; i++)
		b[i]=a[1][i];
	sort(b+1, b+m+1);
	for(i=2; i<=m; i++)
		if(b[i]==b[i-1])
			return 1;
	
	for(i=1; i<=m; i++)
		b[i]=a[n][i];
	sort(b+1, b+m+1);
	for(i=2; i<=m; i++)
		if(b[i]==b[i-1])
			return 1;
}
int main()
{
	int T, t=0, i, j;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d%d", &n, &m);
		for(i=1; i<=n; i++)
			for(j=1; j<=m; j++)
				scanf("%d", &a[i][j]);
		int temp=0;
		for(i=1; i<=n; i++)
		{
			for(j=1; j<=m; j++)
			{
				if(dfs(i, j))
				{
					temp=1;
					break;
				}	
			}
			if(temp==1)
				break;
		}
		if(temp==0)
		{
			temp=dfs1();
		}
		if(temp==1)
			printf("Case #%d: Yes
", ++t);
		else printf("Case #%d: No
", ++t);		 	
	}
	return 0;
}
原文地址:https://www.cnblogs.com/zyq1758043090/p/11852532.html