Problem 2186 小明的迷宫
Accept: 65 Submit: 196
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
小明误入迷宫,塞翁失马焉知非福,原来在迷宫中还藏着一些财宝,小明想获得所有的财宝并离开迷宫。因为小明还是学生,还有家庭作业要做,所以他想尽快获得所有财宝并离开迷宫。
Input
有多组测试数据。
每组数据第一行给出两个正整数n,m(0<n,m<=100)。代表迷宫的长和宽。
接着n行,每行m个整数。正数代表财宝(财宝的个数不超过10);负数代表墙,无法通过;0代表通道。
每次移动到相邻的格子,所花费的时间是1秒。小明只能按上、下、左、右四个方向移动。
小明的初始位置是(1,1)。迷宫的出口也在(1,1)。
Output
输出获得所有财宝并逃出迷宫所花费的最小时间,如果无法完成目标则输出-1。
Sample Input
3 30 0 00 100 00 0 02 21 11 1
Sample Output
44
Source
FOJ有奖月赛-2015年03月
题意:如题。
做法:方法1:可以用bfs+dfs做,因为宝藏最多不超过10个。
方法2:用bfs+旅行商算法(状态压缩)。
BFS:用来求出宝藏起点间的最短距离。
bfs+dfs:
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define LL long long
using namespace std;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int s[105][105];
int vis[105][105],h,n,m,tt;
int vv[20];
int num[15][15];
int ans;
struct node
{
int x, y;
int time;
}st[15],aa,bb;
queue<node> q;
bool check(int x, int y)
{
if(x>=0 && x<h && y>=0 && y<n)
return true;
return false;
}
int BFS(int a,int b)
{
int x, y, z, t, i;
aa.x=st[a].x;
aa.y=st[a].y;
aa.time=0;
vis[aa.x][aa.y]=1;
q.push(aa);
bb.x=st[b].x;
bb.y=st[b].y;
while(!q.empty())
{
node tmp = q.front();
q.pop();
x = tmp.x;
y = tmp.y;
t = tmp.time;
for(i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(check(nx,ny)&&!vis[nx][ny] && s[nx][ny]>=0 )
{
if(nx == bb.x && ny == bb.y)
return t+1;
vis[nx][ny] = 1;
node temp;
temp.x=nx;
temp.y=ny;
temp.time=t + 1;
q.push(temp);
}
}
}
return -1;
}
void dfs(int x,int nm,int step)
{
int i;
if(step>ans)
return;
if(nm==tt&&num[x][0]!=-1)
{
ans=min(ans,step+num[x][0]);
return;
}
for(i=0;i<tt;i++)
{
if(vv[i]==0&&num[x][i]!=-1)
{
vv[i]=1;
dfs(i,nm+1,step+num[x][i]);
vv[i]=0;
}
}
}
int main()
{
int i,j,k;
while(scanf("%d%d",&h,&n)!=EOF)
{
memset(vv,0,sizeof(vv));
memset(num,0,sizeof(num));
tt=0;
ans=inf;
st[tt].x=0;
st[tt].y=0;
st[tt++].time=0;
for(i=0;i<h;i++)
{
getchar();
for(j=0;j<n;j++)
{
scanf("%d",&s[i][j]);
if(s[i][j]>0&&(i+j!=0))
{
st[tt].x=i;
st[tt].y=j;
st[tt++].time=0;
}
}
}
if(s[0][0]<0)
{
printf("-1\n");
continue;
}
if(tt==1)
{
printf("0\n");
continue;
}
for(i=0;i<tt;i++)
{
for(j=i;j<tt;j++)
{
if(i!=j)
{
while(!q.empty())
q.pop();
memset(vis,0,sizeof(vis));
num[j][i]=num[i][j]=BFS(i,j);
}
}
}
dfs(0,0,0);
if(ans==inf)
printf("-1\n");
else
printf("%d\n",ans);
}
}
bfs+状压:
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define LL long long
using namespace std;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int s[105][105];
int vis[105][105],h,n,m,tt;
int vv[20];
int num[15][15];
int ans=inf;
struct node
{
int x, y;
int time;
}st[15],aa,bb;
queue<node> q;
bool check(int x, int y)
{
if(x>=0 && x<h && y>=0 && y<n)
return true;
return false;
}
int BFS(int a,int b)
{
int x, y, z, t, i;
aa.x=st[a].x;
aa.y=st[a].y;
aa.time=0;
vis[aa.x][aa.y]=1;
q.push(aa);
bb.x=st[b].x;
bb.y=st[b].y;
while(!q.empty())
{
node tmp = q.front();
q.pop();
x = tmp.x;
y = tmp.y;
t = tmp.time;
for(i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(check(nx,ny)&&!vis[nx][ny] && s[nx][ny]>=0 )
{
if(nx == bb.x && ny == bb.y)
return t+1;
vis[nx][ny] = 1;
node temp;
temp.x=nx;
temp.y=ny;
temp.time=t + 1;
q.push(temp);
}
}
}
return -1;
}
int main()
{
int i,j,k;
while(scanf("%d%d",&h,&n)!=EOF)
{
memset(vv,0,sizeof(vv));
memset(num,0,sizeof(num));
tt=0;
st[tt].x=0;
st[tt].y=0;
st[tt++].time=0;
for(i=0;i<h;i++)
{
getchar();
for(j=0;j<n;j++)
{
scanf("%d",&s[i][j]);
if(s[i][j]>0&&(i+j!=0))
{
st[tt].x=i;
st[tt].y=j;
st[tt++].time=0;
}
}
}
if(s[0][0]<0)
{
printf("-1\n");
continue;
}
if(tt==1)
{
printf("0\n");
continue;
}
for(i=0;i<tt;i++)
{
for(j=i;j<tt;j++)
{
if(i!=j)
{
while(!q.empty())
q.pop();
memset(vis,0,sizeof(vis));
num[j][i]=num[i][j]=BFS(i,j);
}
}
}
int dp[2100][15];
memset (dp, inf, sizeof(dp) );
dp[0][0] = 0;
for (int i = 0; i < (1 << tt); ++i)
{
for (int j = 0; j < tt; ++j)
{
for (int k = 0; k < tt; ++k)
{
if(( i & (1 << j) ) == 0)
{
if(num[k][j]!=-1)
dp[i | (1 << j)][j] = min(dp[i | ( 1 << j)][j], dp[i][k] + num[k][j]);
}
}
}
}
if(dp[(1 << tt) - 1][0]!=inf)
printf("%d\n", dp[(1 << tt) - 1][0]);
else
printf("-1\n");
}
}