记录编号 381551 评测结果 AAAAAAAAAA
题目名称 走迷宫 最终得分 100
用户昵称 Gravatarxzcxzc11 是否通过 通过
代码语言 C++ 运行时间 0.005 s
提交时间 2017-03-11 22:27:22 内存使用 0.32 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <vector>
//#define DEBUG

using namespace std;

struct node
{
	int x;
	int y;
	node(int h = 1, int l = 1)
	{
		x = h;
		y = l;
	}
};

const int maxmn = 20;
int m, n;
int map[maxmn][maxmn];
bool vis[maxmn][maxmn] = { 0 };
node a[300];
int SH, SL, FH, FL;
int cnt = 0;
const node walk[4] = {
	{-1,0},{0,-1},{0,1},{1,0}
};
vector<node> answer;

void init();
bool can(const node & nd);
void search(node cur);
void print();

int main()
{
#ifndef DEBUG
	freopen("maize.in", "r", stdin);
	freopen("maize.out", "w", stdout);
	ios::sync_with_stdio(0);
#endif	
	init();
	node first(SH, SL);
	search(first);
	if (cnt == 0)
	{
		cout << "-1" << endl;
	}
	return 0;
}

void init()
{
	cin >> m >> n;
	for (int i = 1; i <= m; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cin >> map[i][j];
		}
	}
	cin >> SH >> SL >> FH >> FL;
}

bool can(const node & nd)
{
	if (nd.x<1 || nd.x>m) return false;
	if (nd.y<1 || nd.y>n) return false;
	if (map[nd.x][nd.y] == 0) return false;
	
	return true;
}

void search(node cur)
{
	answer.push_back(cur);
	if (cur.x == FH&&cur.y == FL)
	{
		++cnt;
		print();
		answer.pop_back();
		return;
	}
	map[cur.x][cur.y] = 0;
	/*
	else
	{
		map[cur.x][cur.y] = 0;
		node temp;
		for (int i = 0; i < 4; ++i)
		{
			temp.x = cur.x + walk[i].x;
			temp.y = cur.y + walk[i].y;
			if (can(temp))
			{
				search(temp);
			}
		}
		map[temp.x][temp.y] = 1;
		answer.pop_back();
	}
	*/
	if (map[cur.x - 1][cur.y] == 1)
		search(node(cur.x - 1, cur.y));
	if (map[cur.x][cur.y - 1] == 1)
		search(node(cur.x, cur.y - 1));
	if (map[cur.x][cur.y + 1] == 1)
		search(node(cur.x, cur.y + 1));
	if (map[cur.x + 1][cur.y] == 1)
		search(node(cur.x + 1, cur.y));
	answer.pop_back();
	map[cur.x][cur.y] = 1;
}

void print()
{
	cout << '(' << answer[0].x << ',' << answer[0].y << ')';
	for (int i = 1; i < answer.size(); ++i)
	{
		cout << "->"
			<< '(' << answer[i].x << ',' << answer[i].y << ')';
	}
	cout << endl;
}