Bfs hdu 1175

Source: Internet
Author: User

, Question. It was difficult at the beginning. After reading the problem, I found that many people use DFS. I use BFs, which is similar to 1728.

The idea is. Search in one direction. However, it should be noted that a piece cannot pass through.

 

Code abuse me a thousand times, waiting for code to be like first love

 

 

 

 

Serialization

Time Limit: 20000/10000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 18770 accepted submission (s): 4895


Problem description "continuous viewing" is believed to have been played by many people. It doesn't matter if you haven't played it. Next I will introduce you to the game rules: a board contains a lot of pawns. If two of the same pawns can be connected through a line (this line cannot pass through other pawns), and the number of turns of the line cannot exceed two times, then the two pieces can be removed from the board. Sorry, because I have never played in the past, and I have consulted my colleagues, the connection cannot be bypassed from outside, but in fact this is wrong. Now it has become a serious disaster, so we can only make mistakes, and the connections cannot be bypassed from the periphery.
The player clicks two pawns one by one, tries to remove them, and then checks whether the two squares can be deleted in the background of the game. Now your task is to write this background program.


 

There are multiple groups of input data. The first row of each group of data has two positive integers n, m (0 <n <= 1000, 0 <m <), indicating the number of rows and columns of the Board respectively. In the next n rows, each row has m non-negative integers to describe the square distribution of the Board. 0 indicates that there is no pawn in this position, and a positive integer indicates the type of the pawn. The next row is a positive integer Q (0 <q <50), indicating that there are Q queries below. In the next Q row, each row has four positive integers X1, Y1, X2, Y2, which indicates whether the pawns in column Y1 of row X1 can be deleted from column Y2 of row X2. When n = 0 and m = 0, the input ends.
Note: There is no sequential relationship between the queries. They are all in the current state!


 

Each group of input data corresponds to a row of output. If yes, "yes" is output. If no, "no" is output ".


 

Sample Input
 
3 41 2 3 40 0 04 3 141 1 3 41 1 2 41 1 3 32 1 2 43 40 1 4 30 2 10 0 0 021 1 2 41 3 2 30 0


 

Sample output
 
Yesnonononoyes
# Include <stdio. h> # include <stdlib. h> # include <malloc. h> # include <limits. h> # include <ctype. h> # include <string. h> # include <string> # include <math. h> # include <algorithm> # include <iostream> # include <queue> # include <stack> # include <deque> # include <vector> # include <set> using namespace std; # define maxn 1000 + 10int num [maxn] [maxn]; int vis [maxn] [maxn]; int n, m; int FX, FY; int XX [4] = {-,}; int YY [4] = {, 0 ,-1}; struct node {int X; int y; int count ;}; void BFS (int A, int B) {queue <node> q; node front; int I; node rear; front. X = A; front. y = B; front. count =-1; q. push (Front); int mark = 0; while (! Q. empty () {front = Q. front (); q. pop (); If (front. X = FX & Front. y = FY) {If (front. count <= 2) {printf ("Yes \ n"); Mark = 1; break;} else {printf ("NO \ n"); Mark = 1; break;} rear. count = front. count + 1; for (I = 0; I <4; I ++) {int dx = front. X + XX [I]; int DY = front. Y + YY [I]; while (dx> = 0 & DX <n & dy> = 0 & dy <M & num [dx] [dy] = 0) | (dx> = 0 & DX <n & dy> = 0 & dy <M & dx = FX & DY = FY )) {If (vis [dx] [dy] = 0) {vis [dx] [d Y] = 1; rear. X = DX; rear. y = Dy; q. push (rear);} dx = dx + XX [I]; DY = Dy + YY [I] ;}} if (mark = 0) {printf ("NO \ n") ;}} int main () {int I, j; int K; int X1, Y1, X2, Y2; while (~ Scanf ("% d", & N, & M) {If (n = 0 & M = 0) {break;} memset (Num, 0, sizeof (Num); memset (VIS, 0, sizeof (VIS); for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {scanf ("% d", & num [I] [J]) ;}} if (n = 1 & M = 1) {printf ("NO \ n"); continue;} scanf ("% d", & K ); while (k --) {scanf ("% d", & X1, & Y1, & X2, & Y2); memset (VIS, 0, sizeof (VIS); vis [x1-1] [y1-1] = 1; FX = x2-1; FY = y2-1; If (Num [x1-1] [y1-1]! = Num [x2-1] [y2-1]) {printf ("NO \ n"); continue ;} else if (Num [x1-1] [y1-1] = num [x2-1] [y2-1] & num [x1-1] [y1-1] = 0) {printf ("NO \ n"); continue;} else {BFS (x1-1, y1-1);} // BFS (x1-1, y1-1) ;}} return 0 ;}


 

Bfs hdu 1175

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.