walk the maze

walk the maze

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

There is a maze of m*n grids (representing m rows and n columns), in which there are walkable and unwalkable ones. If you use 1 to indicate that you can walk, and 0 to indicate that you cannot walk, enter the m*n data and the starting point. Start point and end point (both the start point and the end point are described by two data, which represent the row number and column number of this point respectively). Now you are asked to program to find all feasible roads, requiring that there are no repeated points in the road you walk, and you can only walk in four directions, up, down, left, and right. If none of the paths are feasible, output the corresponding information (use -1 to indicate no path).

Input

The first line is two numbers m, n (1< m, n< 15), the next is m lines and n columns of data composed of 1 and 0, and the last two lines are the start and end points.

Output

All feasible paths are output in the order of upper left and lower right. When describing a point, use the form of (x, y), except for the starting point, all others must be represented by "->". If there is no feasible way, output -1.

Sample Input

5 4
1 1 0 0
1 1 1 1
0 1 1 0
1 1 0 1
1 1 1 1
1 1
5 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)

Hint

Source

dfs

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <bits/stdc++.h>
using namespace std;
int ma[20][20];
int force[20][20]
int flag;
int n,m,top,x2,y2;
int next1[5][3] = {{-1,0},{0,-1},{0,1},{1,0}};
struct node{
    int x,y;
}path[100000];
void out(){
    int i;
    for(i=0;i<top-1;i++)
        printf("(%d,%d)->",path[i].x,path[i].y);
    printf("(%d,%d)\n",x2,y2);
}
void dfs(int x,int y){
    if(x==x2&&y==y2){
        flag=1;
        out();
        return ;
    }
    int x3,y3,i;
    for(i=0;i<4;i++){
        x3=x+next1[i][0];
        y3=y+next1[i][1];
        if(x3>=1&&y3>=1&&x3<=n&&y3<=m&&vis[x3][y3]==0&&ma[x3][y3]){
            path[top].x=x3;
            path[top++].y=y3;
            vis [x3] [y3] = 1;
            dfs(x3,y3);
            vis [x3] [y3] = 0;
            top--;
        }
    }
}
intmain()
{

    int i,j,x1,y1;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++)
            scanf("%d",&ma[i][j]);
    }
    flag=0;
    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    vis [x1] [y1] = 1;
    path[0].x=x1;
    path[0].y=y1;
    top=1;
    dfs(x1,y1);
    if(!flag)
        printf("-1\n");
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324849921&siteId=291194637