1. 程式人生 > >Mr. Frog’s Game(模擬連連看)

Mr. Frog’s Game(模擬連連看)

nal ole -c amp sam sdi into pri day

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 \leq 500$), which indicates the number of test cases.

For each test case, the first line contains two integers n and m ($1 \leq n,m \leq 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. 
          
技術分享圖片

題目意思:這是一個連連看遊戲,只要矩陣內部有兩個相同圖案相鄰或者外圍(四條最外邊)上只少有兩個相同的圖案,就可以進行一次消去,問能否進行一次消去。

解題思路:大致分為兩種情況:1矩陣內部有相鄰的圖案,2矩陣外圍上至少有兩個相同的圖案。直接暴力遍歷這個矩陣即可。

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     int t,i,j,n,m,flag,count=1;
 6     int a[32][32];
 7     scanf("%d",&t);
 8     while(t--)
 9     {
10         flag=0;
11 
12         memset(a,0,sizeof(a));
13         scanf("%d%d",&n,&m);
14         for(i=1; i<=n; i++)
15         {
16             for(j=1; j<=m; j++)
17             {
18                 scanf("%d",&a[i][j]);
19             }
20         }
21         for(i=1; i<m; i++)
22         {
23             for(j=i+1; j<=m; j++)
24             {
25                 if(a[1][i]==a[1][j]||a[n][i]==a[n][j])
26                 {
27                     flag=1;
28                 }
29             }
30             if(flag==1)
31             {
32                 break;
33             }
34         }
35         for(i=1; i<n; i++)
36         {
37             for(j=i+1; j<=n; j++)
38             {
39                 if(a[i][1]==a[j][1]||a[i][m]==a[j][m])
40                 {
41                     flag=1;
42                 }
43             }
44             if(flag==1)
45             {
46                 break;
47             }
48         }
49         for(i=1; i<=n; i++)
50         {
51             for(j=1; j<=m; j++)
52             {
53                 if(i!=n&&a[i][j]==a[i+1][j])
54                 {
55                     flag=1;
56                 }
57                 if(j!=m&&a[i][j]==a[i][j+1])
58                 {
59                     flag=1;
60                 }
61             }
62             if(flag==1)
63             {
64                 break;
65             }
66         }
67         if(flag==1)
68         {
69             printf("Case #%d: Yes\n",count);
70             count++;
71         }
72         else
73         {
74             printf("Case #%d: No\n",count);
75             count++;
76         }
77     }
78     return 0;
79 }

Mr. Frog’s Game(模擬連連看)