Dearboy ' s Puzzle (POJ 2308 search Dfs+bfs)

Source: Internet
Author: User



Language:DefaultDearboy ' s Puzzle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1202 Accepted: 208

Description

Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game was played on a board with n*m grids, and lots of cards was put on the board in the grids. Should find a pair of the same cards, if not more than three segments can link this pair without passing any other car DS, you can take this pair away from the board. (You are puzzled about the meaning for not more than 3 segments, just refer to the figure below, in which we express som e allowable links). Continue the process above, if you can clear all the cards, you win the game and otherwise you lose it.

If you had played this game, you may know that sometimes the game had no solution and you were sure to lose. Dearboy is very boring on the games without solutions, so he asks you, a famous programmer, to tell him whether he can Win the Giving game phase or not.

Input

The input consists of multiple test cases. The first line of all test case contains, integers n and m (2 <= N, M <=), which denote the sizes of the game Board. The next N lines give the board layout, with all line containing M characters. A character is one of the following: ' * ' (an empty position), ' A ', ' B ', ' C ', ' D ' (the cards, which imply that there was at Most 4 different kinds of cards). Different letters represent Different cards. The number of same cards is odd, and there may is more than one pair of the same cards.

The input is terminated with the 0 ' s. This test case is shoud not being processed.

Output

If Dearboy can win the game, the "no" otherwise, for each test case, print in one line "yes".

Sample Input

6 8*********a**c*****b********b*d******d***********2 2abba6 8***a*****a**c*****b***c****b*d******d***********0 0

Sample Output

Nonoyes

Source

POJ Monthly,wang Yijie


Test instructions: Repeatedly watching the game, given a situation to determine the final can be eliminated.

Idea: Using DFS to determine which lattice to remove (x, y), in Dfs, BFS, see (x, y) around how many grids can and (x, y) disappear, and then use DFS enumeration with which to eliminate or current (x, y).

Note A pruning that exists in the following case is certainly not in accordance with test instructions:

*********

ab***

ba***

*********

In addition, pay attention to the elimination method of test instructions, connect up to only two bends.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 1005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO  N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long ll;using Nam Espace std;struct node{int x,y,turn,w,d;//turn is goHow many bends have been turned at the current (x, y), and D is (x, y) the direction of the previous step};int dir[4][2]={1,0,0,1,0,-1,-1,0};int mp[11][11],num[4];int N,m;char str[11]    ; bool Flag;bool isOK (int x,int y) {if (x>=0&&x<n&&y>=0&&y<m) return true; return false;} BOOL IsOK () {for (int. i=0;i<n;i++) {for (int j=0;j<m;j++) {if (mp[i][j]!=-1&&am p;mp[i][j+1]!=-1&&num[mp[i][j]]==num[mp[i][j+1]]&&num[mp[i][j]]==2) {if (mp[i][            J]==MP[I+1][J+1]&AMP;&AMP;MP[I][J+1]==MP[I+1][J]) return true; }}} return false;}    void BFs (int x,int y,int v,int s[25][2],int &nn) {nn=0;    queue<node>q;    Node St,now; St.x=x; St.y=y; St.w=-1; st.turn=0;    St.d=-1;    BOOL Vis[11][11];    memset (vis,false,sizeof (VIS));    Vis[x][y]=true;    Q.push (ST); while (! Q.empty ()) {St=q.front ();        Q.pop ();      if (st.w==v) {s[nn][0]=st.x; Record the coordinates of the found lattice that can be eliminated[nn++]            [1]=st.y;        Continue            } for (int i=0;i<4;i++) {now.x=st.x+dir[i][0];            NOW.Y=ST.Y+DIR[I][1]; if (isOK (NOW.X,NOW.Y) &&!vis[now.x][now.y]) {if (mp[now.x][now.y]!=-1&&mp[now.x]                [Now.y]!=v] continue; if (st.d==i| |                St.d==-1) Now.turn=st.turn;                else now.turn=st.turn+1;                    if (now.turn<=2) {NOW.W=MP[NOW.X][NOW.Y];                    Now.d=i;                    Vis[now.x][now.y]=true;                Q.push (now);    }}}}}void dfs (int cnt) {if (flag) return;        if (cnt==0) {flag=true;    Return        } if (IsOK ()) return;                Pruning for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (mp[i][j]!=-1) {                int s[25][2];             int sum;   int V=MP[I][J];                BFS (i,j,v,s,sum);                num[v]-=2;                Mp[i][j]=-1;                    for (int k=0;k<sum;k++) {int x=s[k][0];                    int y=s[k][1];                    Mp[x][y]=-1;                    DFS (CNT-2);                Mp[x][y]=v;                } num[v]+=2;            Mp[i][j]=v;    }}}}int Main () {#ifndef Online_judge freopen ("C:/users/asus1/desktop/in.txt", "R", stdin), #endif int i,j;        while (scanf ("%d%d", &n,&m)) {if (n==0&&m==0) break;        Flag=false;        MEM (num,0);        MEM (mp,-1);        int all=0;            for (i=0;i<n;i++) {scanf ("%s", str);                for (j=0;j<m;j++) {if (str[j]== ' * ') mp[i][j]=-1;                    else if (str[j]== ' A ') {num[0]++;                    mp[i][j]=0;                all++; } elsE if (str[j]== ' B ') {num[1]++;                    Mp[i][j]=1;                all++;                    } else if (str[j]== ' C ') {num[2]++;                    mp[i][j]=2;                all++;                    } else{num[3]++;                    mp[i][j]=3;                all++; }}} if (num[0]%2| | num[1]%2| | num[2]%2| |            num[3]%2)//There is a certain non-requirement {PF ("no\n") when there is an odd number;        Continue        } dfs (all);        if (flag) PF ("yes\n");    Else PF ("no\n"); } return 0;}



Language:DefaultDearboy ' s Puzzle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1202 Accepted: 208

Description

Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game was played on a board with n*m grids, and lots of cards was put on the board in the grids. Should find a pair of the same cards, if not more than three segments can link this pair without passing any other car DS, you can take this pair away from the board. (You are puzzled about the meaning for not more than 3 segments, just refer to the figure below, in which we express som e allowable links). Continue the process above, if you can clear all the cards, you win the game and otherwise you lose it.

If you had played this game, you may know that sometimes the game had no solution and you were sure to lose. Dearboy is very boring on the games without solutions, so he asks you, a famous programmer, to tell him whether he can Win the Giving game phase or not.

Input

The input consists of multiple test cases. The first line of all test case contains, integers n and m (2 <= N, M <=), which denote the sizes of the game Board. The next N lines give the board layout, with all line containing M characters. A character is one of the following: ' * ' (an empty position), ' A ', ' B ', ' C ', ' D ' (the cards, which imply that there was at Most 4 different kinds of cards). Different letters represent Different cards. The number of same cards is odd, and there may is more than one pair of the same cards.

The input is terminated with the 0 ' s. This test case is shoud not being processed.

Output

If Dearboy can win the game, the "no" otherwise, for each test case, print in one line "yes".

Sample Input

6 8*********a**c*****b********b*d******d***********2 2abba6 8***a*****a**c*****b***c****b*d******d***********0 0

Sample Output

Nonoyes

Source

POJ Monthly,wang Yijie

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dearboy ' s Puzzle (POJ 2308 search Dfs+bfs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.