校内赛 下忍考试

//View Code
  1 #include <iostream>
  2 #include <algorithm>
  3 #include <queue>
  4 using namespace std;
  5 const int INF = 0x3ffffff;
  6 struct Node{
  7     bool operator<(Node a)const{
  8         return time > a.time;
  9     }
 10     int x;
 11     int y;
 12     int time;
 13     int sudu;
 14     int juan;
 15     int juannum;
 16 };
 17 int point[4][2] = {-1,0,1,0,0,-1,0,1};
 18 int n,m;
 19 char map[110][110];
 20 int used[110][110][100];
 21 int BFS(Node s){
 22     priority_queue<Node> q;//由于有速度的比较  先到的不一定快  要用优先队列
 23     memset(used,0,sizeof(used));
 24     q.push(s);
 25     used[s.x][s.y][s.juan] = 1;
 26     while(!q.empty()){
 27         Node mid = q.top();
 28         q.pop();
 29         int i;
 30         for(i=0;i<4;i++){
 31             Node ans = mid;
 32             ans.x = ans.x + point[i][0];
 33             ans.y = ans.y + point[i][1];
 34             ans.time += ans.sudu;
 35             if(map[ans.x][ans.y] == 'T'){
 36                 return ans.time;
 37             }
 38             if(ans.x >= 0 && ans.x < n && ans.y >= 0 && ans.y < m
 39                 && !used[ans.x][ans.y][ans.juan] && map[ans.x][ans.y] != 'X'){
 40 
 41                     if(map[ans.x][ans.y] >= '0' && map[ans.x][ans.y] <= '5'){
 42                         int x = (ans.juan >> (map[ans.x][ans.y] - '0'));
 43                         if(x & 1){
 44                             used[ans.x][ans.y][ans.juan] = 1;
 45                             q.push(ans);
 46                         }
 47                         else{
 48                             ans.juan = (ans.juan | 1<< (map[ans.x][ans.y] - '0'));
 49                             ans.juannum ++;
 50                             used[ans.x][ans.y][ans.juan] = 1;
 51                             ans.sudu = 6 - (ans.juannum * 2 - 1);
 52                             q.push(ans);
 53                         }
 54                     }
 55                     else{
 56                         used[ans.x][ans.y][ans.juan] = 1;
 57                         q.push(ans);
 58                     }
 59             }
 60         }
 61     }
 62     return INF;
 63 }
 64 int main(){
 65     int T;
 66     cin>>T;
 67     while(T--){
 68         cin>>n>>m;
 69     //    freopen("out.txt","w",stdout);
 70         memset(map,0,sizeof(map));
 71         Node s = {0};
 72         int i,j;
 73         int num = 1;
 74 
 75         for(i=0;i<n;i++){
 76             for(j=0;j<m;j++){
 77                 cin>>map[i][j];
 78                 if(map[i][j] == 'S'){
 79                     s.x = i;
 80                     s.y = j;
 81                 }
 82                 if(map[i][j] == 'B'){
 83                     map[i][j] = '0' + num;
 84                     num++;
 85                 }
 86             }
 87         }
 88         s.sudu = 6;
 89         int ans = BFS(s);
 90         if(ans >= INF/2){
 91             cout<<-1<<endl;
 92         }
 93         else{
 94             cout<<ans<<endl;
 95         }
 96     }
 97     return 0;
 98 }
 99 /*
100 2
101 14 40
102 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
103 X......................................X
104 X......................................X
105 X......................................X
106 X......................................X
107 X......................................X
108 X..............XXX.....................X
109 X..............XBX.....................X
110 X..............X.X.....................X
111 X..............X.X.....................X
112 X..............X.X.....................X
113 XXXXXXXXXXXXXXXX.X.....................X
114 XXB..............XXXXXXXXXXXXXXXXXXXXXXX
115 XS.XXXXXXXXXXXXXB.....................TX
116 
117 */

 

posted @ 2013-04-21 21:58  zx雄  阅读(207)  评论(0编辑  收藏  举报