记录编号 67525 评测结果 AAAAAAAAAA
题目名称 走迷宫 最终得分 100
用户昵称 GravatarHobo 是否通过 通过
代码语言 C++ 运行时间 0.003 s
提交时间 2013-08-12 20:44:45 内存使用 0.31 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#define MAXN 20
using namespace std;

stack<int> A,Q,C;
int M,N,X1,Y1,X2,Y2,ANS=0,T;
bool B[MAXN][MAXN];

void print()
{
	C=A;
	while (!C.empty())
	{
		Q.push(C.top());
		C.pop();
	}
	while (Q.size()>2)
	{
		int T1,T2;
		T1=Q.top();Q.pop();
		T2=Q.top();Q.pop();
		cout<<"("<<T1<<",";
		cout<<T2<<")->";
	}
	int T1,T2;
	T1=Q.top();Q.pop();
	T2=Q.top();Q.pop();
	cout<<"("<<T1<<",";
	cout<<T2<<")"<<endl;
}

void dfs(int x,int y)
{
	A.push(x);A.push(y);
	if (x==X2 && y==Y2 && A.size()>2) {print();A.pop();A.pop();ANS++;B[x][y]=1;return;}
	B[x][y]=0;
	if (B[x-1][y]==1) dfs(x-1,y);
	if (B[x][y-1]==1) dfs(x,y-1);
	if (B[x][y+1]==1) dfs(x,y+1);
	if (B[x+1][y]==1) dfs(x+1,y);
	A.pop();A.pop();
	B[x][y]=1;
return;
}

int main()
{
freopen("maize.in","r",stdin);
freopen("maize.out","w",stdout);
cin>>M>>N;
memset(B,0,sizeof(B));
for (int i=1;i<=M;i++) for (int j=1;j<=N;j++){ cin>>T; T==1?B[i][j]=1:B[i][j]=0; }
cin>>X1>>Y1>>X2>>Y2;
dfs(X1,Y1);
if (ANS==0) cout<<"-1";
return 0;
}