Luogu P1002 过河卒

原题传送门

#include 
using namespace std;
int xend,yend,xbeg,ybeg;

int walk[17][17]={0};
int mp[17][17]={0};
int movex[]={0,-1,0,1};
int movey[]={-1,0,1,0};
bool flag=false;
string convert[]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};

void dfs(int x,int y,string ans)
{
    if(x==xend&&y==yend)
    {
        cout<<ans<<endl;
        flag=true;
    }
    for(int i=0;i<4;i++)
    {
        int xx=x+movex[i],yy=y+movey[i];
        if(walk[xx][yy]==0&&mp[xx][yy]==1)
        {
            walk[xx][yy]=1;
            dfs(xx,yy,ans+"->"+"("+convert[xx]+","+convert[yy]+")");
            walk[xx][yy]=0;
        }
    }
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>mp[i][j]; 
    cin>>xbeg>>ybeg>>xend>>yend;
    walk[1][1]=1;
    dfs(xbeg,ybeg,"("+convert[xbeg]+","+convert[ybeg]+")");
    if(flag==0)
    cout<<-1;	
}

你可能感兴趣的:(Luogu,动规)