Hdoj1254 push box Java code

Source: Internet
Author: User

The code I wrote can run normally, but the result is wrong. I did not consider the location of the person. In addition, the Code still has some problems that have not been found yet.

Import Java. util. *; <br/> public class main1254 {<br/> Public static void main (string [] ARGs) {<br/> Using SC = new using (system. in); <br/> int T = SC. nextint (); <br/> int M, N; <br/> int [] [] G; <br/> boxworld bw; <br/> while (0! = T --) {<br/> M = SC. nextint (); <br/> N = SC. nextint (); <br/> G = new int [M + 2] [n + 2]; <br/> // wrap a wall on the outermost side <br/> for (INT I = 0; I <m + 2; I ++) {<br/> for (Int J = 0; j <n + 2; j ++) {<br/> G [I] [J] = 1; <br/>}< br/> for (INT I = 1; I <= m; I ++) {<br/> for (Int J = 1; j <= N; j ++) {<br/> G [I] [J] = SC. nextint (); <br/>}< br/> BW = new boxworld (g); <br/> system. out. println (BW. push (BW. cur,-1); <br/>}< br/> C Lass boxworld {<br/> int [] [] graph; <br/> position cur, end; </P> <p> Public boxworld (INT [] [] G) {// initialize the square <br/> graph = g; <br/> for (INT I = 1; I <graph. length-1; I ++) {<br/> for (Int J = 1; j <graph [I]. length-1; j ++) {<br/> If (graph [I] [J] = 2) {<br/> cur = new position (I, j); <br/>}< br/> If (graph [I] [J] = 3) {<br/> end = new position (I, j ); <br/>}</P> <p> int push (position cur, int steps) {<Br/>/* // the following code prompts that a return statement is missing <br/> If (cur. equals (end) {<br/> return steps; <br/>}else {<br/> If (cur. canpushto (0, 1) {// up <br/> return push (cur. to (0, 1), steps); <br/>}< br/> If (cur. canpushto (0,-1) {// down <br/> return push (cur. to (0, 1), steps); <br/>}< br/> If (cur. canpushto (-1, 0) {// left <br/> return push (cur. to (0, 1), steps); <br/>}< br/> If (cur. canpushto (1, 0) {// right <br/> return push (cur. to (0, 1), steps); <br/>}< br/> steps ++; <br/>}< br/> */<br/> steps ++; <br/> If (! Cur. equals (end) {<br/> If (cur. canpushto (0, 1) {// up <br/> return push (cur. to (0, 1), steps); <br/>}< br/> If (cur. canpushto (0,-1) {// down <br/> return push (cur. to (0,-1), steps); <br/>}< br/> If (cur. canpushto (-1, 0) {// left <br/> return push (cur. to (-1, 0), steps); <br/>}< br/> If (cur. canpushto (1, 0) {// right <br/> return push (cur. to (1, 0), steps); <br/>}< br/> return steps; <br/>}</P> <p> class position {// define the position of the box as a class <br/> int X, Y; <br/> public position (INT _ x, int _ y) {<br/> X = _ x; <br/> Y = _ y; <br/>}< br/> position to (INT xstep, int ystep) {<br/> position P = new position (x + xstep, Y + ystep ); <br/> return P; <br/>}< br/> Public Boolean equals (Object O) {<br/> position P = (position) O; <br/> If (this. X = P. X & this. y = P. y) {<br/> return true; <br/>} else {<br/> return false; <br/>}< br/> Boolean canpushto (INT xstep, int ystep) {<br/> If (graph [this. X + xstep] [This. Y + ystep] = 1 <br/> | graph [this. x-xstep] [This. y-ystep] = 1) {<br/> return false; <br/>} else {<br/> return true; <br/>}< br/>}

Hope someone can give me some advice ......

 

Other people's c ++ code on the Internet:

# Include <iostream> <br/> # include <queue> <br/> using namespace STD; <br/> int dir [4] [2] = {0, 1}, {1, 0}, {0,-1}, {-1, 0 }}; <br/> struct point <br/>{< br/> int PX; <br/> int py; <br/> int xx; <br/> int XY; <br/> int step; <br/>}; <br/> queue <point> q; <br/> queue <point> P; <br/> int map [8] [8]; <br/> int visit [8] [8] [8] [8]; <br/> int hash [8] [8] [8] [8]; <br/> int n, m, dx, Dy, flag; </P> <p> point input () <br/> {<Br/> int I, j; <br/> point temp; <br/> dx = 9, dy = 9; <br/> scanf ("% d", & N, & M); <br/> temp. step = 0; <br/> for (I = 0; I <n; I ++) <br/> {<br/> for (j = 0; j <m; j ++) <br/> {<br/> scanf ("% d", & map [I] [J]); <br/> If (Map [I] [J] = 4) <br/> {<br/> map [I] [J] = 0; <br/> temp. px = I; <br/> temp. py = J; <br/>}< br/> If (Map [I] [J] = 2) <br/>{< br/> map [I] [J] = 0; <br/> temp. xx = I; <br/> temp. XY = J; <br/>}< br/> If (Map [I] [J] = 3) <br/> {<br/> map [I] [J] = 0; <br/> dx = I; <br/> DY = J; <br/>}< br/> return temp; <br/>}</P> <p> int peopletest (point temp) <br/>{< br/> If (temp. px <0 | temp. PY <0 | temp. px> = n | temp. PY> = m) <br/> return 0; <br/> If (temp. px = temp. XX & temp. py = temp. XY) <br/> return 0; <br/> If (Map [temp. px] [temp. PY] = 1) <br/> return 0; <br/> re Turn 1; <br/>}< br/> void people_bfs (point temp, point TAR) <br/>{< br/> int I; <br/> point tt; <br/> memset (hash, 0, sizeof (hash); <br/> while (! P. Empty () <br/> P. Pop (); <br/> P. Push (temp); <br/> while (! P. empty () <br/>{< br/> temp = P. front (); <br/> P. pop (); <br/> TT = temp; <br/> If (TT. px = tar. px & TT. py = tar. PY) <br/>{< br/> flag = 1; <br/> return; <br/>}< br/> for (I = 0; I <4; I ++) <br/>{< br/> TT. px = temp. PX + dir [I] [0]; <br/> TT. py = temp. PY + dir [I] [1]; </P> <p> If (! Peopletest (TT) continue; </P> <p> If (! Hash [temp. XX] [temp. XY] [TT. px] [TT. PY]) <br/>{< br/> P. push (TT); <br/> hash [temp. XX] [temp. XY] [TT. px] [TT. PY] = 1; <br/>}</P> <p> int boxtest (point TT) <br/>{< br/> If (TT. XX> = n | TT. XY> = M | TT. XX <0 | TT. XY <0) <br/> return 0; <br/> If (Map [TT. XX] [TT. XY] = 1) <br/> return 0; <br/> return 1; <br/>}< br/> int box_bfs (point temp) <br/>{< br/> int I; <br/> point tt; <br/> whi Le (! Q. Empty () <br/> q. Pop (); <br/> q. Push (temp); <br/> while (! Q. empty () <br/>{< br/> temp = Q. front (); </P> <p> q. pop (); <br/> If (temp. xx = DX & temp. XY = Dy) <br/>{< br/> return temp. step; <br/>}< br/> TT. step = temp. step + 1; <br/> for (I = 0; I <4; I ++) <br/> {<br/> TT. xx = temp. xx + dir [I] [0]; <br/> TT. XY = temp. xy + dir [I] [1]; </P> <p> If (! Boxtest (TT) continue; </P> <p> TT. px = temp. XX-dir [I] [0]; <br/> TT. py = temp. xy-dir [I] [1]; </P> <p> If (TT. px <0 | TT. PY <0 | TT. px> = n | TT. PY >=m) <br/> continue; </P> <p> If (Map [TT. px] [TT. PY] = 1) <br/> continue; </P> <p> flag = 0; <br/> people_bfs (temp, TT ); <br/> If (FLAG) <br/> {<br/> TT. px = temp. XX; <br/> TT. py = temp. XY; <br/> If (! Visit [TT. px] [TT. PY] [TT. XX] [TT. XY]) <br/>{< br/> visit [TT. px] [TT. PY] [TT. XX] [TT. XY] = 1; <br/> q. push (TT); <br/>}< br/> return-1; <br/>}</P> <p> point temp; </P> <p> int main () <br/>{< br/> int T; <br/> scanf ("% d", & T); <br/> while (t --) <br/>{< br/> printf ("% d/N", box_bfs (input (); <br/> memset (visit, 0, sizeof (visit); <br/>}< 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.