Sdut 1157 Mouse Maze problem (water problem, DFS + backtracking)

Source: Internet
Author: User

Mouse Maze problem Time limit:1500ms Memory limit:65536k have questions? Dot here ^_^ The topic describes mouse A and mouse B in a maze of MXN. Each square represents a room in the maze. Some of the rooms in this MXN room are enclosed and are not allowed to be entered by anyone. In the maze anywhere can go up, down, left, right 4 directions into the closed room. Mouse A is located in the Maze (p,q) square, which must find a way to the (r,s) square where mouse B resides. Please help mouse A to find all the shortest paths to mouse B.



Please program for a given mouse maze, calculate mouse A leads to mouse B for all the shortest roads. Enter multiple sets of input data on the subject and you must process to EOF.
The first row of each group of data has 3 positive integer n,m,k, representing the number of rows, columns, and closed rooms of the maze, respectively.
The next K line, 2 positive integers per line, indicates the line number and column number of the enclosed room.
In the last 2 lines, there are also 2 positive integers in each row, representing the squares (p,q) where mouse A is located and the squares (r,s) of mouse B. Output for each set of data, the calculated mouse A leads to mouse B's shortest-path length and how many different shortest-circuiting outputs.
Each group of data output two lines, the first line is the shortest path length, the 2nd row is a different number of short.
There are no blank lines between each set of outputs.
If mouse A does not lead to mouse B, the output is "No solution!". Sample input
8 8 33 34 56 62 17 7
Sample output
1196
Source

NOI ' 2005 Fujian Province contestant audition




#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <    stdlib.h>using namespace std;struct node{int x; int y;}  A,b;int jx[] = {0,1,0,-1};int jy[] = {1,0,-1,0};int v[101][101];int map[101][101];int n,m,k;int flag;int ans;int psum;void    DFS (int xx,int yy,int cnt) {if (Cnt>ans) {return;            } if (xx = = b.x && yy = = b.y) {if (Cnt<ans) {flag = 1;            ans = cnt;        Psum = 1;        } else if (cnt = = ans) {psum++;    } return; } for (int i=0;i<4;i++) {if (xx+jx[i]>=0 && xx+jx[i]<n && yy+jy[i]>=0 && y Y+jy[i]<m && Map[xx+jx[i]][yy+jy[i]] = = 0 && V[xx+jx[i]][yy+jy[i]] = = 0) {V[xx+jx[i]            ][yy+jy[i]] = 1;            DFS (xx+jx[i],yy+jy[i],cnt+1);        V[xx+jx[i]][yy+jy[i]] = 0; }}}int Main () {while (scanf ("%d%d%d",&n,&m,&k)!=eof) {for (int. i=0;i<=n;i++) {for (int j=0;j<=m;j++) {                MAP[I][J] = 0;            V[I][J] = 0;        }} int x, y;            for (int i=0;i<k;i++) {scanf ("%d%d", &x,&y);        Map[x][y] = 1;        } ans = 99999999;        Flag = 0;        psum = 0;        scanf ("%d%d", &AMP;A.X,&AMP;A.Y);        scanf ("%d%d", &b.x,&b.y);        DFS (a.x,a.y,0);        if (flag = = 0) {printf ("No solution!\n");            } else {printf ("%d\n", ans);        printf ("%d\n", psum); }} return 0;}


Sdut 1157 Mouse Maze problem (water problem, DFS + backtracking)

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.