HDU 1728 escaped from the maze Java BFS + one search to the end

Source: Internet
Author: User

/* <Br/> it is always tricky to start learning Java code <br/> */<br/> Import Java. io. *; <br/> Import Java. util. *; <br/> class node <br/> {<br/> int X, Y, pre, count; <br/> node (int x, int y, int pre, int count) <br/>{< br/> This. X = x; <br/> This. y = y; <br/> This. pre = pre; <br/> This. count = count; <br/>}< br/> public class main1728 <br/> {<br/> static int dir [] [] = {-1, 0 },{ 1, 0 },{ 0, 1}, {0,-1 }}; <br/> static char map [] [] = New char [105] [105]; <br/> static Boolean hash [] [] = new Boolean [105] [105]; <br/> static int N, m, K; <br/> static printwriter cout = new printwriter (New outputstreamwriter (system. out); </P> <p> static Boolean index (node now) <br/>{< br/> If (now. x> = 0 & now. x <n & now. y> = 0 & now. Y <M & now. count <= K & map [now. x] [now. y]! = '*') <Br/> return true; <br/> return false; <br/>}< br/> static void BFS (INT X1, int Y1, int X2, int Y2) <br/>{< br/> If (x1 = X2 & Y1 = Y2) <br/>{< br/> cout. println ("yes"); <br/> cout. flush (); <br/> return; <br/>}< br/> for (INT I = 0; I <= N; I ++) <br/> for (Int J = 0; j <= m; j ++) <br/> hash [I] [J] = false; <br/> queue <node> q = new queue list <node> (); <br/> q. clear (); <br/> hash [X1] [Y1] = true; <br/> (Int I = 0; I <4; I ++) <br/>{< br/> int Tx = X1 + dir [I] [0]; <br/> int ty = Y1 + dir [I] [1]; <br/> int TP = I; <br/> int Tc = 0; <br/> node next = new node (TX, Ty, TP, Tc); <br/> If (! Index (next) <br/> continue; <br/> If (! Hash [TX] [ty]) <br/>{< br/> // cout. println (next. X + "111" + next. Y + "" + next. count + "" + next. PRE); <br/> q. add (next); <br/> hash [next. x] [next. y] = true; <br/>}< br/> int xxx = x1; <br/> int YYY = Y1; <br/> while (true) <br/>{< br/> Tx = xxx + dir [I] [0]; <br/> ty = YYY + dir [I] [1]; <br/> TP = I; <br/> Tc = 0; <br/> node NEX = new node (TX, Ty, TP, Tc ); <br/> If (index (NEX) <br/>{< br/> If (NEX. X = X2 & NEX. Y = Y2) <br/>{< br/> cout. println ("yes"); <br/> cout. flush (); <br/> return; <br/>}< br/> // If (! Hash [TX] [ty]) <br/>{< br/> q. add (NEX); <br/> hash [NEX. x] [NEX. y] = true; <br/>}< br/> else <br/> break; <br/> xxx = NEX. x; <br/> YYY = NEX. y; </P> <p >}< br/> node now = new node (0, 0, 0, 0 ); <br/> while (! Q. isempty () <br/>{< br/> now = Q. poll (); <br/> // cout. println (now. X + "222" + now. Y + "" + now. count + "" + now. PRE); <br/> If (now. X = X2 & now. y = Y2) <br/>{< br/> cout. println ("yes"); <br/> cout. flush (); <br/> return; <br/>}< br/> int xx = now. x; <br/> int YY = now. y; <br/> while (true) <br/>{< br/> int Tx = xx + dir [now. pre] [0]; <br/> int ty = YY + dir [now. pre] [1]; <br/> int TP = now. pre; <br/> int t C = now. count; <br/> node next = new node (TX, Ty, TP, Tc); <br/> If (index (next )) <br/>{< br/> If (next. X = X2 & next. y = Y2) <br/>{< br/> cout. println ("yes"); <br/> cout. flush (); <br/> return; <br/>}< br/> If (! Hash [TX] [ty]) <br/>{< br/> q. add (next); <br/> hash [next. x] [next. y] = true; <br/>}< br/> else <br/> break; <br/> xx = next. x; <br/> YY = next. y; </P> <p >}< br/> for (INT I = 0; I <4; I ++) <br/>{< br/> if (I = now. PRE) <br/> continue; <br/> int Tx = now. X + dir [I] [0]; <br/> int ty = now. Y + dir [I] [1]; <br/> int TP = I; <br/> int Tc = now. count + 1; <br/> node next = new node (TX, Ty, TP, Tc); <br/> If (! Index (next) <br/> continue; <br/> If (! Hash [TX] [ty]) <br/>{< br/> If (next. X = X2 & next. y = Y2) <br/>{< br/> cout. println ("yes"); <br/> cout. flush (); <br/> return; <br/>}< br/> q. add (next); <br/> hash [TX] [ty] = true; <br/>}< br/> xx = next. x; <br/> YY = next. y; <br/> int TP1 = next. pre; <br/> int TC1 = next. count; <br/> while (true) <br/> {<br/> Tx = xx + dir [TP1] [0]; <br/> ty = YY + dir [TP1] [1]; <br/> TP = TP1; <br/> Tc = TC1; <br/> node nextt = N EW node (TX, Ty, TP, Tc); <br/> If (index (nextt) <br/>{< br/> If (nextt. X = X2 & nextt. y = Y2) <br/>{< br/> cout. println ("yes"); <br/> cout. flush (); <br/> return; <br/>}< br/> If (! Hash [TX] [ty]) <br/>{< br/> q. add (nextt); <br/> hash [nextt. x] [nextt. y] = true; <br/>}< br/> else <br/> break; <br/> xx = nextt. x; <br/> YY = nextt. y; </P> <p >}< br/> cout. println ("no"); <br/> cout. flush (); <br/> return; </P> <p >}< br/> Public static void main (string [] ARGs) <br/>{< br/> // streamtokenizer CIN = new streamtokenizer (New bufferedreader (New inputstreamreader (system. in); <br/> int t; <br/> running in = new partition (system. in); <br/> T = in. nextint (); <br/> while (t --> 0) <br/> {<br/> N = in. nextint (); <br/> M = in. nextint (); <br/> string S = new string (); <br/> for (INT I = 0; I <n; I ++) <br/> {<br/> S = in. next (); <br/> map [I] = S. tochararray (); <br/>}< br/> K = in. nextint (); <br/> int X1, Y1, X2, Y2; <br/> Y1 = in. nextint (); <br/> Y1 --; <br/> X1 = in. nextint (); <br/> X1 --; <br/> Y2 = in. nextint (); <br/> Y2 --; <br/> X2 = in. nextint (); <br/> X2 --; <br/> BFS (x1, Y1, X2, Y2 ); <br/>}</P> <p >}< br/> 

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.