Sdut 1269-path through the maze (DFS print path)

Source: Internet
Author: User
Maze Time Limit: 1000 ms memory limit: 65536 k any questions? Click Here ^_^ The topic description has a maze of M * n cells (which indicates that there are m rows and n columns), where there are accessible and inaccessible. If 1 is used, you can go, 0 indicates that the data cannot be taken. Input m x n data records, start points, and end points (both start points and end points are described using two data types, the row number and column number of the vertex respectively ). Now, you need to program to find all feasible paths, requiring that there are no repeated points in the path, and the walking time can only be in the top, bottom, and left directions. If a path is unavailable, the corresponding information is output (-1 indicates no path ). The first row of the input is two numbers m, n (1 <m, n <15), followed by data in n columns of m rows consisting of 1 and 0, the last two rows are the start and end points. Output all feasible paths in the upper left and lower right order. Describe a point in the form of (x, y). Except the start point, all others must be represented by "->. If there is no feasible path, output-1. Sample Input
5 41 1 0 01 1 1 10 1 1 01 1 0 11 1 1 11 15 4
Sample output
(1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)(1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)(1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)(1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)(1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)(1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
Path [] storage path of the array .. Brainless DFS
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cctype>#include <cstdlib>#include <set>#include <map>#include <vector>#include <string>#include <queue>#include <stack>#include <cmath>#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;struct node{int x,y;}path[1000];int n,m,sx,sy,ex,ey,ok,step;bool vis[16][16],ma[16][16];int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};void print(){for(int i=0;i<step-1;i++)printf("(%d,%d)->",path[i].x,path[i].y);printf("(%d,%d)\n",ex,ey);}void dfs(int x,int y){if(x==ex&&y==ey){ok=1;print();return ;}for(int i=0;i<4;i++){int tx=x+dir[i][0];int ty=y+dir[i][1];if(tx>=1&&tx<=m&&ty>=1&&ty<=n&&!vis[tx][ty]&&ma[tx][ty]){path[step].x=tx;path[step++].y=ty;vis[tx][ty]=1;dfs(tx,ty);vis[tx][ty]=0;step--;}}}int main(){while(~scanf("%d%d",&m,&n)){ok=0;memset(path,-1,sizeof(path));memset(vis,0,sizeof(vis));for(int i=1;i<=m;i++)for(int j=1;j<=n;j++)scanf("%d",&ma[i][j]);scanf("%d%d%d%d",&sx,&sy,&ex,&ey);vis[sx][sy]=1;step=0;path[step].x=sx;path[step++].y=sy;dfs(sx,sy);if(!ok)puts("-1");}return 0;}

Sdut 1269-path through the maze (DFS print path)

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.