标签:ref 针对 tle mod log i++ char 需要 code
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8175 Accepted Submission(s): 2933
//显然bfs,但是要记录访问过的状态,用二进制记录访问过的状态vis[21][21][1<<11],[1<<10]记录10把钥匙的状态 #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; struct Lu { char mp[21][21]; int px,py,tim; int sta; }L; int dir[4][2]={1,0,-1,0,0,1,0,-1}; bool vis[21][21][1<<11]; int n,m,t; int bfs() { queue<Lu>q; Lu L1,L2; memset(vis,0,sizeof(vis)); vis[L.px][L.py][0]=1; L.tim=0;L.sta=0; q.push(L); while(!q.empty()){ L1=q.front(); q.pop(); if(L1.tim>=t) break;//超过时间就不用搜了 if(L1.mp[L1.px][L1.py]==‘^‘) return L1.tim; for(int i=0;i<4;i++){ int x=L1.px+dir[i][0],y=L1.py+dir[i][1]; if(x<0||x>=n||y<0||y>=m) continue; if(L1.mp[x][y]==‘*‘) continue; if(L1.mp[x][y]>=‘a‘&&L1.mp[x][y]<=‘j‘){ L2=L1; L2.sta=(L1.sta|(1<<(L1.mp[x][y]-‘a‘))); vis[x][y][L1.sta]=1; L2.px=x;L2.py=y; L2.mp[x][y]=‘.‘; L2.tim++; q.push(L2); } if(L1.mp[x][y]>=‘A‘&&L1.mp[x][y]<=‘J‘){ int now=(1<<(L1.mp[x][y]-‘A‘)); if(!(now&L1.sta)) continue; L2=L1; vis[x][y][L1.sta]=1; L2.px=x;L2.py=y; L2.mp[x][y]=‘.‘; L2.tim++; q.push(L2); } else if(!vis[x][y][L1.sta]){ L2=L1; vis[x][y][L1.sta]=1; L2.px=x;L2.py=y; L2.tim++; q.push(L2); } } } return -1; } int main() { while(scanf("%d%d%d",&n,&m,&t)!=EOF){ for(int i=0;i<n;i++){ scanf("%s",L.mp[i]); for(int j=0;j<m;j++) if(L.mp[i][j]==‘@‘) {L.px=i;L.py=j;} } int ans=bfs(); printf("%d\n",ans); } return 0; }
标签:ref 针对 tle mod log i++ char 需要 code
原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6287626.html