玄关,警示后人
  • 板块P1238 走迷宫
  • 楼主myl_coder
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/11/18 17:01
  • 上次更新2024/11/18 19:51:56
查看原帖
玄关,警示后人
1382496
myl_coder楼主2024/11/18 17:01

太玄关了,RE了,80tps
勾石记录\

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

int n, m, T;
int a[100][100];
int book[100][100];
int sx, sy, fx, fy;
int ans = 0;
int dx[5] = {0, 0, -1, 0, 1};
int dy[5] = {0, -1, 0, 1, 0};

struct node {
	int sx, sy;
};

void dfs(int x, int y, node s[], int step) {
	if (x == fx && y == fy) {
		++ ans;
		for (int i = 1; i <= step - 2; i ++) {
			cout << "(" << s[i].sx << "," << s[i].sy << ")->";
		}
		cout << "(" << s[step - 1].sx << "," << s[step - 1].sy << ")\n";
		return ;
	}
	book[x][y] = 1;
	for (int i = 1; i <= 4; i ++) {
		int tx = x + dx[i], ty = y + dy[i];
		
//		cout << tx << " " << ty << " " << book[tx][ty] << " " << a[tx][ty] << endl;
		if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && book[tx][ty] == 0 && a[tx][ty] == 1) {
			s[step].sx = tx;
			s[step].sy = ty;
			dfs (tx, ty, s, step + 1);
			s[step].sx = 0;
			s[step].sy = 0;
			book[tx][ty] = 0;
		}
	}
}

int main() {
	ios :: sync_with_stdio();
	cin.tie(0);
	cout.tie(0);
	
	//freopen (".in", "r", stdin);
	//freopen (".out", "w", stdout);
	
	cin >> n >> m;
	
	for (int i = 1; i <= n; i ++) {
		for (int j = 1; j <= m; j ++) {
			cin >> a[i][j];
		}
	}
	cin >> sx >> sy >> fx >> fy;
	
	node b[20] = {};
	b[1].sx = sx;
	b[1].sy = sy;
	dfs (sx, sy, b, 2);
	if (ans == 0) {
		cout << -1 << endl;
	}
	
	return 0;
}

代码如上
请各位大佬帮帮我这个蒟蒻

2024/11/18 17:01
加载中...