标签:des style blog http io color ar os java
5 4 1 1 0 0 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 5 4
(1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4) (1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4) (1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4) (1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4) (1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4) (1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4) (1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4) (1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
path[]数组保存路径。。无脑dfs
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
struct node
{
int x,y;
}path[1000];
int n,m,sx,sy,ex,ey,ok,step;
bool vis[16][16],ma[16][16];
int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
void print()
{
for(int i=0;i<step-1;i++)
printf("(%d,%d)->",path[i].x,path[i].y);
printf("(%d,%d)\n",ex,ey);
}
void dfs(int x,int y)
{
if(x==ex&&y==ey)
{
ok=1;
print();
return ;
}
for(int i=0;i<4;i++)
{
int tx=x+dir[i][0];
int ty=y+dir[i][1];
if(tx>=1&&tx<=m&&ty>=1&&ty<=n&&!vis[tx][ty]&&ma[tx][ty])
{
path[step].x=tx;
path[step++].y=ty;
vis[tx][ty]=1;
dfs(tx,ty);
vis[tx][ty]=0;
step--;
}
}
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
ok=0;
memset(path,-1,sizeof(path));
memset(vis,0,sizeof(vis));
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
scanf("%d",&ma[i][j]);
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
vis[sx][sy]=1;step=0;
path[step].x=sx;path[step++].y=sy;
dfs(sx,sy);
if(!ok)
puts("-1");
}
return 0;
}
标签:des style blog http io color ar os java
原文地址:http://blog.csdn.net/qq_16255321/article/details/40709013