FZU1892 Riser game-BFS plus simple state compression and bit arithmetic processing

Source: Internet
Author: User
Tags bitwise

Original title address: http://acm.fzu.edu.cn/problem.php?pid=1892

problem 1892 riser Gameaccept:108 submit:498
Time limit:1000 mSec Memory limit:32768 KB problem Description

the rules of the water connection game are as follows:
1. There are two special water pipes on n*n size squares, respectively, inlet and outlet;
2, there are 7 kinds of 1*1 size of the water pipe needs to be placed in the n*n size of the box, so that the water flow from the water inlet through the water flow to the outlet;
3, when the water flow to the outlet or water flow to the outlet before the outflow of water pipes, then the game is over;
4, the game score for the water flow through the number of water pipes (except water inlet and outlet);
5, the game has four directions, respectively, indicating the main and cardinal, can be used to indicate the direction of water flow in the water pipe, 1 can be to the east flow, 2 indicates that it can flow to the South, 4 can flow to the West, 8 to the Beiliu;
6. The shape of seven water pipes is as follows:: Number 15;: 9;: 6;: 3;: 12;: 10;: number 5;
7, the number of water inlet needs to add 16, the number of the outlet needs to add 32, the water inlet and water flow in the mouth only one Direction, the game has only one inlet and a water outlet;
8, every moment, the flow will flow along the water stream to the adjacent box, if the box does not have a water pipe or pipe joints do not correspond, then the water flow will flow out of the pipe.
now that the water pipe has been picked up, please calculate the score you can get in the current situation.

Input

the first behavior of the input data is an integer t, which indicates that there is a T group input data. The first behavior of each set of input data is an integer n (5≤n≤50), which indicates the size of the square, the following n rows, each row has n digits mij (1≤i,j≤n), which indicates the water pipe number in the column J of Row I, and mij=0 that there is no water pipe in the square.

Output

for each set of data, the output is an integer that represents the number of points that can be obtained.

Sample Input253 5 5 6 010 3 5 15 610 10 0 10 4010 9 5 12 189 5 5 5 1253 5 5 6 010 0 0 15 610 10 0 0 4010 9 5 12 189 5 5 5 Sample Output1814 Hint
SOURCE Fuzhou University seventh session Program design Competition



This problem can be known as BFS, and the data is small, will not time out. But the interface problem how to deal with, this is the key.


The problem is clearly that the tube is state compression. Title 5th: There are four directions in the game, respectively, indicating the main and cardinal, can be used to indicate the direction of water flow in the water pipe, 1 to the east flow, 2 to the South flow, 4 to the West flow, 8 to the Beiliu. 1,2,4,8 is the binary number of 4 bits, number 15 of the pipe, is 1+2+4+8=15, indicating that the water pipe leads to four directions. Similarly, the tube numbered 9, 1+8=9, leads to the east and north. So the analogy.


So the question is, how do you get to the direction of a pipe? Before learning the point of the pressure of the Foundation can know, using a bitwise operation.


Bitwise operations: <<, left shift operation, the number of binary number to the left n bits, left discard, 0 on the right. 1<<n is to move 1 to the left N-bit, left to discard, 0 on the right, its value is equivalent to 1* (2^n). For example, 1<<3, originally 1 of the binary number is 0001, left 3 bits, changed to 1000, equals 8,2^3.


Bitwise operations: &, and bitwise operations, two digits of the binary number bitwise AND operation, the same as 1, the bit is 1, otherwise 0. As follows:

00101
11100
----------------
00100



Then, combined with these two operations, you can tell which direction the tube leads. The implementation code is as follows:

for (i=0; i<4; i++)
{
if ((n& (1<<i)) >0)
{

The corresponding binary I bit is 1, which means the direction of this bit can be circulated.

}

}




Then next, judge the adjacent two pipe can be connected, it is good to do. is to determine which direction the pipe leads to, and then to determine the direction of the adjacent pipe can also lead to the opposite direction.


The pipe on the left is facing east, and the pipe to the east of it leads to the west, so they connect. The implementation code is as follows:


Maps are stored in two-dimensional int

int MAP[50][50];

Defines the structure, now, which represents the current coordinates;

The array used to move

int xx[4]= {0,1,0,-1};
int yy[4]= {1,0,-1,0};


