您的位置:首页 > 其它

HDU 1429 胜利大逃亡(续) 【bfs+状压】

2015-11-13 14:46 375 查看
题意:中文题。。。。

思路:key用二进制来表示钥匙。1代表有钥匙,0代表没有钥匙。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
#define inf 1073741824
#define llinf 4611686018285162540LL
#define eps 1e-8
#define mod 9223372034707292160LL
#define pi acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define drep(i,a,b) for(int i=a;i>=b;i--)
#define mset(x,val) memset(x,val,sizeof(x))
#define mcpy(x,y) memcpy(x,y,sizeof(y))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define mpii(a,b) make_pair(a,b);
#define NN 101010
#define MM 202020
using namespace std;
typedef long long ll;
typedef long double lb;
typedef pair<int,int> pii;
char s[22][22];
int n,m,t;
int sx,sy,ex,ey;
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
struct node{
int step,x,y,key;
node(){}
node(int xx,int yy,int k,int s){
step=s,x=xx,y=yy,key=k;
}
}q[MM*3];
int vis[22][22][1028];
int head,tail;

int bfs(){
head=tail=0;
q[tail++]=node(sx,sy,0,0);
vis[sx][sy][0]=1;
while(head<tail){
node top=q[head++];
rep(i,0,3){
int x=top.x+dx[i];
int y=top.y+dy[i];
int k=top.key,pk=0,st=top.step;
if(x<0||x>=n||y<0||y>=m||st+1>=t||s[x][y]=='*'||vis[x][y][k]==1) continue;
if(s[x][y]=='^') return st+1;
if(s[x][y]>='A'&&s[x][y]<='J'){
int pd=k>>(s[x][y]-'A');
if(pd%2==0) continue;
}
if(s[x][y]>='a'&&s[x][y]<='j') pk=1<<(s[x][y]-'a');
vis[x][y][k]=1;
q[tail++]=node(x,y,k|pk,st+1);
}
}
return -1;
}

int main(){
while(scanf("%d%d%d",&n,&m,&t)!=EOF){
mset(vis,0);
rep(i,0,n-1){
scanf("%s",s[i]);
rep(j,0,m-1) if(s[i][j]=='@') sx=i,sy=j;
}
int ans=bfs();
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: