走迷宫
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
有一个m*n格的迷宫(表示有m行、n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,输入这m*n个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。如果一条路都不可行,则输出相应信息(用-1表示无路)。
Input
第一行是两个数m,n(1< m, n< 15),接下来是m行n列由1和0组成的数据,最后两行是起始点和结束点。
Output
所有可行的路径,输出时按照左上右下的顺序。描述一个点时用(x,y)的形式,除开始点外,其他的都要用“->”表示。如果没有一条可行的路则输出-1。
Example 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
Example 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
#include <iostream>
#include <cstring>
using namespace std;
int n,m,x1,y1,x2,y2,Map[17][17],dx[200],dy[200],c;
int v[17][17],di[4][2]= {{0,-1},{-1,0},{0,1},{1,0}},num;
void print()
{
for(int j=0; j<=num-2; j++)
cout<<'('<<dx[j]<<','<<dy[j]<<")->";
cout<<'('<<x2<<','<<y2<<')'<<endl;
}
void DFS(int xx,int yy)
{
if(xx==x2&&yy==y2)
{
c=1;
print();
return;
}
for(int i=0; i<4; i++)
{
int x=xx+di[i][0];//不能直接改变传入的参数xx,yy的值,另设变量
int y=yy+di[i][1];
if(Map[x][y]&&!v[x][y]&&x>=1&&x<=n&&y>=1&&y<=m)
{
v[x][y]=1;
dx[num]=x;
dy[num++]=y;
DFS(x,y);
v[x][y]=0;
num--;//再存储新的数据
}
}
}
int main()
{
while(cin>>n>>m)
{
c=0;
memset(Map,0,sizeof(Map));
memset(v,0,sizeof(v));
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
cin>>Map[i][j];
cin>>x1>>y1>>x2>>y2;
v[x1][y1]=1;
num=0;
dx[num]=x1;
dy[num++]=y1;
DFS(x1,y1);
if(!c)
cout<<"-1"<<endl;
}
return 0;
}
//注意题意要求按左上右下输出
import java.util.*;
public class Main {
static int Map[][] = new int[20][20], x1, x2, y1, y2, num = 0, judge = 0, n, m;
static int v[][] = new int[20][20], px[] = new int[200], py[] = new int[200];
static int Move[][] = { { 0, -1 }, { -1, 0 }, { 0, 1 }, { 1, 0 } };
static boolean check(int x, int y) {
if (x >= 1 && y >= 1 && x <= n && y <= m)
return true;
else
return false;
}
static void Output() {
for (int i = 0; i < num; i++)
System.out.printf("(%d,%d%s", px[i], py[i], i == num - 1 ? ")\n" : ")->");
}
static void DFS(int xx, int yy) {
if (xx == x2 && yy == y2) {
judge = 1;
Output();
return;
}
for (int i = 0; i < 4; i++) {
int x = xx + Move[i][0];
int y = yy + Move[i][1];
if (Map[x][y] == 1 && v[x][y] == 0 && check(x, y) == true) {
v[x][y] = 1;
px[num] = x;
py[num++] = y;
DFS(x, y);
v[x][y] = 0;
num--;
}
}
}
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
n = cin.nextInt();
m = cin.nextInt();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
Map[i][j] = cin.nextInt();
x1 = cin.nextInt();
y1 = cin.nextInt();
x2 = cin.nextInt();
y2 = cin.nextInt();
v[x1][y1] = 1;
px[num] = x1;
py[num++] = y1;
DFS(x1, y1);
if (judge == 0)
System.out.println("-1");
cin.close();
}
}