Hangzhou Electric HDU ACM 1175 repeatedly see (Trouble of BFS)

Source: Internet
Author: User

Look at it repeatedly

Time Limit: 10000 MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u

SubmitStatusPracticeHDU 1175

Description

I believe a lot of people have played. It doesn't matter if you have not played, let me introduce you to the rules of the game: in a chessboard, put a lot of pieces. If two identical pieces can be connected by a line (this line cannot pass through other pieces), and the number of transitions of the line is not more than two times, then the two pieces can be eliminated on the board. Sorry, because I have not played a long time to see, consulted the views of the students, the connection can not be around the past, but in fact it is wrong. Now has become a catastrophe, you can only will wrong, wiring can not be bypassed from the periphery.
The player mouse has clicked on two pieces, trying to eliminate them, and then the game's background to determine whether the two squares can be eliminated. Now your task is to write this daemon.

Input

There are multiple sets of input data. The first row of each group of data has two positive integer n,m (0<n<=1000,0<m<1000), representing the number of rows and columns of the board, respectively. In the next n rows, each row has a m nonnegative integer describing the checkerboard's grid distribution. 0 means there is no pawn in this position, and a positive integer indicates the type of pawn. The next line is a positive integer q (0<q<50), which indicates that there is a Q query below. In the next Q line, there are four positive integer x1,y1,x2,y2 per line, which indicates that the pieces with the X1 row y1 column and the first X2 row y2 column can be eliminated. When n=0,m=0, the input ends.
Note: There is no relationship between inquiries, it is for the current state!

Output

Each set of input data corresponds to one row of output. If you can eliminate the output "YES", you cannot output "no".

Sample Input


Sample Output

Yesnonononoyes This topic at that time to pit me an afternoon time .... The code is flawed, and there are always unexpected letters, and the most pit is that dir array elements are not the same as the wrong, ghosts know why. It's similar to the one I did before. Just BFS a search to the end, according to the number of layers as the bfs depth, and the queue only to join the points that may be able to meet the conditions. In the process of expanding according to the number of layers, look for the end point together with the initial pruning. Optimized enough.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include < string.h> #include <cctype> #include <string> #include <cmath> #include <vector> #include <stack> #include <queue> #include <map> #include <set>using namespace Std;const int Inf=1003;int    Cnt[inf][inf];int Vis[inf][inf];int Dir[4][2] = {{ -1,0}, {0, 1}, {1, 0}, {0, -1}};int n,m;int sx,sy,ex,ey;struct Node{    int x,y,step;    Node (int x,int y,int Step): X (x), Y (y), step (step) {} Node () {}};void bfs (int x,int y) {queue<node>q;    Node Next;    Vis[x][y]=1;    Q.push (Node (x,y,0)); while (!q.empty ()) {Node U=q.front ();//cout<< "u.x=" <<u.x<< "u.y=" <<u.y<< "u.step=" &L        t;<u.step<<endl;        Q.pop ();            for (int i=0; i<4; i++) {next.x=u.x+dir[i][0];            NEXT.Y=U.Y+DIR[I][1];            Next.step=u.step; while (!vis[next.x][next.y]&&next.step<3&& cnt[next.x][next.y]==0&& next.x>=1&&next.x<=n&&next.y>=1&&                next.y<=m) {vis[next.x][next.y]=1;                Next.step+=1;                Q.push (next);                Next.step-=1;                NEXT.X+=DIR[I][0];            NEXT.Y+=DIR[I][1];                } if (next.x==ex&&next.y==ey&&next.step<3) {printf ("yes\n");            return;    }}} printf ("no\n"); return;}        int main () {while (cin>>n>>m,n+m) {memset (cnt,0,sizeof (CNT));        for (int. I=1; i<=n; i++) for (int j=1; j<=m; j + +) scanf ("%d", &cnt[i][j]);        int qu;        scanf ("%d", &qu);            for (int i=0; i<qu; i++) {memset (vis,0,sizeof (VIS));            scanf ("%d%d%d%d", &sx,&sy,&ex,&ey); if (cnt[sx][sy]==0| | cnt[ex][ey]==0) printf ("no\n");            else if (Sx==ex&&sy==ey) printf ("no\n");            else if (Cnt[sx][sy]!=cnt[ex][ey]) printf ("no\n");        else BFS (Sx,sy); }} return 0;} /*3 41 2 3 40 3 0 00 0 3 141 1 3 4*/




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

Hangzhou Electric HDU ACM 1175 repeatedly see (Trouble of 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.