HDU 1175 Watch (DFS)

Source: Internet
Author: User
Tags define local

problem DescriptionI 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 data has multiple groups. 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 a row of outputs. If you can eliminate the output "YES", you cannot output "no". Sample Input
3 41 2 3 40 0 0 04 3 2 141 1 3 41 1 2 41 1 3 32 1 2 43 40 1 4 30 2 4 10 0 0 021 1 2 41 3 2 30 0
Sample Output
Yesnonononoyes

Links: http://acm.hdu.edu.cn/showproblem.php?pid=1175
The puzzle: is the transition two times can go from the beginning to the end. So use dire to record the direction, TER records the number of turns, DFS can.
WA several times, the reason is restores the starting point when restores to 0, restores the original value to be OK. There is also the first time not a turning point of departure.
This code is time consuming 7878ms, and 78ms really can't compare with ...


#include <cstdio>#include<iostream>#include<string>#include<cstring>#include<stack>#include<queue>#include<algorithm>#include<cmath>#include<map>using namespacestd;//#define LOCALintmp[ -][ -];intN,m,flag,tar;intSx,sy,ex,ey;intdir[5][2]= {{0,0},{0,1},{1,0},{0,-1},{-1,0}};voidDfsintXintYintDireintter) {    if(x<=0|| y<=0|| x>n| | Y&GT;M)return; if(ter>2)return; if(flag)return; intNx,ny;  for(intI=1; i<=4; i++) {NX=x+dir[i][0]; NY=y+dir[i][1]; if(mp[nx][ny]==0) {Mp[nx][ny]=-1; if(dire==i) {dfs (nx,ny,i,ter); Mp[nx][ny]=0; }            Else{DFS (nx,ny,i,ter+1); Mp[nx][ny]=0; }        }        if(nx==ex&&ny==ey) {            if(dire==i) {if(ter<=2) flag=true; return; }            Else            {                if(ter<2) flag=true; return; }        }    }    return;}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif //LOCAL    //StartIos::sync_with_stdio (false);  while(cin>>n>>m&& (n| |m) {memset (MP,0,sizeofMP);  for(intI=1; i<=n; i++)             for(intj=1; j<=m; J + +) Cin>>Mp[i][j]; intN; CIN>>o;  while(n--) {flag=false; CIN>>sx>>sy>>ex>>ey; if(mp[sx][sy]==0|| mp[ex][ey]==0) printf ("no\n"); Else if(mp[sx][sy]!=Mp[ex][ey]) printf ("no\n"); Else{tar=Mp[sx][sy]; Mp[sx][sy]=-1; DFS (Sx,sy,0,-1); Mp[sx][sy]=tar; if(flag) printf ("yes\n"); Elseprintf"no\n"); }        }    }    return 0;}

HDU 1175 Watch (DFS)

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.