Advertisement
SuitNdtie

Robot

May 30th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<queue>
  3. using namespace std;
  4. int n,m;
  5. struct pos{
  6.     int I,J;
  7.     pos operator + (const pos& rhs)const
  8.     {
  9.         return {I + rhs.I , J  + rhs.J};
  10.     }
  11. };
  12.  
  13. bool IsIb(pos a){
  14.     return (0 <= a.I && a.I < n && 0 <= a.J && a.J < m);
  15. }
  16.  
  17. struct elem{
  18.     pos ps;
  19.     int dis;
  20.     bool operator < (const elem& rhs)const
  21.     {
  22.         return dis > rhs.dis;
  23.     }
  24. };
  25.  
  26. int main()
  27. {
  28.     scanf("%d %d",&n,&m);
  29.     char board[n][m+1];
  30.     for(int i = 0 ; i < n ; i ++){
  31.         scanf(" %s",board[i]);
  32.     }
  33.     pos Xpos[110];
  34.     pos Apos[110];
  35.     int xn = 0 , an = 0;
  36.    
  37.     priority_queue<elem> pq;
  38.     int dist[n][m];
  39.     bool visited[n][m];
  40.     for(int i = 0 ; i < n ; i ++){
  41.         for(int j = 0 ; j < m ; j ++){
  42.             visited[i][j] = false;
  43.             dist[i][j] = 1e9;
  44.             if(board[i][j] == 'X'){
  45.                 Xpos[xn++] = {i,j};
  46.                 pq.push({i,j,0});
  47.                 dist[i][j] = 0;
  48.             }
  49.             else if(board[i][j] == 'A'){
  50.                 Apos[an++] = {i,j};
  51.             }
  52.         }
  53.     }
  54.    
  55.     while(!pq.empty()){
  56.         pos u = pq.top().ps;
  57.         pq.pop();
  58.         if(visited[u.I][u.J])continue;
  59.         visited[u.I][u.J] = true;
  60.     //  printf("Test u : (%d,%d)\n",u.I,u.J);
  61.         pos move[4] = {{-1,0},{0,1},{1,0},{0,-1}};
  62.         for(int i = 0 ; i < 4 ; i ++){
  63.             pos v = u + move[i];
  64.             if(IsIb(v) && board[v.I][v.J] != 'W' && !visited[v.I][v.J] && dist[u.I][u.J] + 1 < dist[v.I][v.J] ){
  65.                 dist[v.I][v.J] = dist[u.I][u.J] + 1;
  66.                 pq.push({v,dist[v.I][v.J]});
  67.             }
  68.         }
  69.     }
  70.     int cnt = 0;
  71.     int ans = 0;
  72. //  printf("Test an = %d\n",an);
  73.     for(int i = 0 ; i < an ; i ++){
  74.         pos a = Apos[i];
  75.         int da = dist[a.I][a.J];
  76.     //  printf("Test #%d : %d\n",i,da);
  77.         if(da != 1e9){
  78.             ans += da * 2;
  79.             cnt++; 
  80.         }
  81.     }
  82.     printf("%d %d",cnt,ans);
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement