当前位置: 代码迷 >> 综合 >> HDU 5926 Mr. Frog’s Game(连连看,暴力)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛
  详细解决方案

HDU 5926 Mr. Frog’s Game(连连看,暴力)——2016CCPC东北地区大学生程序设计竞赛 - 重现赛

热度:31   发布时间:2023-12-12 09:20:40.0

此文章可以使用目录功能哟↑(点击上方[+])

 HDU 5926 Mr. Frog’s Game

Accept: 0    Submit: 0
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

 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.

 Problem Idea

解题思路:

【题意】
给你一个n*m的矩阵,进行连连看游戏

问矩阵中是否存在能够消除的一对

【类型】
连连看,暴力
【分析】
相信玩过连连看的小伙伴,这题并没有什么难度

但是,没玩过连连看的也不要紧,接下来我来具体讲一下规则:

①连连看只能消除相同的一对

即不管你是数字连连看,还是图像连连看,我们每次只能移除两个相同的数字或图像


也就是说上图中,红色框框内是可以消除的,而蓝色框框则不行

②连连看可以消除相邻的一对相同数字或图像

如上图中红框内的一对4就是相邻的

③连连看可以消除三段线段及以内的相同数字或图像


如上图,红框内的一对1可以消除,因为恰好为3段线段相连

而蓝框内的一对2则无法消除,因为已经超过3段线段

综上所述,此题由于是满矩阵的,所以只需判断矩阵最外层四边中至少有一条边存在两个相同的数 or 矩阵中存在两个相邻数相同

就能断定矩阵中存在能够消除的一对

而这个暴力就可以做到

【时间复杂度&&优化】
O(N*M)

题目链接→HDU 5926 Mr. Frog’s Game

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 35;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
int s[N][N];
map<int,int> q;
int main()
{int t,n,m,p=1,i,j;bool flag;scanf("%d",&t);while(t--){flag=false;scanf("%d%d",&n,&m);for(i=1;i<=n;i++)for(j=1;j<=m;j++)scanf("%d",&s[i][j]);q.clear();for(i=1;i<=n;i++){q[s[i][1]]++;if(q[s[i][1]]>1){flag=true;break;}}q.clear();for(i=1;i<=n;i++){q[s[i][m]]++;if(q[s[i][m]]>1){flag=true;break;}}q.clear();for(i=1;i<=m;i++){q[s[1][i]]++;if(q[s[1][i]]>1){flag=true;break;}}q.clear();for(i=1;i<=m;i++){q[s[n][i]]++;if(q[s[n][i]]>1){flag=true;break;}}for(i=1;i<=n&&!flag;i++)for(j=1;j<=m&&!flag;j++)if(s[i][j]==s[i-1][j]||s[i][j]==s[i][j-1]){flag=true;break;}if(flag)printf("Case #%d: Yes\n",p++);elseprintf("Case #%d: No\n",p++);}return 0;
}
菜鸟成长记

  相关解决方案