1.首先迷宫问题最短路径,肯定想到了BFS,但是此题多了一个条件,就是有最多十扇门,而且必须找到钥匙才能开门,题目的数据都很小。那么我们不难想到,不能单纯地使用 v i s [ x ] [ y ] vis[x][y] vis[x][y]来判重,因为有钥匙的状态和没有钥匙的状态是不同的,每一格可以在不同状态下重复走
2.因为最多有10把钥匙,那么我们使用二进制表示每一位是否取过,即 1 < < i 1<<i 1<<i表示第 i i i把钥匙的状态,使用三维的vis数组判重,并在结构体中保存每个节点的状态,直接BFS即可。那么:
- s t a t e & ( 1 < < i ) {state\&(1<<i)} state&(1<<i)表示第 i i i扇门的钥匙是否得到
- s t a t e ∣ ( 1 < < i ) {state|(1<<i)} state∣(1<<i)表示取到第 i i i把钥匙
代码:
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define lowbit(x) (x&(-x))
#define mkp(x,y) make_pair(x,y)
#define mem(a,x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=2e5+10;
const int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
struct node{
int x,y,p,st;
};
char g[25][25];
bool vis[25][25][1<<11];
int n,m,t;
int sx,sy,ex,ey;
int bfs(){
queue<node> q;
q.push(node{sx,sy,0,0});
vis[sx][sy][0]=1;
while(!q.empty()){
node u=q.front();
q.pop();
if(u.x==ex && u.y==ey) return u.p>=t?-1:u.p;
for(int i=0;i<4;i++){
int xx=u.x+dx[i],yy=u.y+dy[i],cur=u.st;
if(xx<0 || xx>=n || yy<0 || yy>=m) continue;
if(g[xx][yy]=='*') continue;
if(u.p+1>=t) continue;
if(g[xx][yy]>='A' && g[xx][yy]<='J'){
int ok=cur&(1<<(g[xx][yy]-'A'));
if(!vis[xx][yy][cur] && ok){
q.push(node{xx,yy,u.p+1,cur});
vis[xx][yy][cur]=1;
//g[xx][yy]='.'; 这样多此一举会错,不知道为什么
}
}else if(g[xx][yy]>='a' && g[xx][yy]<='j'){
cur=cur|(1<<(g[xx][yy]-'a'));
if(!vis[xx][yy][cur]){
q.push(node{xx,yy,u.p+1,cur});
vis[xx][yy][cur]=1;
//g[xx][yy]='.';
}
}else if(!vis[xx][yy][cur]){
q.push(node{xx,yy,u.p+1,cur});
vis[xx][yy][cur]=1;
}
}
}
return -1;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
while(cin>>n>>m>>t){
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
cin>>g[i][j];
if(g[i][j]=='@') sx=i,sy=j;
if(g[i][j]=='^') ex=i,ey=j;
}
mem(vis,0);
cout<<bfs()<<endl;
}
return 0;
}
1.首先不难发现灯的数量很少,那么我们可以状压表示每个灯的打开状态。本题不能用dp,因为有后效性(这一点本蒟蒻目前不会证明= =)
2.将每种状态看着一个节点,那么根据对按钮的操作可以将这些节点连接起来,那么就是隐氏图的最短路问题,显然也是BFS求解,关于状态变化:
- s t a t e & ( 1 < < i − 1 ) {state\&(1<<i-1)} state&(1<<i−1)表示取到第 i i i个灯的状态,注意因为这里下标是从 1 − n 1-n 1−n因此用 i − 1 i-1 i−1,如果是 0 − ( n − 1 ) 0-(n-1) 0−(n−1)直接 1 < < i 1<<i 1<<i即可
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define lowbit(x) (x&(-x))
#define mkp(x,y) make_pair(x,y)
#define mem(a,x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=2e5+10;
struct node{
int s,p;
};
int n,m;
int a[105][105];
bool vis[maxn];
int bfs(){
queue<node> q;
q.push(node{(1<<n)-1,0});
vis[(1<<n)-1]=1;
while(!q.empty()){
node u=q.front();
q.pop();
if(u.s==0) return u.p;
for(int i=1;i<=m;i++){
int x=u.s;
for(int j=1;j<=n;j++){
if(a[i][j]==1 && (x&(1<<j-1)) ) x^=(1<<j-1);
else if(a[i][j]==-1 && !(x&(1<<j-1)) ) x^=(1<<j-1);
}
if(!vis[x]){
q.push(node{x,u.p+1});
vis[x]=1;
}
}
}
return -1;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
cout<<bfs()<<endl;
return 0;
}