#include <bits/stdc++.h>
using namespace std;
char _map[1010][1010];
int b[1020100][3];
bool st[1010][1010];
int m,z,n,a1,b1,a2,b2;
int s;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};
bool nbsp;
void dfs(int x, int y, int step)
{
if(x == n && y == m)
{
nbsp=true;
cout << "(1,1)";
for(int i = 2; i <= step - 1; i++)
{
cout << "->(" << b[i][1] << "," << b[i][2]<<")";
}
cout << endl; return;
}
for(int i = 0; i < 4; i++)
{
int nx = x + dx[i], ny = y + dy[i];
if(nx >= 1 and ny >= 1 and nx <= n and
ny <= m and st[nx][ny] == 0 and _map[nx][ny]-'0')
{
if(!st[nx][ny])
{
b[step][1] = nx;b[step][2] = ny;
st[nx][ny] = 1;
dfs(nx, ny, step + 1);
st[nx][ny] = 0;}
}
}}
int main(){
cin>>n >> m;
b[1][1] = 1;b[1][2] = 1;
st[1][1] = 1;
for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++)cin>>_map[i][j];
cin>>a1>>b1>>a2>>b2;
dfs(a1, b1, 2);
if(!nbsp)cout<<-1;
return 0;
}
[link]