for (i=0; i<4; i++)
{
if ((maps[now.x][now.y]& (1<<i)) >0)
{
F ((maps[now.x+xx[i] [Now.y+yy[i]] & (1<< ((2+i)%4)) >0)
{

Connected!! (There is no feeling jiangzi very burning ~2333)

}

}

}



After this, and then use BFS is no problem ~ But the bit operation let me very big, the details of the problem let me debug for a long time, the game did not make out, the same night changed to 1 o'clock in the morning more, but still not right, or the next day to change the right ...


The AC code is as follows:

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue >using namespace Std;int maps[50][50];bool book[50][50];int xx[4]= {0,1,0,-1};int yy[4]= {1,0,-1,0};int t,n;int x1,x2 , Y3,y2,i,j;int flag=0;int sum=0;int k=0;int num=0;struct node{int x,y,com,step;};    Node Now;node next;queue <node> qq;int Main () {scanf ("%d", &t);        while (t--) {scanf ("%d", &n);            {num=0; Note that both the entrance and exit are in one direction.                    Minus 16 or 32 is the exit direction for the pipe for (i=0; i<n; i++) {for (j=0; j<n; J + +) {                    scanf ("%d", &maps[i][j]);                        if (maps[i][j]>=32) {maps[i][j]-=32;                        X2=i;                    Y2=j;                        } else if (maps[i][j]>=16) {maps[i][j]-=16;                        X1=i; Y3=j;                    } if (maps[i][j]!=0) {num++; }}}/* for (int i=0;i<n;i++) {for (int j=0;j                <n;j++) {printf ("%d", maps[i][j]);            } printf ("\ n");            }//* * NEXT.X=X1;            Next.y=y3;            Next.step=0;            flag=0x3f3f3f3f;//is greater than 10^9 and in the range of int, it is very useful to have a maximum value of sum=0;                    for (i=0; i<4; i++) {if ((maps[x1][y3]& (1<<i)) >0) {                    Next.com= (i+2)%4;//to determine the starting point of the water direction (the starting point in the direction of the opposite direction), for the later use//printf ("%d\n", next.com);                Break            }} qq.push (next);            memset (book,0,sizeof (book));                while (!qq.empty ()) {Now=qq.front ();     Qq.pop ();           printf ("$%d\n", k++); Determine if the game ends in two steps (to the end point, or water to the pipe) for (i=0; i<4; i++) {if (Maps[now.x][now. y]& (1<<i)) >0)//Find all directions//Not for the end point, do not cross over, and can connect if (((Now.x+xx[i])!=x2| | (Now.y+yy[i])!=y2) && ((Now.x+xx[i]) >=0) && ((Now.x+xx[i]) <n) && ((now.y+yy[i)) >=0)                    && ((Now.y+yy[i]) <n) && ((Maps[now.x+xx[i]][now.y+yy[i]) & (1<< ((2+i))%4) >0)                            {for (j=0; j<4; j + +)//The next step is still not the end point, does not cross the line, can connect { if ((maps[now.x+xx[i]][now.y+yy[i]]& (1<<j)) >0) if (((now.x+xx[i]+xx[j))!=x2| | (Now.y+yy[i]+yy[j])!=y2) && ((Now.x+xx[i]+xx[j]) >=0) && ((Now.x+xx[i]+xx[j]) <n) && (( NOW.Y+YY[I]+YY[J] >=0) && ((Now.y+yy[i]+yy[j]) <n) && ((maps[now.x+xx[i]+xx[j]][now.y+yy[i]+ Yy[j]]) & (1<< ((2+j)%4)) >0) {}                                else//otherwise marks the number of steps, and steps take the minimum value {flag=min (flag,now.step+1);                            printf ("^^%d\n", flag);                        }}} else//otherwise marks the end of the step {                        Flag=min (Flag,now.step);                    printf ("^%d\n", flag); }} for (i=0; i<4; i++) {if ((maps[now.x][now.y]& (1 <<i)) (>0) && (now.com!=i))//except for the direction of water in the inlet direction (cannot be returned from the original path) {//the next pipe has not been streamed, not for The end point does not cross and can be connected if (!book[now.x+xx[i]][now.y+yy[i]] && (now.x+xx[i)!=x2| | (Now.y+yy[i])!=y2) && ((Now.x+xx[i]) >=0) && ((Now.x+xx[i]) <n) && ((now.y+yy[i)) >=0) && ((Now.y+yy[i]) <n) && ((Maps[now.x+xx[i]][now.y+yy[i]) & (1<< ((2+i)%4)) >0) {//printf ("!!!                            %d%d\n ", now.x+xx[i],now.y+yy[i]); sum++;//answer + + book[now.x+xx[i]][now.y+yy[i]]=1;//tag next.x=now.x+xx[i                            ];                            Next.y=now.y+yy[i];                            Next.com= (i+2)%4;                            next.step=now.step+1; if (Next.step<=flag)//The next step will not reach the end, will not cross out, then press into the queue.                                Flag has been previously judged, the number of steps for the game to end {Qq.push (next);                                printf ("%d%d%d\n", now.x+xx[i],now.y+yy[i],now.step);                            GetChar ();            }}}}} printf ("%d\n", sum);                while (!qq.empty ()) {Qq.pop (); }}} return 0;}  /* provide you with more than one set of data test use ~ No Thanks ~ My name is red scarf ~simple input:4418 15 15 159 15 15 1515 15 15 1515 15 15 4053 5 5 6 010 3 5 15 610 10 0 10 4010 9 5 189 5 5 5 1253 5 5 6 010 0 0 610 0 0 4010 9 5 189 5 5 5 12Simple output:101814*/


Finally, the Goddess Zhen Zhai ~



FZU1892 Riser game-BFS plus simple state compression and bit arithmetic processing

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.