您的位置:首页 > 其它

fuz 1205(小鼠迷宫问题)

2013-05-08 12:05 495 查看
/*
首先   广搜,找到最短距离
然后   深搜,寻找方法数
*/

#include<stdio.h>
#include<string.h>
//#include<math.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;

#define N 100

int abs(int x)
{
if(x<0) return -x;
return x;
}

struct node
{
int x,y,step;
};

int dir[4][2]={-1,0,0,1,0,-1,1,0};

int visted

;
int map

;

int n,m;
int x1,y1,x2,y2;
int num;
int minstep;

int bfs()
{

queue<node>qq;

node s,cur,next;

s.x=x1;
s.y=y1;
s.step=0;

visted[x1][y1]=1;
qq.push(s);

while(!qq.empty())
{
cur=qq.front();
qq.pop();

if(cur.x==x2 && cur.y==y2)
return cur.step;

for(int i=0;i<4;i++)
{
next.x=cur.x+dir[i][0];
next.y=cur.y+dir[i][1];

if(next.x>=1 && next.x<=n && next.y>=1 && next.y<=m && visted[next.x][next.y]==0 )
{
next.step = cur.step + 1;
qq.push(next);
visted[next.x][next.y]=1;
}
}
}
return -1;
}

void dfs(int x3,int y3,int step)
{
if(x3==x2 && y3==y2 && step==minstep)
{
num++;
return ;
}

if(abs(x3-x2)+abs(y3-y2)+step>minstep) return ;

for(int i=0;i<4;i++)
{
int x=x3+dir[i][0];
int y=y3+dir[i][1];

if(x>=1 && x<=n && y>=1 && y<=m  && map[x][y]==0)
{
map[x][y]=1;
dfs(x,y,step+1);
map[x][y]=0;
}
}

}

int main()
{
int k,x,y;

while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
memset(visted,0,sizeof(visted));
memset(map,0,sizeof(map));

while(k--)
{
scanf("%d%d",&x,&y);
visted[x][y]=1;
map[x][y]=1;
}

scanf("%d%d",&x1,&y1);
scanf("%d%d",&x2,&y2);
minstep=-1;

minstep=bfs();

if(minstep==-1)
{
printf("No Solution!\n");
}
else
{
printf("%d\n",minstep);
num=0;
dfs(x1,y1,0);
printf("%d\n",num);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: