Bfs_ Watch the game repeatedly

Source: Internet
Author: User
Tags printf time limit
Problem I:bfs_ Watch the game repeatedlyTime limit: 2 Sec memory limit: Ten MB
Submissions: 207 Resolution: 27
Submitted State [Discussion Version] Title Description

We've all played and watched. Today we play a similar game. In a rectangle composed of 10*10 squares, there are N (n<=10) pairs of characters (they are the first n in uppercase characters). Some of the positions in the rectangle can be traversed from above, while others cannot. A place to walk with '. ' Identification, cannot be identified with ' # '. If 2 of the same characters are connected (from one character to another, be careful to go up, down, left, and right). When there are other characters in a location, this position is not allowed to go), then the pair of characters can be paired. If you pair this pair of characters, the pair will be removed from the rectangle, meaning that the 2 characters are in the same position that they are moving around for other characters.

Now the question is: Please decide the pairing order of these characters (only pairing can be paired), so that the n pairs of characters are finally paired successfully. input

First, a positive integer t (t<=10) is given, which indicates that there is a T group of test data.
Each set of test data consists of 10 rows, each with 10 characters. These characters can only be '. ', ' # ', or the first n in uppercase characters. No more than 10 pairs of characters in each set of test data.
Output

If the n pairs of characters in each set of test data can be paired successfully, the order of pairing is output. If there are multiple pairings in the order of success, the output dictionary is the smallest group.
Otherwise output "My god!".
Sample Input

2
ABF
.... CE ...
..
D ..... ..........
..........
..........
..........
......... D .....
EC
..... FBA
ABF
.... CE ...
..
D ..... ..........
..........
............
#D ................
EC
..... FBA
Sample Output
DCABEF
My god!
TipsThe level of digestion teacher to the sample code, after reading, he played again, when the test found a lot of problems, slowly dubug, found that the understanding of the process deepened a lot. But at the same time found that their ability is still very weak, the idea has been given out, their own to achieve, even wrong many times. We have to strengthen training. The following code, given the comment, spoke a bit of a mess
#include <iostream> #include <cstdio> #include <cstring> using namespace std;
    struct Q {int x;
int y;
};
Q queu[10000];
int n;
Char map[16][16];
int visti[16][16]; int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
    Direction array int bfs (int x,int y) {memset (visti,0,sizeof (Visti)); int f=1;//head int r=1;
    Tail queu[f].x=x;
    Queu[f].y=y;
    Visti[x][y]=1;
    BOOL Flag=true;
        while (f<=r && flag) {int x1=queu[f].x;
        int y1=queu[f].y;
            for (int i=0; i<4; i++) {int newx=x1+dir[i][0];
            int newy=y1+dir[i][1]; if (newx<10 && newx >=0 && newy<10 && newx >=0 &&!visti[newx][newy] &&amp ; (map[newx][newy]== '. ' | | map[newx][newy]==map[x][y]))
                {visti[newx][newy]=1;
                ++r;
                QUEU[R].X=NEWX;
                Queu[r].y=newy; if (Map[newx][newy]==map[x][y]) {map[newx][newy]='.';
                    Map[x][y]= '. ';
                    Flag=false;
                Break
    }}} f++;
    } if (!flag) return 1;

else return 0;
    } int main () {scanf ("%d", &n);
    int i,j,k;
    BOOL ENDL1,ENDL2;
    int counts;
    while (GetChar ()! = ' \ n ');
        while (n--) {char ans[100];
        for (i=0; i<10; i++) scanf ("%s", Map[i]);
        counts=0;
        Endl2=false;   while (!ENDL2) {endl2=true; After 26 letters have been traversed, they have not found the letters that can be erased, then end; for (i=0; i<25&&endl2; i++) {//26 Alphabetical search by dictionary order Endl
                1=true; for (j=0; j<10&&endl1; j + +) for (k=0; k<10&& endl1; k++) {//traverse the map, if any, then BFS, see
                                Whether or not to erase if (map[j][k]==i+65) {if (BFS (j,k))////If it can be eliminated, iterate from the beginning
                            Endl1=endl2=false;
     Else                           Endl1=false; If the letter cannot be erased, then i++, traversing the next letter,}}} if (!ENDL2) {//the letter can be erased,  Save to ans array in ans[++counts]=i-1;
            The reason is i-1, because in the For loop, the last I also performed a i++; }} for (i=0; i<10&&endl2; i++) for (j=0; j<10&&endl2; j + +)//The existing non-erased letters,
                Jump out immediately.
        if (map[i][j]!= '. ' && map[i][j]!= ' # ') Endl2=false;
        if (!ENDL2) printf ("My god!");
        else for (I=1; i<=counts; i++) printf ("%c", ans[i]+65);
    printf ("\ n");
 }
}


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.