标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7895 Accepted Submission(s): 2795
#include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <iostream> using namespace std; const int N = 22; int n,m,t; int vis[N][N][1<<10]; ///第三维存的钥匙的状态 char graph[N][N]; struct Node{ int x,y,step,state; }s; int dir[][2] = {{-1,0},{1,0},{0,1},{0,-1}}; bool check(int x,int y){ if(x<1||y<1||x>n||y>m||graph[x][y]==‘*‘) return false; return true; } int bfs(){ memset(vis,false,sizeof(vis)); queue<Node> q; q.push(s); vis[s.x][s.y][0] = 0; while(!q.empty()){ Node now = q.front(); q.pop(); if(now.step>=t) return -1; if(graph[now.x][now.y]==‘^‘) return now.step; Node next; for(int i=0;i<4;i++){ next.x = now.x+dir[i][0]; next.y = now.y+dir[i][1]; next.step = now.step+1; next.state = now.state; if(!check(next.x,next.y)||vis[next.x][next.y][next.state]) continue; char c = graph[next.x][next.y]; if(isupper(c)){ int t = c-‘A‘; if(!((1<<t)&next.state)) continue; } if(islower(c)){ int t = c-‘a‘; next.state = (1<<t)|next.state; } if(!vis[next.x][next.y][next.state]){ vis[next.x][next.y][next.state] = true; q.push(next); } } } return -1; ///没加这句wa无数发....因为它有可能在前面两个出口都没出去..以后写程序一定要严谨点 } int main(){ while(scanf("%d%d%d",&n,&m,&t)!=EOF){ for(int i=1;i<=n;i++){ scanf("%s",graph[i]+1); for(int j=1;j<=m;j++){ if(graph[i][j]==‘@‘){ graph[i][j]=‘.‘; s.x = i; s.y = j; s.step = 0; s.state = 0; } } } printf("%d\n",bfs()); } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5747556.html