求调QWQ
  • 板块P1238 走迷宫
  • 楼主Stone_Xz
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/30 11:47
  • 上次更新2024/3/30 14:34:25
查看原帖
求调QWQ
661984
Stone_Xz楼主2024/3/30 11:47

45分。。。

#include<bits/stdc++.h>
#define int long long
using namespace std;

string s[16] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"};

int n, m, sx, sy, ex, ey;
bool vis[20][20], flag, a[20][20];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

void dfs(int x, int y, string ans)
{
	if(x < 1 || y < 1 || x > n || y > m)
		return ;
	if(x == ex && y == ey)
	{
		flag = true;
		cout << ans << "\n";
		return ;
	}
	for(int i = 0; i < 4; i++)
	{
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(a[nx][ny] == 1 && vis[nx][ny] == false)
		{
			vis[nx][ny] = true;
			dfs(nx, ny, ans + "->(" + s[nx] + "," + s[ny] + ")");
			vis[nx][ny] = false;
		}
	}
	return ;
}

signed main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			cin >> a[i][j];
	cin >> sx >> sy >> ex >> ey;
	vis[sx][sy] = true;
	dfs(sx, sy, "(" + s[sx] + "," + s[sy] + ")");
	if(flag == false)
		cout << "-1";
	return 0;
}
2024/3/30 11:47
加载中...