Hdu 1547 (BFS)

Source: Internet
Author: User

Bubble shooter

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1057 Accepted Submission (s): 454


Problem descriptionbubble shooter is a popular game. You can find a IoT of versions from the Internet.



The goal of this game was to the bubbles off the field. Every time just point the cannon to where you want the next bubble to go, and if three or more about bubbles with the SAM E color came together (including the newly shot bubble), they would detonate. After the first explode, if some bubbles is disconnected from the bubble (s) in the topmost row, they would explode too.

In this problem, you'll be given a arranged situation of bubbles in the field and the newly shot bubble. Your program should output, the total number of bubbles, that would explode.

Inputthere is multiple test cases. Each test case begins with four integers h (the height of the field, 2 <= H <=), W (the width of the field, 2 &L t;= W <=, in the picture above, W are), H (the vertical position of the newly shot bubble, count from top to Bott OM, and the topmost is counted as 1) and W (the horizontal position of the newly shot bubble, count from d The leftmost is counted as 1).
Then H lines follow, the odd lines would contain W characters while the even lines would contain W-1 characters (refer to th e picture above). Each character would be either a lowercase from ' a ' to ' Z ' indicating the color of the bubble in that position, or a capita L Letter ' E ' indicating an empty position. Assure the arranged situation is always valid (all the bubbles be directly or indirectly connected with at least One bubble in the topmost row, and the position of newly shot bubble are never empty).

Outputfor each test case, output a integer indicating how many bubbles would explode.

Sample Input2 2 2 1aaa3 3 3 3AAABABBA3 3 3 1AAABABBA3 3 3 3aaaEaaab

Sample Output3830

Authorjin, Tianpeng

Source Zhejiang Provincial Programming Contest 2006 test instructions: Play Bubble dragon, start is all the balls are connected by six directions, the rule is if the position specified now has at least three balls in six directions to form a connected component, These balls can be eliminated, after the elimination of these balls, if some of the ball is not connected with the topmost layer, then will fall, now give the size of the matrix and the specified position, ask the maximum number of balls can be eliminated? The problem is mainly to understand the bubble Dragon six direction is where, to divide the odd lines and even lines to discuss, If it is an odd line, then he can and (X,y-1), (x,y+1), (x-1,y-1), (X-1,y), (x+1,y-1), (x+1,y) connected, if it is an even line, it can and (x,y-1), (x,y+1), (x+1,y+1), (x+1,y ), (x-1,y+1), (x-1,y) connected, first from the initial position to do a BFS, all the same color and connected ball marker count, and then start from the first row to do a second BFS, all the connected blocks are connected, and finally statistics which are not marked, then the ball will fall down, counting.
#include <cstdio>#include<cstring>#include<queue>#include<algorithm>#include<stdlib.h>using namespacestd;Const intN = -;intn,m,a,b;CharGraph[n][n];BOOLVis[n][n];structnode{intx, y;};BOOLCheckintXintYCharc) {    if(%2){        if(x<1|| x>n| | y<1|| y>m| | vis[x][y]| | graph[x][y]=='E'|| GRAPH[X][Y]!=C)return false; }Else{        if(x<1|| x>n| | y<1|| y>=m| | vis[x][y]| | graph[x][y]=='E'|| GRAPH[X][Y]!=C)return false; }    return true;}BOOLCheck2 (intXinty) {    if(%2){        if(x<1|| x>n| | y<1|| y>m| | vis[x][y]| | graph[x][y]=='E')return false; }Else{        if(x<1|| x>n| | y<1|| y>=m| | vis[x][y]| | graph[x][y]=='E')return false; }    return true;}intdir1[][2]={{0,-1},{0,1},{1,1},{1,0},{-1,0},{-1,1}};///even rowsintdir2[][2]={{0,-1},{0,1},{-1,-1},{-1,0},{1,-1},{1,0}};///Odd RowsvoidBFs () {Queue<Node>Q;    Node s; S.x= A,s.y =b; VIS[S.X][S.Y]=true;    Q.push (s);  while(!Q.empty ()) {Node now=Q.front ();        Q.pop ();        Node Next; if(now.x%2==0){             for(intI=0;i<6; i++) {Next.x= now.x+dir1[i][0]; Next.y= now.y+dir1[i][1]; if(!check (Next.x,next.y,graph[now.x][now.y]))Continue; VIS[NEXT.X][NEXT.Y]=true;            Q.push (next); }        }Else{             for(intI=0;i<6; i++) {Next.x= now.x+dir2[i][0]; Next.y= now.y+dir2[i][1]; if(!check (Next.x,next.y,graph[now.x][now.y]))Continue; VIS[NEXT.X][NEXT.Y]=true;            Q.push (next); }        }    }}voidBFS2 (intk) {Queue<Node>Q;    Node s; S.x=1, S.y =K; VIS[S.X][S.Y]=true;    Q.push (s);  while(!Q.empty ()) {Node now=Q.front ();        Q.pop ();        Node Next; if(now.x%2==0){             for(intI=0;i<6; i++) {Next.x= now.x+dir1[i][0]; Next.y= now.y+dir1[i][1]; if(!check2 (NEXT.X,NEXT.Y))Continue; VIS[NEXT.X][NEXT.Y]=true;            Q.push (next); }        }Else{             for(intI=0;i<6; i++) {Next.x= now.x+dir2[i][0]; Next.y= now.y+dir2[i][1]; if(!check2 (NEXT.X,NEXT.Y))Continue; VIS[NEXT.X][NEXT.Y]=true;            Q.push (next); }        }    }}intMain () { while(SCANF ("%d%d%d%d", &n,&m,&a,&b)! =EOF) {memset (graph,0,sizeof(graph));  for(intI=1; i<=n;i++) {scanf ("%s", graph[i]+1); } memset (Vis,false,sizeof(VIS));        BFS (); intAns =0;  for(intI=1; i<=n;i++){             for(intj=1; j<=m;j++){                if(Vis[i][j]) ans++; }        }        if(ans<3) {printf ("%d\n",0); Continue; }         for(intI=1; i<=m;i++){            if(graph[1][i]=='E'|| vis[1][i])Continue;        BFS2 (i); }         for(intI=1; i<=n;i++){             for(intj=1; j<=m;j++){                if(i%2==0&AMP;&AMP;J==M)Continue; if(graph[i][j]=='E')Continue; if(!vis[i][j]) ans++; }} printf ("%d\n", ans); }}

Hdu 1547 (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.