Foj 1205 mouse maze

Source: Internet
Author: User

This question uses the idea of traversing the graph's breadth first.

First, find the shortest path.

Then, recursively return to the end point to solve the problem.

 

In fact, this recursive process can be written as a stack, which saves more time.

 

Below is my code .....

 

# Include <iostream> <br/> # include <queue> <br/> # include <string. h> <br/> using namespace STD; <br/> # define x 100 <br/> # define Y 100 <br/> # define disable_sign-1 <br/> # define mouseone_sign-2 <br/> # define mousetwo_sign-3 <br/> struct point_struct <br/>{< br/> int X; <br/> int y; <br/> int flag; <br/>}; </P> <p> int map [x + 2] [Y + 2]; </P> <p> void calcwaynum (Int & num, int X, int y, int m1x, int m1y) <br/>{< br/> int P Lusx [] = {, 0,-1}; <br/> int plusy [] = {,-}; <br/> int I; <br/> If (x = m1x & Y = m1y) <br/>{< br/> num ++; <br/> return; <br/>}< br/> for (I = 0; I <4; I ++) <br/> {<br/> If (Map [x + plusx [I] [Y + plusy [I] = map [x] [Y]-1) <br/> calcwaynum (Num, x + plusx [I], Y + plusy [I], m1x, m1y ); <br/>}</P> <p> int main () <br/>{< br/> int plusx [] = {0, 1, 0, -1 }; <br/> int plusy [] = {,-}; <br/> int X, Y, numofdisable; <br/> I NT disx, DISY; <br/> int mouseonex, mouseoney; <br/> int mousetwox, mousetwoy; <br/> int I; <br/> int breakflag; <br/> int numofway; <br/> int shortway; <br/> queue <point_struct> queue; <br/> point_struct temp_point, temp_point2; <br/> while (scanf ("% d", & X, & Y, & numofdisable )! = EOF) <br/>{< br/> memset (MAP, 0, sizeof (MAP); <br/> for (I = 0; I <= Y + 1; I ++) <br/>{< br/> map [0] [I] = disable_sign; <br/> map [x + 1] [I] = disable_sign; <br/>}< br/> for (I = 0; I <= x + 1; I ++) <br/>{< br/> map [I] [0] = disable_sign; <br/> map [I] [Y + 1] = disable_sign; <br/>}< br/> for (I = 0; I <numofdisable; I ++) <br/>{< br/> scanf ("% d", & disx, & DISY); <br/> map [disx] [DISY] = disable_sign; <br/>}< br/> scanf ("% d", & mouse Onex, & mouseoney); <br/> scanf ("% d", & mousetwox, & mousetwoy); <br/> map [mouseonex] [mouseoney] = mouseone_sign; <br/> map [mousetwox] [mousetwoy] = mousetwo_sign; <br/> temp_point.x = mouseonex; <br/> temp_point.y = mouseoney; <br/> temp_point.flag = 0; <br/> queue. push (temp_point); <br/> // start of breadth-first traversal <br/> breakflag = 0; <br/> shortway = 0; <br/> while (! Queue. empty () <br/>{< br/> temp_point = queue. front (); <br/> for (I = 0; I <4; I ++) <br/> {<br/> If (Map [temp_point.x + plusx [I] [temp_point.y + plusy [I] = 0 | <br/> map [temp_point.x + plusx [I] [temp_point.y + plusy [I] = mousetwo_sign) <br/> {<br/> map [temp_point.x + plusx [I] [temp_point.y + plusy [I] = temp_point.flag + 1; <br/> temp_point2.x = temp_point.x + plusx [I]; <br/> temp_point2.y = temp_point.y + plusy [I]; <br/> temp_point2.flag = temp_point.flag + 1; <br/> queue. push (temp_point2); <br/>}< br/> queue. pop (); <br/>}< br/> map [mouseonex] [mouseoney] = 0; <br/> numofway = 0; <br/> calcwaynum (numofway, mousetwox, mousetwoy, mouseonex, mouseoney); <br/> printf ("% d/N", map [mousetwox] [mousetwoy]); <br/> printf ("% d/N", numofway); <br/>}< br/> return 0; <br/>}< br/>

Related Article

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.